<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    中文JAVA技術平等自由協作創造

    Java專題文章博客和開源

    常用鏈接

    統計

    最新評論

    通過Jetty搭建一個簡單的Servlet運行環境

      最近在做一些簡單的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的開發和調試。托福改分
     

    posted on 2013-10-12 17:31 好不容易 閱讀(264) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 精品国产_亚洲人成在线| 亚洲伦理一二三四| 一本久久免费视频| 伊人久久亚洲综合影院| 国产精品亚洲综合天堂夜夜| 色www永久免费视频| 免费高清国产视频| 亚洲国产一二三精品无码| 成人久久免费网站| 久久综合图区亚洲综合图区| 亚洲av专区无码观看精品天堂| 91免费国产精品| 精品国产日韩久久亚洲| 永久免费视频v片www| 男性gay黄免费网站| 亚洲女同成人AⅤ人片在线观看| 久久久免费观成人影院| 亚洲成色在线综合网站| 亚洲毛片免费视频| 亚洲码欧美码一区二区三区| 亚洲av麻豆aⅴ无码电影| 中文字幕乱码系列免费| 亚洲精品日韩中文字幕久久久| 丁香花免费完整高清观看| 久久久久亚洲AV无码去区首| 亚洲情a成黄在线观看| 国内精品久久久久影院免费| 亚洲国产综合在线| 四虎永久精品免费观看| 黄色视频在线免费观看| 亚洲欧洲国产成人精品| 国产免费观看网站| 少妇性饥渴无码A区免费| 亚洲国产成人久久| 亚洲?V无码成人精品区日韩| 久久免费精彩视频| 亚洲AV无码男人的天堂| 亚洲AV一宅男色影视| 成人免费a级毛片无码网站入口| 一级毛片成人免费看a| 免费一级毛片女人图片|