1.准备工作:
Jetty下载地址:http://dist.codehaus.org/jetty/jetty-hightide-8.1.14/
Jetty源码下载地址:http://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty
添加的jar包:
jetty-continuation-8.1.14.v20131031.jar jetty-http-8.1.14.v20131031.jar jetty-io-8.1.14.v20131031.jar jetty-security-8.1.14.v20131031.jar jetty-server-8.1.14.v20131031.jar jetty-servlet-8.1.14.v20131031.jar jetty-util-8.1.14.v20131031.jar servlet-api-3.0.jar
添加以下三个jar,为了能看到启动日志:
logback-classic-1.0.9.jar logback-core-1.0.9.jar slf4j-api-1.7.2.jar
2.上传demo:
package com.potter.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; /** * @Title: TestServletServer.java * @Package com.ddz.web.test * @Description: TODO(用一句话描述该文件做什么) * @author Potter * @date 2013-12-13 上午11:32:27 * @version V2.0 */ public class TestServletServer { public static void main(String[] args) throws Exception{ Server server=new Server(8080); //第一种方式:通过构造函数,设置默认的session ServletContextHandler context=new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); server.setHandler(context); //第二种方式:设置session //Create the SessionHandler (wrapper) to handle the sessions /*HashSessionManager manager = new HashSessionManager(); SessionHandler sessions = new SessionHandler(manager); context.setHandler(sessions);*/ context.addServlet(new ServletHolder(new HelloServlet()), "/*"); server.start(); server.join(); } } class HelloServlet extends HttpServlet{ private static final long serialVersionUID = 1L; private String greeting = "Hello World!"; public HelloServlet() { } public HelloServlet(String greeting) { this.greeting = greeting; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>"+greeting+"</h1>"); response.getWriter().println("session=" + request.getSession(true).getId()); } }
测试web是否启动成功:http://127.0.0.1:8080
web显示结果:
----启动日志----
11:41:04.737 [main] DEBUG org.eclipse.jetty.util.log - Logging to Logger[org.eclipse.jetty.util.log] via org.eclipse.jetty.util.log.Slf4jLog
11:41:04.753 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.server.Server@1572e449 + SelectChannelConnector@0.0.0.0:8080 as connector
11:41:04.771 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.server.Server@1572e449 + o.e.j.s.ServletContextHandler{/,null} as handler
11:41:04.810 [main] DEBUG o.e.jetty.servlet.ServletHandler - filterNameMap={}
11:41:04.810 [main] DEBUG o.e.jetty.servlet.ServletHandler - pathFilters=null
11:41:04.810 [main] DEBUG o.e.jetty.servlet.ServletHandler - servletFilterMap=null
11:41:04.810 [main] DEBUG o.e.jetty.servlet.ServletHandler - servletPathMap={/*=com.potter.test.HelloServlet-51b48197}
11:41:04.810 [main] DEBUG o.e.jetty.servlet.ServletHandler - servletNameMap={com.potter.test.HelloServlet-51b48197=com.potter.test.HelloServlet-51b48197}
11:41:04.811 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting org.eclipse.jetty.server.Server@1572e449
11:41:04.813 [main] INFO org.eclipse.jetty.server.Server - jetty-8.1.14.v20131031
11:41:04.826 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.server.Server@1572e449 + qtp683347213{8<=0<=0/254,-1} as threadpool
11:41:04.826 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting o.e.j.s.ServletContextHandler{/,null}
11:41:04.848 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.servlet.ServletHandler@6f9bb25a + com.potter.test.HelloServlet-51b48197 as servlet
11:41:04.849 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.servlet.ServletHandler@6f9bb25a + [/*]=>com.potter.test.HelloServlet-51b48197 as servletMapping
11:41:04.849 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.server.session.SessionHandler@56da6bf4 + org.eclipse.jetty.servlet.ServletHandler@6f9bb25a as handler
11:41:04.849 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.server.session.SessionHandler@56da6bf4 + org.eclipse.jetty.server.session.HashSessionManager@1de58cb8 as sessionManager
11:41:04.849 [main] DEBUG o.e.jetty.util.component.Container - Container o.e.j.s.ServletContextHandler{/,null} + org.eclipse.jetty.server.session.SessionHandler@56da6bf4 as handler
11:41:04.849 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting org.eclipse.jetty.server.session.SessionHandler@56da6bf4
11:41:04.849 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting org.eclipse.jetty.server.session.HashSessionManager@1de58cb8
11:41:04.851 [main] DEBUG o.e.jetty.util.component.Container - Container org.eclipse.jetty.server.Server@1572e449 + org.eclipse.jetty.server.session.HashSessionIdManager@66100363 as sessionIdManager
11:41:04.851 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting org.eclipse.jetty.server.session.HashSessionIdManager@66100363
11:41:04.875 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED org.eclipse.jetty.server.session.HashSessionIdManager@66100363
11:41:04.876 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED org.eclipse.jetty.server.session.HashSessionManager@1de58cb8
11:41:04.877 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting org.eclipse.jetty.servlet.ServletHandler@6f9bb25a
11:41:04.877 [main] DEBUG o.e.jetty.servlet.ServletHandler - filterNameMap={}
11:41:04.877 [main] DEBUG o.e.jetty.servlet.ServletHandler - pathFilters=null
11:41:04.877 [main] DEBUG o.e.jetty.servlet.ServletHandler - servletFilterMap=null
11:41:04.877 [main] DEBUG o.e.jetty.servlet.ServletHandler - servletPathMap={/*=com.potter.test.HelloServlet-51b48197}
11:41:04.877 [main] DEBUG o.e.jetty.servlet.ServletHandler - servletNameMap={com.potter.test.HelloServlet-51b48197=com.potter.test.HelloServlet-51b48197}
11:41:04.877 [main] DEBUG o.e.j.server.handler.AbstractHandler - starting org.eclipse.jetty.servlet.ServletHandler@6f9bb25a
11:41:04.877 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED org.eclipse.jetty.servlet.ServletHandler@6f9bb25a
11:41:04.877 [main] DEBUG o.e.j.server.handler.AbstractHandler - starting org.eclipse.jetty.server.session.SessionHandler@56da6bf4
11:41:04.877 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED org.eclipse.jetty.server.session.SessionHandler@56da6bf4
11:41:04.878 [main] DEBUG o.e.j.server.handler.AbstractHandler - starting o.e.j.s.ServletContextHandler{/,null}
11:41:04.878 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting com.potter.test.HelloServlet-51b48197
11:41:04.879 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED com.potter.test.HelloServlet-51b48197
11:41:04.880 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED o.e.j.s.ServletContextHandler{/,null}
11:41:04.880 [main] DEBUG o.e.j.server.handler.AbstractHandler - starting org.eclipse.jetty.server.Server@1572e449
11:41:04.880 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting qtp683347213{8<=0<=0/254,-1}
11:41:04.881 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED qtp683347213{8<=7<=8/254,0}
11:41:04.882 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting [email protected]:8080
11:41:04.895 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting null/null
11:41:04.897 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED PooledBuffers [0/1024@6144,0/1024@16384,0/1024@-]/PooledBuffers [0/1024@6144,0/1024@32768,0/1024@-]
11:41:04.897 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - starting org.eclipse.jetty.server.nio.SelectChannelConnector$ConnectorSelectorManager@5ff3ce5c
11:41:04.903 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED org.eclipse.jetty.server.nio.SelectChannelConnector$ConnectorSelectorManager@5ff3ce5c
11:41:04.903 [qtp683347213-12 Selector0] DEBUG org.eclipse.jetty.io.nio - Starting Thread[qtp683347213-12 Selector0,5,main] on org.eclipse.jetty.io.nio.SelectorManager$1@69e328e0
11:41:04.904 [main] INFO o.e.jetty.server.AbstractConnector - Started SelectChannelConnector@0.0.0.0:8080
11:41:04.904 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED [email protected]:8080
11:41:04.904 [main] DEBUG o.e.j.u.component.AbstractLifeCycle - STARTED org.eclipse.jetty.server.Server@1572e449
11:42:05.326 [qtp683347213-12 Selector0] DEBUG org.eclipse.jetty.io.nio - created SCEP@7dc5ddc9{l(/127.0.0.1:52719)<->r(/127.0.0.1:8080),s=0,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{AsyncHttpConnection@4963f7a1,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}
11:42:05.326 [qtp683347213-18] DEBUG org.eclipse.jetty.http.HttpParser - filled 390/390
11:42:05.327 [qtp683347213-18 - /] DEBUG org.eclipse.jetty.server.Server - REQUEST / on AsyncHttpConnection@4963f7a1,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=10,c=0},r=1
11:42:05.328 [qtp683347213-18 - /] DEBUG o.e.j.server.handler.ContextHandler - scope null||/ @ o.e.j.s.ServletContextHandler{/,null}
11:42:05.328 [qtp683347213-18 - /] DEBUG o.e.j.server.handler.ContextHandler - context=||/ @ o.e.j.s.ServletContextHandler{/,null}
11:42:05.328 [qtp683347213-18 - /] DEBUG org.eclipse.jetty.server.session - Got Session ID 165xmjlweo3zi6vpqoomc4x3s from cookie
11:42:05.328 [qtp683347213-18 - /] DEBUG org.eclipse.jetty.server.session - sessionManager=org.eclipse.jetty.server.session.HashSessionManager@1de58cb8
11:42:05.328 [qtp683347213-18 - /] DEBUG org.eclipse.jetty.server.session - session=org.eclipse.jetty.server.session.HashedSession:165xmjlweo3zi6vpqoomc4x3s@532404146
11:42:05.329 [qtp683347213-18 - /] DEBUG o.e.jetty.servlet.ServletHandler - servlet ||/ -> com.potter.test.HelloServlet-51b48197
11:42:05.329 [qtp683347213-18 - /] DEBUG o.e.jetty.servlet.ServletHandler - chain=null
11:42:05.329 [qtp683347213-18 - /] DEBUG org.eclipse.jetty.server.Server - RESPONSE / 200 handled=true
11:42:05.330 [qtp683347213-18] DEBUG o.e.jetty.server.AsyncHttpConnection - Enabled read interest SCEP@7dc5ddc9{l(/127.0.0.1:52719)<->r(/127.0.0.1:8080),s=1,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0r}-{AsyncHttpConnection@4963f7a1,g=HttpGenerator{s=4,h=0,b=0,c=-1},p=HttpParser{s=0,l=10,c=0},r=1}
11:42:05.331 [qtp683347213-18] DEBUG org.eclipse.jetty.http.HttpParser - filled 0/0
----连接超时断开日志------
13:00:47.025 [qtp274064735-17] DEBUG o.e.jetty.io.nio.ChannelEndPoint - ishut SCEP@26c623af{l(/127.0.0.1:53195)<->r(/127.0.0.1:8080),s=1,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=1r}-{AsyncHttpConnection@1a50ca0c,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=-3},r=1}
13:00:47.027 [qtp274064735-17] DEBUG o.e.jetty.io.nio.ChannelEndPoint - java.net.SocketException: Socket is not connected
13:00:47.027 [qtp274064735-17] DEBUG org.eclipse.jetty.http.HttpParser - filled -1/0
13:00:47.028 [qtp274064735-17] DEBUG o.e.jetty.server.AsyncHttpConnection - Disabled read interest while writing response SCEP@26c623af{l(/127.0.0.1:53195)<->r(/127.0.0.1:8080),s=1,open=true,ishut=true,oshut=false,rb=false,wb=false,w=true,i=1r}-{AsyncHttpConnection@1a50ca0c,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1}
13:00:47.030 [qtp274064735-17] DEBUG o.e.jetty.io.nio.ChannelEndPoint - close SCEP@26c623af{l(/127.0.0.1:53195)<->r(/127.0.0.1:8080),s=1,open=true,ishut=true,oshut=false,rb=false,wb=false,w=true,i=1r}-{AsyncHttpConnection@1a50ca0c,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1}
13:00:47.033 [qtp274064735-12 Selector0] DEBUG org.eclipse.jetty.io.nio - destroyEndPoint SCEP@26c623af{l(null)<->r(/127.0.0.1:8080),s=0,open=false,ishut=true,oshut=true,rb=false,wb=false,w=true,i=1!}-{AsyncHttpConnection@1a50ca0c,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1}
13:00:47.034 [qtp274064735-12 Selector0] DEBUG o.e.j.server.AbstractHttpConnection - closed AsyncHttpConnection@1a50ca0c,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=-3},r=1
后面将通过例子,来逐步加深对《Jetty 的工作原理以及与 Tomcat 的比较》的理解。