This article link: http://blog.csdn.net/kongxx/article/details/7230080
In embedded jetty, sometimes we want to run some servlet, and then we need to create a context and then have our servlet run in these servletcontext.
1. First create a Servletcontextserver class to initialize the context of the Web application and specify the URL that the servlet and servlet match. There are two servlet designations, respectively HelloServlet and Goodbyeservlet, and corresponding to/hello/* and/goodbye/* respectively.
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 Serve
R (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 ("Hello kongxx!")), "/
Hello/kongxx ");
Http://localhost:8080/goodbye Context.addservlet (New Servletholder (New Goodbyeservlet ()), "/goodbye"); Http://localhost:8080/goodbye/kongxx Context.addservlet (New Servletholder ("Goodbye kongxx!")
, "/goodbye/kongxx");
Server.start ();Server.join (); }
}
2. Two simple Servlet:helloservlet and Goodbyeservlet:
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 ("
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 ("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 ("Goodbye kongxx!"), "/kongxx");
contexthandlercollection contexts = new contexthandlercollection ();
Contexts.sethandlers (new handler[] {context1, context2});
Server.sethandler (contexts);
Server.start ();
Server.join (); }
}