<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 好不容易 閱讀(259) 評論(0)  編輯  收藏


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


    網站導航:
     
    PK10開獎 PK10開獎
    主站蜘蛛池模板: 亚洲精品色婷婷在线影院| 韩国免费一级成人毛片| 亚洲第一永久AV网站久久精品男人的天堂AV| 亚洲国产成人久久精品影视 | 亚洲精品动漫人成3d在线| 亚洲精品无码你懂的| 成年女人视频网站免费m| 亚洲一级毛片免观看| 久久不见久久见免费影院| 亚洲精品国产综合久久久久紧| 女性无套免费网站在线看| 久久亚洲精品无码网站| 亚洲国产高清精品线久久| 三级网站免费观看| 亚洲人成电影在线天堂| 亚洲一级免费毛片| 亚洲国产AV无码一区二区三区| 国产裸模视频免费区无码| 特级做a爰片毛片免费看| 亚洲线精品一区二区三区| 久久aⅴ免费观看| 亚洲成人动漫在线观看| 男女啪啪永久免费观看网站| 日韩亚洲综合精品国产| 在线观看国产区亚洲一区成人| 99久久国产精品免费一区二区| 亚洲白色白色在线播放| 四虎成人免费网站在线| 无码日韩人妻AV一区免费l| 久久噜噜噜久久亚洲va久| 免费大片黄在线观看yw| 男人j进女人p免费视频| 亚洲专区先锋影音| 国产精品国产午夜免费福利看| 黄视频在线观看免费| 亚洲网站视频在线观看| 国产免费怕怕免费视频观看| 中国videos性高清免费| 亚洲人6666成人观看| 亚洲国产精品尤物YW在线观看| 中文字幕免费观看|