jetty作为内嵌服务器自启动
为了完成web工程的测试,最近内嵌jetty也要搞起来.第一次搞还是挺焦头烂额的.直接上成果:
package com.test.action; import java.io.File; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.webapp.WebAppContext; public class RunTest { public static void main(String[] args) { tt(); } public static void tt() { Server server = new Server();// 创建jetty web容器 server.setStopAtShutdown(true);// 在退出程序是关闭服务 // 创建连接器,每个连接器都是由IP地址和端口号组成,连接到连接器的连接将会被jetty处理 Connector connector = new SelectChannelConnector();// 创建一个连接器 connector.setHost("127.0.0.1");// ip地址 connector.setPort(8080);// 连接的端口号 server.addConnector(connector);// 添加连接 QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setMaxThreads(3000); server.setThreadPool(threadPool); // 配置服务 WebAppContext context = new WebAppContext();// 创建服务上下文 context.setContextPath("/strutsDemo");// 访问服务路径 http://{ip}:{port}/ context.setConfigurationDiscovered(true); String baseDir = Thread.currentThread().getContextClassLoader() .getResource("").getPath(); context.setDescriptor(baseDir + File.separator + "/WEB-INF/web.xml");// 指明服务描述文件,就是web.xml // context.setDescriptor("/E:/workspace/strutsDemo/target/classes/\\/WEB-INF/web.xml");// // 指明服务描述文件,就是web.xml context.setResourceBase(System.getProperty("user.dir") + "/src/main/webapp/");// 指定服务的资源根路径,配置文件的相对路径与服务根路径有关 server.setHandler(context);// 添加处理try { server.start();// 开启服务 server.join(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }
启动访问就可以了.主要是context的配置花了很多功夫,老是配不好.还有据说jetty版本或者jar包不同会有配置差异,我用的是8.0.4版本,顺便贴上maven依赖:
<!--jetty --> <dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-all</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-deploy</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-xml</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-security</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-client</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-ajp</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-annotations</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-websocket</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-util</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-io</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-http</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-continuation</artifactId> <version>8.0.4.v20111024</version> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jsp-2.1-glassfish</artifactId> <version>2.1.v20100127</version> </dependency>
事实上不需要这么多,我这是一劳永逸的偷懒做法,你们可以改进哟