Jetty Embedded Jetty running servlet

Source: Internet
Author: User
Tags join sessions

In embedded jetty, sometimes we want to run some servlets, we need to create a context, and then let our servlet run in these servletcontext.

1. First create a Servletcontextserver class that initializes the context of the Web application and specifies the URL that the servlet and servlet match. Two servlets are specified here, HelloServlet and Goodbyeservlet, respectively, and corresponding to/hello/* and/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);      &Nbsp;       // 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&Nbsp;kongxx! ")), "/goodbye/kongxx ");                       server.start ();            server.join ();       }  }   2. Two simple servlet:helloservlet and 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;&NBsp;      }          protected void  Doget (Httpservletrequest request, httpservletresponse response)  throws  servletexception, ioexception {            Response.setcontenttype ("text/html");            Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);            Response.getwriter (). println ("[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 ();       }  } 
From: http://blog.csdn.net/kongxx/article/details/7230080

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.