JETTY嵌入式Web容器的开发JETTY嵌入式Web容器体积小,启动速度快,性能好,免费开源,是一款很适合开发调试和简单演示的轻量级Web容器.而且它的嵌入式的特点,使得开发者可以直接将容器的操作包含在程序逻辑里,得以摆脱TOMCAT,JBOSS等独立容器带来的安装,维护,部署等一系列令人头疼的问题.JETTY嵌入式开发步骤下载本文采用的是2012年最新版本的JETTY8(8.1.4版)jetty-hightide-8.1.4.v20120524.zipECLIPSE建立一个普通JAVA项目testjetty注意:是JAVAAPPLICATION项目,不是WAR项目.JDK要1.6在项目里建立jetty目录目录下再建立etc目录将jetty-hightide-8.1.4.v20120524.zip包中etc目录下的jetty.xml和webdefault.xml文件拷贝入新建的/jetty/etc目录下将jetty-hightide-8.1.4.v20120524.zip文件中lib/目录下的所有jar文件和lib/jsp子目录下的所有jar文件都加入项目的buildPath(很多讨论JETTY开发的文章总是在最小JAR包数上做文章,我觉得初学者还是应该将所有JAR包都加入,因为作为一款Web容器,JETTY的JAR容纳了大量的细节功能.有的JAR包你没有加入,也许测HELLOWORLD这类功能能够通过,但在真正的商用页面里,就会针对各种HTTP元素出问题)在项目里建webRoot目录webRoot目录下建立WEB-INF目录,以后web.xml就放在这里建成的项目的结构如下:修改/jetty/etc/jetty.xml我做了下面两处修改(红字),以利于测试?xmlversion=1.0?!DOCTYPEConfigurePUBLIC-//Jetty//Configure//EN=Serverclass=org.eclipse.jetty.server.ServerSetname=ThreadPool!--Defaultqueuedblockingthreadpool--Newclass=org.eclipse.jetty.util.thread.QueuedThreadPoolSetname=minThreads30/SetSetname=maxThreads200/SetSetname=detailedDumpfalse/Set/New/SetCallname=addConnectorArgNewclass=org.eclipse.jetty.server.nio.SelectChannelConnectorSetname=hostPropertyname=jetty.host//SetSetname=portPropertyname=jetty.portdefault=8088//SetSetname=maxIdleTime300000/SetSetname=Acceptors2/SetSetname=statsOnfalse/SetSetname=confidentialPort8443/SetSetname=lowResourcesConnections20000/SetSetname=lowResourcesMaxIdleTime5000/Set/New/Arg/CallJETTYService类packageorg.jetty.demo;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importorg.apache.commons.lang.StringUtils;importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.server.handler.ContextHandlerCollection;importorg.eclipse.jetty.webapp.WebAppContext;importorg.eclipse.jetty.xml.XmlConfiguration;importorg.xml.sax.SAXException;publicclassJettyCustomServerextendsServer{privateStringxmlConfigPath;privateStringcontextPath;privateStringwarPath;privateStringresourceBase=./webRoot;privateStringwebXmlPath=./webRoot/WEB-INF/web.xml;publicJettyCustomServer(StringxmlConfigPath,StringcontextPath,StringresourceBase,StringwebXmlPath){this(xmlConfigPath,contextPath,resourceBase,webXmlPath,null);}publicJettyCustomServer(StringxmlConfigPath,StringcontextPath){this(xmlConfigPath,contextPath,null,null,null);}publicJettyCustomServer(StringxmlConfigPath,StringcontextPath,StringwarPath){this(xmlConfigPath,contextPath,null,null,warPath);}publicJettyCustomServer(StringxmlConfigPath,StringcontextPath,StringresourceBase,StringwebXmlPath,StringwarPath){super();if(StringUtils.isNotBlank(xmlConfigPath)){this.xmlConfigPath=xmlConfigPath;readXmlConfig();}if(StringUtils.isNotBlank(warPath)){this.warPath=warPath;if(StringUtils.isNotBlank(contextPath)){this.contextPath=contextPath;applyHandle(true);}}else{if(StringUtils.isNotBlank(resourceBase))this.resourceBase=resourceBase;if(StringUtils.isNotBlank(webXmlPath))this.webXmlPath=webXmlPath;if(StringUtils.isNotBlank(contextPath)){this.contextPath=contextPath;applyHandle(false);}}}privatevoidreadXmlConfig(){try{XmlConfigurationconfiguration=newXmlConfiguration(newFileInputStream(this.xmlConfigPath));configuration.configure(this);}catch(FileNotFoundExceptione1){e1.printStackTrace();}catch(SAXExceptione1){e1.printStackTrace();}catch(IOExceptione1){e1.printStackTrace();}catch(Exceptione){e.printStackTrace();}}publicvoidapplyHandle(BooleanwarDeployFlag){ContextHandlerCollectionhandler=newContextHandlerCollection();WebAppContextwebapp=newWebAppContext();webapp.setContextPath(contextPath);webapp.setDefaultsDescriptor(./jetty/etc/webdefault.xml);if(!warDeployFlag){webapp.setResourceBase(resourceBase);webapp.setDescriptor(webXmlPath);}else{webapp.setWar(warPath);}handler.addHandler(webapp);super.setHandler(handler);}publicvoidstartServer(){try{super.start();System.out.println(currentthread:+super.getThreadPool().getThreads()+|idlethread:+super.getThreadPool().getIdleThreads());super.join();}catch(Exceptione){e.printStackTrace();}}publicStringgetXmlConfigPath(){returnxmlConfigPath;}publicvoidsetXmlConfigPath(StringxmlConfigPath){this.xmlConfigPath=xmlConfigPath;}publicStringgetContextPath(){returncontextPath;}publicvoidsetContextPath(StringcontextPath){this.contextPath=contextPath;}publicStringgetResourceBase(){returnresourceBase;}publicvoidsetResourceBase(StringresourceBase){this.resourceBase=resourceBase;}publicStringgetWebXmlPath(){returnwebXmlPath;}publicvoidsetWebXmlPath(StringwebXmlPath){this.webXmlPath=webXmlPath;}publicStringgetWarPath(){returnwarPath;}publicvoidsetWarPath(StringwarPath){this.warPath=warPath;}}做一个简单的可执行的服务启动类packageorg.jetty.demo;publicclassJettyServerStart{publicstaticvoidmain(String[]args){JettyCustomServerserver=newJettyCustomServer(./jetty/etc/jetty.xml,/testContext);server.startServer();}}/webRoot/目录下做一个JSP测试页面%@pagelanguage=javacontentType=text/html;charset=GBKpageEncoding=GBK%!DOCTY