Jetty實戰之 嵌入式Jetty運行Servlet

來源:互聯網
上載者:User

在嵌入式Jetty中,有時候我們想運行一些的Servlet,此時就需要建立建立Context,然後讓自己的Servlet運行在這些ServletContext中。

1. 首先建立一個ServletContextServer類,用來初始化web應用程式的Context,並且指定Servlet和Servlet匹配的url。這裡指定了兩個Servlet,分別是HelloServlet和GoodbyeServlet,並分別對應/hello/*和/goodbye/*。
[java] view plain copy print ? package com.google.code.garbagecan.jettystudy.sample5;      import org.eclipse.jetty.server.Server;   import org.eclipse.jetty.servlet.ServletContextHandler;   import org.eclipse.jetty.servlet.ServletHolder;      public class ServletContextServer {       public static void main(String[] args) throws Exception {           Server server = new Server(8080);              ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);           context.setContextPath("/");           server.setHandler(context);              // http://localhost:8080/hello           context.addServlet(new ServletHolder(new HelloServlet()), "/hello");           // http://localhost:8080/hello/kongxx           context.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/hello/kongxx");              // http://localhost:8080/goodbye           context.addServlet(new ServletHolder(new GoodbyeServlet()), "/goodbye");           // http://localhost:8080/goodbye/kongxx           context.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/goodbye/kongxx");                      server.start();           server.join();       }   }   2. 兩個簡單的Servlet:HelloServlet和GoodbyeServlet:
[java] view plain copy print ? package com.google.code.garbagecan.jettystudy.sample5;      import java.io.IOException;   import javax.servlet.ServletException;   import javax.servlet.http.HttpServlet;   import javax.servlet.http.HttpServletRequest;   import javax.servlet.http.HttpServletResponse;      public class HelloServlet extends HttpServlet {       private static final long serialVersionUID = 1L;       private String msg = "Hello World!";          public HelloServlet() {       }          public HelloServlet(String msg) {           this.msg = msg;       }          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           response.setContentType("text/html");           response.setStatus(HttpServletResponse.SC_OK);           response.getWriter().println("<h1>" + msg + "</h1>");           response.getWriter().println("session=" + request.getSession(true).getId());       }   }      package com.google.code.garbagecan.jettystudy.sample5;      import java.io.IOException;   import javax.servlet.ServletException;   import javax.servlet.http.HttpServlet;   import javax.servlet.http.HttpServletRequest;   import javax.servlet.http.HttpServletResponse;      public class GoodbyeServlet extends HttpServlet {       private static final long serialVersionUID = 1L;       private String msg = "Goodbye!";          public GoodbyeServlet() {       }          public GoodbyeServlet(String msg) {           this.msg = msg;       }          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           response.setContentType("text/html");           response.setStatus(HttpServletResponse.SC_OK);           response.getWriter().println("<h1>" + msg + "</h1>");           response.getWriter().println("session=" + request.getSession(true).getId());       }   }   3. 運行ServletContextServer類,然後分別訪問以下四個url

  http://localhost:8080/hello
  http://localhost:8080/hello/kongxx
  http://localhost:8080/goodbye
  http://localhost:8080/goodbye/kongxx


4. 除了上面的方式外,也可以建立兩個個Context,分別綁定到"/hello"和"/goodbye",如下: [java] view plain copy print ? package com.google.code.garbagecan.jettystudy.sample5;      import org.eclipse.jetty.server.Handler;   import org.eclipse.jetty.server.Server;   import org.eclipse.jetty.server.handler.ContextHandlerCollection;   import org.eclipse.jetty.servlet.ServletContextHandler;   import org.eclipse.jetty.servlet.ServletHolder;      public class MultiContextServer {       public static void main(String[] args) throws Exception {           Server server = new Server(8080);              // http://localhost:8080/hello/kongxx           ServletContextHandler context1 = new ServletContextHandler(ServletContextHandler.SESSIONS);           context1.setContextPath("/hello");           context1.setResourceBase(".");           context1.setClassLoader(Thread.currentThread().getContextClassLoader());           context1.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/kongxx");              // http://localhost:8080/goodbye/kongxx           ServletContextHandler context2 = new ServletContextHandler(ServletContextHandler.SESSIONS);           context2.setContextPath("/goodbye");           context2.setResourceBase(".");           context2.setClassLoader(Thread.currentThread().getContextClassLoader());           context2.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/kongxx");              ContextHandlerCollection contexts = new ContextHandlerCollection();           contexts.setHandlers(new Handler[] { context1, context2 });              server.setHandler(contexts);              server.start();           server.join();       }   } 
來自: http://blog.csdn.net/kongxx/article/details/7230080

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.