最近在做一些簡單的Servlet開發的時候,感覺每次調試的時候都要發布到tomcat上很麻煩,把程序共享給同事也很麻煩,需要幫他設置本地的tomcat環境。 在網上找了找其他的Servlet運行環境,發現用Jetty可以很方便的實現嵌入式Web container.這里我記錄一下通過Jetty搭建簡單Servlet運行環境的過程,希望對有同樣需要的朋友有所幫助。托福答案
整個環境的代碼可以找到。 代碼包括了IntelliJ的項目文件,如果需要eclipse項目文件,請在下載代碼后運行 mvn eclipse:eclipse 來生成eclipse項目文件。 (當然, 請在本地安裝Maven)。
設置Maven Dependency:
[plain]
<dependencies>
<!-- jetty -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
設置servlet-context.xml:
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns=".springframework.org/schema/mvc"
xmlns:beans=".springframework.org/schema/beans"
xmlns:context=".springframework.org/schema/context"
xmlns:xsi=".w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
.springframework.org/schema/mvc .springframework.org/schema/mvc/spring-mvc-3.0.xsd
.springframework.org/schema/beans .springframework.org/schema/beans/spring-beans-3.0.xsd
.springframework.org/schema/context .springframework.org/schema/context/spring-context-3.0.xsd">
<interceptors>
<interceptor>
<mapping path="/*"/>
<beans:bean class="weblog.examples.jettysetup.LoggingInterceptor"/>
</interceptor>
</interceptors>
<context:annotation-config/>
<context:component-scan base-package="weblog.examples.jettysetup.serlvet"/>
</beans:beans>
一個簡單的Main Class:
[java]
public static void main(String[] args) throws Exception {
try {
DOMConfigurator.configure(Thread.currentThread()。getContextClassLoader()。getResource("log4j.xml"));
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(7411);
server.setConnectors(new Connector[] {connector});
DispatcherServlet servlet = new DispatcherServlet();
servlet.setContextConfigLocation("classpath:servlet-context.xml");
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addServlet(new ServletHolder("baseServlet", servlet), "/");
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { context, new DefaultHandler()});
server.setHandler(handlers);
XmlWebApplicationContext wctx = new XmlWebApplicationContext();
wctx.setConfigLocation("");
wctx.setServletContext(servlet.getServletContext());
wctx.refresh();
context.setAttribute(XmlWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wctx);
server.start();
log.info("Jetty embedded server started");
log.info("Press any key to stop");
System.in.read();
log.info("Stopping Server");
server.stop();
log.info("Server stopped");
} catch (Exception ex) {
log.error("Failed to run Jetty Server", ex);
throw ex;
}
}
在JettyLauncher運行后,我們可以訪問localhost:7411/ping來查看Jetty是否成功運行。 localhost:7411/ping將運行以下Spring MVC Servlet:
[java]
@Controller
public class TestServlet {
private static Logger log = Logger.getLogger(TestServlet.class);
@RequestMapping(value="/ping", method = RequestMethod.GET)
public void ping(HttpServletResponse response) throws IOException {
log.info("ping page is called");
IOUtils.write("Embedded Jetty Server is Up and Running", response.getOutputStream());
}
}
通過Jetty,我們可以很容易的在本地運行和調試Servlet,而生成好的Servlet我們可以直接發布到Tomcat. 這樣,我們可以簡化Servlet的開發和調試。托福改分