中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

          Java專(zhuān)題文章博客和開(kāi)源

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          通過(guò)Jetty搭建一個(gè)簡(jiǎn)單的Servlet運(yùn)行環(huán)境

            最近在做一些簡(jiǎn)單的Servlet開(kāi)發(fā)的時(shí)候,感覺(jué)每次調(diào)試的時(shí)候都要發(fā)布到tomcat上很麻煩,把程序共享給同事也很麻煩,需要幫他設(shè)置本地的tomcat環(huán)境。 在網(wǎng)上找了找其他的Servlet運(yùn)行環(huán)境,發(fā)現(xiàn)用Jetty可以很方便的實(shí)現(xiàn)嵌入式Web container.這里我記錄一下通過(guò)Jetty搭建簡(jiǎn)單Servlet運(yùn)行環(huán)境的過(guò)程,希望對(duì)有同樣需要的朋友有所幫助。托福答案

            整個(gè)環(huán)境的代碼可以找到。 代碼包括了IntelliJ的項(xiàng)目文件,如果需要eclipse項(xiàng)目文件,請(qǐng)?jiān)谙螺d代碼后運(yùn)行 mvn eclipse:eclipse 來(lái)生成eclipse項(xiàng)目文件。 (當(dāng)然, 請(qǐng)?jiān)诒镜匕惭bMaven)。

            設(shè)置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>

            設(shè)置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>

            一個(gè)簡(jiǎn)單的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運(yùn)行后,我們可以訪問(wèn)localhost:7411/ping來(lái)查看Jetty是否成功運(yùn)行。 localhost:7411/ping將運(yùn)行以下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());

            }

            }

            通過(guò)Jetty,我們可以很容易的在本地運(yùn)行和調(diào)試Servlet,而生成好的Servlet我們可以直接發(fā)布到Tomcat. 這樣,我們可以簡(jiǎn)化Servlet的開(kāi)發(fā)和調(diào)試。托福改分
           

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


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          PK10開(kāi)獎(jiǎng) PK10開(kāi)獎(jiǎng)
          主站蜘蛛池模板: 三明市| 棋牌| 读书| 阿拉尔市| 余庆县| 大石桥市| 买车| 黑龙江省| 邛崃市| 曲麻莱县| 三原县| 台安县| 大化| 建平县| 稷山县| 阜平县| 墨脱县| 遂平县| 莱阳市| 平泉县| 会东县| 大田县| 清苑县| 伊宁县| 金溪县| 湟源县| 明光市| 思茅市| 东阿县| 呼伦贝尔市| 鄱阳县| 南溪县| 冀州市| 安溪县| 射洪县| 丽江市| 泽普县| 佛学| 杂多县| 个旧市| 江油市|