JETTY嵌入式Web容器的开发(一)---基础开发
JETTY嵌入式Web容器体积小,启动速度快,性能好,免费开源,是一款很适合开发调试和简单演示的轻量级Web容器.
而且它的嵌入式的特点,使得开发者可以直接将容器的操作包含在程序逻辑里,得以摆脱TOMCAT,JBOSS等独立容器带来的安装,维护,部署等一系列令人头疼的问题.
JETTY嵌入式开发步骤
- 下载
本文采用的是2012年最新版本的JETTY8 (8.1.4版)
jetty-hightide-8.1.4.v20120524.zip
- ECLIPSE建立一个普通JAVA项目testjetty
注意:
是JAVA APPLICATION项目,不是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
我做了下面两处修改(红字),以利于测试
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server"> <Set name="ThreadPool"> <!-- Default queued blocking threadpool --> <New class="org.eclipse.jetty.util.thread.QueuedThreadPool"> <Set name="minThreads">30</Set> <Set name="maxThreads">200</Set> <Set name="detailedDump">false</Set> </New> </Set>
<Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.server.nio.SelectChannelConnector"> <Set name="host"><Property name="jetty.host" /></Set> <Set name="port"><Property name="jetty.port" default="8088"/></Set> <Set name="maxIdleTime">300000</Set> <Set name="Acceptors">2</Set> <Set name="statsOn">false</Set> <Set name="confidentialPort">8443</Set> <Set name="lowResourcesConnections">20000</Set> <Set name="lowResourcesMaxIdleTime">5000</Set> </New> </Arg> </Call>
|
- JETTY Service类
package org.jetty.demo;
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.lang.StringUtils; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.xml.XmlConfiguration; import org.xml.sax.SAXException;
public class JettyCustomServer extends Server {
private String xmlConfigPath;
private String contextPath;
private String warPath;
private String resourceBase = "./webRoot";
private String webXmlPath = "./webRoot/WEB-INF/web.xml";
public JettyCustomServer(String xmlConfigPath,String contextPath,String resourceBase,String webXmlPath) { this(xmlConfigPath,contextPath,resourceBase,webXmlPath,null); }
public JettyCustomServer(String xmlConfigPath,String contextPath) { this(xmlConfigPath,contextPath,null,null,null); }
public JettyCustomServer(String xmlConfigPath,String contextPath,String warPath) { this(xmlConfigPath,contextPath,null,null,warPath); }
public JettyCustomServer(String xmlConfigPath,String contextPath,String resourceBase,String webXmlPath,String warPath) { 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); } }
}
private void readXmlConfig(){ try { XmlConfiguration configuration = new XmlConfiguration(new FileInputStream(this.xmlConfigPath)); configuration.configure(this); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (SAXException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
public void applyHandle(Boolean warDeployFlag){
ContextHandlerCollection handler = new ContextHandlerCollection();
WebAppContext webapp = new WebAppContext(); 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); }
public void startServer(){ try { super.start(); System.out.println("current thread:"+super.getThreadPool().getThreads()+"| idle thread:"+super.getThreadPool().getIdleThreads()); super.join(); } catch (Exception e) { e.printStackTrace(); }
}
public String getXmlConfigPath() { return xmlConfigPath; }
public void setXmlConfigPath(String xmlConfigPath) { this.xmlConfigPath = xmlConfigPath; }
public String getContextPath() { return contextPath; }
public void setContextPath(String contextPath) { this.contextPath = contextPath; }
public String getResourceBase() { return resourceBase; }
public void setResourceBase(String resourceBase) { this.resourceBase = resourceBase; }
public String getWebXmlPath() { return webXmlPath; }
public void setWebXmlPath(String webXmlPath) { this.webXmlPath = webXmlPath; }
public String getWarPath() { return warPath; }
public void setWarPath(String warPath) { this.warPath = warPath; }
} |
- 做一个简单的可执行的服务启动类
package org.jetty.demo;
public class JettyServerStart {
public static void main(String[] args) { JettyCustomServer server = newJettyCustomServer("./jetty/etc/jetty.xml","/testContext"); server.startServer();
} } |
- /webRoot/目录下做一个JSP测试页面
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> hello jetty! 中国! </body> </html> |
- 做一个测试Servlet
package org.jetty.demo;
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet{
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet"); resp.getWriter().write("hello world!"); resp.getWriter().close(); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost"); super.doPost(req, resp); }
} |
- /webRoot/WEB-INF目录下建立一个web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name></display-name>
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>org.jetty.demo.HelloWorldServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/servlet/HelloWorldServlet</url-pattern> </servlet-mapping>
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
- 启动服务
执行JettyServerStart |
CONSOLE日志: 2012-06-27 15:49:35.250:INFO:oejs.Server:jetty-8.1.4.v20120524 2012-06-27 15:49:35.484:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/} 2012-06-27 15:49:35.484:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/} 2012-06-27 15:49:35.703:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8088 current thread:30| idle thread:26
看红字部分,记得我们在./jetty/etc/jetty.xml文件中做的修改么? |
测试
测试访问JSP页面
http://localhost:8088/testContext/index.jsp
测试访问SERVLET