Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.eclipse.jetty.server.Server;ImportOrg.eclipse.jetty.servlet.ServletContextHandler;ImportOrg.eclipse.jetty.servlet.ServletHolder; Public classEmbeddingjettywithservlet { Public Static voidMain (string[] args)throwsException {Server server=NewServer (8080); Servletcontexthandler Context=NewServletcontexthandler (servletcontexthandler.sessions); Context.setcontextpath ("/hello"); Server.sethandler (context); Context.addservlet (NewServletholder (NewHelloServlet ()), "/*"); Server.start (); } Public Static classHelloServletextendsHttpServlet {Private Static Final LongSerialversionuid = -6154475799000019575l; Private Static FinalString greeting = "Hello World"; protected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); Response.setstatus (HTTPSERVLETRESPONSE.SC_OK); Response.getwriter (). println (greeting); } }}
Visit url:http://myhost:8080/hello/
Output: Hello World
The above program needs to import Servlet-api.jar and Jetty-all.jar,jetty can be referenced by: http://www.eclipse.org/jetty/[][]
Servlet uses Org.eclipse.jetty to implement small servlet servers