<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開獎
    主站蜘蛛池模板: 免费激情网站国产高清第一页| 中文字幕亚洲精品| 国产成人 亚洲欧洲| 白白国产永久免费视频| 中文字幕精品三区无码亚洲 | 亚洲日韩中文字幕| 成年人免费的视频| 亚洲香蕉久久一区二区| 好吊妞在线新免费视频| 无码一区二区三区亚洲人妻| 国产成人免费福利网站| 天堂亚洲免费视频| 国产V亚洲V天堂无码久久久| 色猫咪免费人成网站在线观看| 亚洲另类图片另类电影| 日韩免费观看的一级毛片| 美女被艹免费视频| 亚洲av午夜成人片精品网站| 91九色老熟女免费资源站| 亚洲最大福利视频| 亚洲高清国产拍精品青青草原| 久久精品无码专区免费| 亚洲永久永久永久永久永久精品| 五月婷婷综合免费| 青娱乐在线免费观看视频| 久久精品国产亚洲香蕉| 久久不见久久见中文字幕免费| 日韩免费在线中文字幕| 亚洲AV乱码久久精品蜜桃 | 又黄又大又爽免费视频| 国产精品视频白浆免费视频| 亚洲综合精品成人| 久久久久久亚洲精品不卡| www视频免费看| 一级一黄在线观看视频免费| 亚洲精品无码久久久久久久| 国产91久久久久久久免费| 99re在线精品视频免费| 美女18一级毛片免费看| 亚洲色四在线视频观看| 亚洲国产高清精品线久久|