通过Jetty搭建一个简单的Servlet运行环境

来源:互联网 发布:淘宝app怎么换皮肤 编辑:程序博客网 时间:2024/06/06 06:29

最近在做一些简单的Servlet开发的时候,感觉每次调试的时候都要发布到tomcat上很麻烦,把程序共享给同事也很麻烦,需要帮他设置本地的tomcat环境. 在网上找了找其他的Servlet运行环境,发现用Jetty可以很方便的实现嵌入式Web container.这里我记录一下通过Jetty搭建简单Servlet运行环境的过程,希望对有同样需要的朋友有所帮助.

整个环境的代码可以在https://github.com/mcai4gl2/jettysetup找到. 代码包括了IntelliJ的项目文件,如果需要eclipse项目文件,请在下载代码后运行 mvn eclipse:eclipse 来生成eclipse项目文件. (当然, 请在本地安装Maven).

  • 设置Maven Dependency:
    [plain] view plaincopy
    1. <dependencies>  
    2.         <!-- jetty -->  
    3.         <dependency>  
    4.             <groupId>org.eclipse.jetty</groupId>  
    5.             <artifactId>jetty-server</artifactId>  
    6.             <version>${jetty.version}</version>  
    7.         </dependency>  
    8.         <dependency>  
    9.             <groupId>org.eclipse.jetty</groupId>  
    10.             <artifactId>jetty-servlet</artifactId>  
    11.             <version>${jetty.version}</version>  
    12.         </dependency>  
    13.         <!-- spring -->  
    14.         <dependency>  
    15.             <groupId>org.springframework</groupId>  
    16.             <artifactId>spring-webmvc</artifactId>  
    17.             <version>${spring.version}</version>  
    18.         </dependency>  
    19.         <!-- log4j -->  
    20.         <dependency>  
    21.             <groupId>log4j</groupId>  
    22.             <artifactId>log4j</artifactId>  
    23.             <version>1.2.17</version>  
    24.         </dependency>  
    25.         <!-- utils -->  
    26.         <dependency>  
    27.             <groupId>org.apache.commons</groupId>  
    28.             <artifactId>commons-io</artifactId>  
    29.             <version>1.3.2</version>  
    30.         </dependency>  
    31.     </dependencies>  

  • 设置servlet-context.xml:
    [html] view plaincopy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans:beans xmlns="http://www.springframework.org/schema/mvc"  
    3.              xmlns:beans="http://www.springframework.org/schema/beans"  
    4.              xmlns:context="http://www.springframework.org/schema/context"  
    5.              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    6.              xsi:schemaLocation="  
    7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
    8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
    10.   
    11.     <interceptors>  
    12.         <interceptor>  
    13.             <mapping path="/*"/>  
    14.             <beans:bean class="weblog.examples.jettysetup.LoggingInterceptor"/>  
    15.         </interceptor>  
    16.     </interceptors>  
    17.   
    18.     <context:annotation-config/>  
    19.   
    20.     <context:component-scan base-package="weblog.examples.jettysetup.serlvet"/>  
    21. </beans:beans>  
  • 一个简单的Main Class:
    [java] view plaincopy
    1. public static void main(String[] args) throws Exception {  
    2.         try {  
    3.             DOMConfigurator.configure(Thread.currentThread().getContextClassLoader().getResource("log4j.xml"));  
    4.   
    5.             Server server = new Server();  
    6.   
    7.             SelectChannelConnector connector = new SelectChannelConnector();  
    8.             connector.setPort(7411);  
    9.             server.setConnectors(new Connector[] {connector});  
    10.   
    11.             DispatcherServlet servlet = new DispatcherServlet();  
    12.             servlet.setContextConfigLocation("classpath:servlet-context.xml");  
    13.   
    14.             ServletContextHandler context = new ServletContextHandler();  
    15.             context.setContextPath("/");  
    16.             context.addServlet(new ServletHolder("baseServlet", servlet), "/");  
    17.   
    18.             HandlerCollection handlers = new HandlerCollection();  
    19.             handlers.setHandlers(new Handler[] { context, new DefaultHandler()});  
    20.             server.setHandler(handlers);  
    21.   
    22.             XmlWebApplicationContext wctx = new XmlWebApplicationContext();  
    23.             wctx.setConfigLocation("");  
    24.             wctx.setServletContext(servlet.getServletContext());  
    25.             wctx.refresh();  
    26.   
    27.             context.setAttribute(XmlWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wctx);  
    28.             server.start();  
    29.   
    30.             log.info("Jetty embedded server started");  
    31.   
    32.             log.info("Press any key to stop");  
    33.             System.in.read();  
    34.             log.info("Stopping Server");  
    35.             server.stop();  
    36.             log.info("Server stopped");  
    37.   
    38.         } catch (Exception ex) {  
    39.             log.error("Failed to run Jetty Server", ex);  
    40.             throw ex;  
    41.         }  
    42.     }  

在JettyLauncher运行后,我们可以访问http://localhost:7411/ping来查看Jetty是否成功运行. http://localhost:7411/ping将运行以下Spring MVC Servlet:

[java] view plaincopy
  1. @Controller  
  2. public class TestServlet {  
  3.   
  4.     private static Logger log = Logger.getLogger(TestServlet.class);  
  5.   
  6.     @RequestMapping(value="/ping", method = RequestMethod.GET)  
  7.     public void ping(HttpServletResponse response) throws IOException {  
  8.         log.info("ping page is called");  
  9.         IOUtils.write("Embedded Jetty Server is Up and Running", response.getOutputStream());  
  10.     }  
  11. }  

通过Jetty,我们可以很容易的在本地运行和调试Servlet,而生成好的Servlet我们可以直接发布到Tomcat. 这样,我们可以简化Servlet的开发和调试.

0 0