jetty
jetty 类似tomcat 可以项目整体发布 (拷贝进去)
区别可以嵌入 近java代码启动。
main 启动实例。
一,全main 配合springMVC (java代码指定 处理类Holder)
1 /*初始化服务s*/ 2 Resource fileserver_xml=null; 3 if(path!=null) fileserver_xml = Resource.newResource("file:"+path+"jetty.xml"); 4 else fileserver_xml = Resource.newClassPathResource("jetty.xml"); 5 XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream()); 6 Server server = (Server)configuration.configure(); 7 8 /*重新设置上下文*/ 9 ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 10 context.setContextPath("/"); 11 /*设置请求分发 servlet 使用spring mvc*/ 12 ServletHolder holder = new ServletHolder(); 13 holder.setClassName("org.springframework.web.servlet.DispatcherServlet"); 14 String cc="classpath*:application-config.xml"; 15 if(path!=null) cc="file:"+path+"application-config.xml"; 16 holder.setInitParameter("contextConfigLocation",cc); 17 context.addServlet(holder, "/*"); 18 /*server 增加上下文 handler 以及jetty默认handler*/ 19 /*HandlerCollection handlers = new HandlerCollection(); 20 handlers.setHandlers(new Handler[] { context, new DefaultHandler() });*/ 21 server.setHandler(context); 22 if(path!=null) StaticFactory.setFislePath(path+"mybatis-config.xml");/*配置绝对路径sql*/ 23 server.start(); 24 server.join();
jetty 配置文件
1 <?xml version="1.0"?> 2 <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> 3 4 <Configure id="ExampleServer" class="org.eclipse.jetty.server.Server"> 5 6 <Set name="connectors"> 7 <Array type="org.eclipse.jetty.server.Connector"> 8 <Item> 9 <New class="org.eclipse.jetty.server.ServerConnector"> 10 <Arg><Ref refid="ExampleServer"/></Arg> 11 <Set name="port">10000</Set> 12 </New> 13 </Item> 14 </Array> 15 </Set> 16 17 </Configure>
二,main配置 jetty配置文件指定
1 /*初始化服务s*/ 2 Resource fileserver_xml=null; 3 if(path!=null) fileserver_xml = Resource.newResource("file:"+path+"jetty.xml"); 4 else fileserver_xml = Resource.newClassPathResource("jetty.xml"); 5 XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream()); 6 Server server = (Server)configuration.configure(); 7 8 server.start(); 9 server.join();
1 <?xml version="1.0"?> 2 <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> 3 4 <Configure id="ExampleServer" class="org.eclipse.jetty.server.Server"> 5 6 <Set name="connectors"> 7 <Array type="org.eclipse.jetty.server.Connector"> 8 <Item> 9 <New class="org.eclipse.jetty.server.ServerConnector"> 10 <Arg><Ref refid="ExampleServer"/></Arg> 11 <Set name="port">10006</Set> 12 </New> 13 </Item> 14 </Array> 15 </Set> 16 <New id="servletHolder" class="org.eclipse.jetty.servlet.ServletHolder" > 17 <Set name="className">com.myouth.login.Servlet.MicroLoginServlet</Set> 18 <Call name="setInitParameter"><Arg>dburl</Arg><Arg>http://127.0.0.1:10000</Arg></Call> 19 </New> 20 <New id="context" class="org.eclipse.jetty.servlet.ServletContextHandler"> 21 <Set name="contextPath">/</Set> 22 <Call name="addServlet"> 23 <Arg><Ref refid="servletHolder"/></Arg><Arg>/login/microLogin</Arg> 24 </Call> 25 </New> 26 <Set name="handler"> 27 <New class="org.eclipse.jetty.server.handler.HandlerCollection"> 28 <Set name="handlers"> 29 <Array type="org.eclipse.jetty.server.Handler"> 30 <Item> 31 <Ref refid="context" /> 32 </Item> 33 <Item> 34 <New class="org.eclipse.jetty.server.handler.DefaultHandler" /> 35 </Item> 36 </Array> 37 </Set> 38 </New> 39 </Set> 40 </Configure>