Excerpts from the article to http://blog.csdn.net/kongxx/article/details /7218787
When embedded and running Jetty, sometimes I want to start two ports, or provide multiple different services through a Jetty server, such as using 8080 is used to specify the default access port, 8433 is used to specify the https access port, etc. At this time, it can be solved by creating multiple Connectors.
1. First create a Server class, in which two Connector instances are created, one is bound to port 8080, and the other is bound to port 9090:
< p>
package com.google.code.garbagecan.jettystudy.sample2; import org.eclipse.jetty.server .Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; public class MyServer {public static void main(String[] args) throws Exception {Server server = new Server( ); SelectChannelConnector connector1 = new SelectChannelConnector(); connector1.setPort(8080); SelectChannelConnector connector2 = new SelectChannelConnector(); connector2.setPort(9090); server.setConnectors(new Connector[] {connector1, connector2 }); server.setHandler (new HelloHandler()); server.start(); server.join();}}
2. Create a Handler class to handle all client requests< /p>
Hello World
“); response.getWriter().println(“
“); response .getWriter().println(“
“);}}
3. Run the MyServer class and visit http://localhost:8080/ and http://localhost through the browser respectively :9090/.
The article is excerpted to http://blog.csdn.net/kongxx/article/details/7218787
< p>
When embedded and running Jetty, sometimes I want to start two ports, or provide multiple different services through a Jetty server, for example, use 8080 to specify the default access port, and use 8433 to specify https Access ports, etc., can be solved by creating multiple Connectors at this time.
1. First create a Server class, in which two Connector instances are created, one is bound to port 8080, and the other is bound to port 9090:
< p>
package com.google.code.garbagecan.jettystudy.sample2; import org.eclipse.jetty.server .Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.nio.SelectChannelConnector; public class MyServer {public static void main(String[] args) throws Exception {Server server = new Server( ); SelectChannelConnector connector1 = new SelectChannelConnector(); connector1.setPort(8080); SelectChannelConnector connector2 = new SelectChannelConnector(); connector2.setPort(9090); server.setConnectors(new Connector[] {connector1, connector2 }); server.setHandler (new HelloHandler()); server.start(); server.join();}}
2. Create a Handler class to handle all client requests< /p>
package com.google.code.garbagecan.jettystudy.sample2; import java.io.IOException; import javax.servlet.ServletException; import javax .servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; public class HelloHandler extends AbstractHandler {public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); response.getWriter().println("Hello World
"); response.getWriter().println("
3. Run the MyServer class and visit http://localhost:8080/ and http://localhost:9090 through the browser respectively /.