Jetty actual combat embedded jetty run the servlet

Source: Internet
Author: User
Tags sessions

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 (); }
}


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.