I tried several versions of jetty, with some differences in the use of classes, in this record jettyversion = 9.0.2.v20130417 Partial instances
Maven Dependencies and configuration:
<Properties> <!--Adapt this to a version found on http://central.maven.org/maven2/org/eclipse/jetty/jetty-maven-plugin/ - <jettyversion>9.0.2.v20130417</jettyversion> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </Properties> <Dependencies> <!--Jetty Dependency - <Dependency> <groupId>Org.eclipse.jetty</groupId> <Artifactid>Jetty-server</Artifactid> <version>${jettyversion}</version> </Dependency> </Dependencies> <Build> <Plugins> <plugin> <groupId>Org.codehaus.mojo</groupId> <Artifactid>Exec-maven-plugin</Artifactid> <version>1.1</version> <executions> <Execution> <Goals> <goal>Java</goal> </Goals> </Execution> </executions> <Configuration> <MainClass>Org.example.HelloWorld</MainClass> </Configuration> </plugin> </Plugins> </Build>
The following is the sample code:
PackageCom.bocom.testjetty.httpserver;ImportOrg.eclipse.jetty.server.Connector;ImportOrg.eclipse.jetty.server.Handler;ImportOrg.eclipse.jetty.server.Server;ImportOrg.eclipse.jetty.server.ServerConnector;ImportOrg.eclipse.jetty.server.handler.ContextHandler;Importorg.eclipse.jetty.server.handler.HandlerList; Public class httpserver { Public Static voidMain (string[] arg0)throwsexception{Server1 (); Server2 (); } /*** Multiple connector instances *@throwsException*/ Public Static voidServer1 ()throwsexception{Server server=NewServer (); Serverconnector Connector1=Newserverconnector (server); Connector1.setport (8082); Serverconnector Connector2=Newserverconnector (server); Connector2.setport (9001); Server.setconnectors (Newconnector[] {connector1, connector2}); Server.sethandler (NewHellohandler ()); Server.start (); } /*** Multiple Handler instances *@throwsException*/ Public Static voidServer2 ()throwsException {Server server=NewServer (9080); Contexthandler Context1=NewContexthandler (); Context1.setcontextpath ("/hello"); Context1.sethandler (NewHellohandler ()); Contexthandler context2=NewContexthandler (); Context2.setcontextpath ("/content"); Context2.sethandler (NewHellohandler ()); Handlerlist handlers=Newhandlerlist (); Handlers.sethandlers (Newhandler[]{context1,context2}); Server.sethandler (handlers); Server.start (); Server.join (); } } PackageCom.bocom.testjetty.httpserver;Importjava.io.IOException;Importjavax.servlet.ServletException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importorg.eclipse.jetty.server.Request;ImportOrg.eclipse.jetty.server.handler.AbstractHandler; Public class Hellohandler extendsabstracthandler{ Public voidhandle (String target, request baserequest, HttpServletRequest request, httpservletresponse response)throwsIOException, servletexception {response.setcontenttype ("Text/html;charset=utf-8"); Response.setstatus (HTTPSERVLETRESPONSE.SC_OK); //Response status Code baserequest.sethandled (true); Response.getwriter (). println ("<a> Hello!" </a> "); }}
Resources:
Http://www.eclipse.org/jetty/documentation/current/index.html
Http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
Http://blog.sina.com.cn/s/articlelist_2664952023_14_1.html
Jetty Embedded Development (example)