转自:http://blog.csdn.net/kongxx/article/details/7218787
在嵌入运行Jetty的时候,有时候想要启动两个端口,或者通过一个Jetty server提供多个不同服务,比如说使用8080来指定默认访问端口,使用8433指定https访问端口等等,此时就可以通过创建多个Connector来解决。
1. 首先创建一个Server类,其中创建了两个Connector实例,一个绑定到了8080端口,另一个绑定到了9090端口:
- 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();
- }
- }
- 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("<h1>Hello World</h1>");
- response.getWriter().println("<li>Request url: " + target + "</li>");
- response.getWriter().println("<li>Server port: " + request.getServerPort() + "</li>");
- }
- }