|
qiuJETTY嵌入式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就放在这里
建成的项目的结构如下:

我做了下面两处修改(粗体),以利于测试
"1.0"?>
"Server" class="org.eclipse.jetty.server.Server">
"ThreadPool">
"org.eclipse.jetty.util.thread.QueuedThreadPool">
"minThreads">30
"maxThreads">200
"detailedDump">false
"addConnector">
"org.eclipse.jetty.server.nio.SelectChannelConnector">
"host">"jetty.host" />
"port">"jetty.port" default="8088"/>
"maxIdleTime">300000
"Acceptors">2
"statsOn">false
"confidentialPort">8443
"lowResourcesConnections">20000
"lowResourcesMaxIdleTime">5000
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();
}
}
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
"Content-Type" content="text/html; charset=GBK">
Insert title here
hello jetty! 中国!
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
"1.0" encoding="UTF-8"?>
"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">
This is the description of my J2EE component
This is the display name of my J2EE component
HelloWorldServlet
org.jetty.demo.HelloWorldServlet
HelloWorldServlet
/servlet/HelloWorldServlet
index.jsp

执行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
http://localhost:8088/testContext/servlet/HelloWorldServlet

refurl:http://my.oschina.net/u/989066/blog/157073 |
|