有时候想要启动两个端口,或者通过一个Jetty server提供多个不同服务,比如说使用8080来指定默认访问端口,使用8433指定https访问端口等等,此时就可以通过创建多个Connector来解决。
1. 首先创建一个Server类,其中创建了两个Connector实例,一个绑定到了8080端口,另一个绑定到了9090端口:
[java] view plaincopy
- 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. 创建一个Handler类,用了处理所有客户端的请求
[java] view plaincopy
- 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>");
- }
- }
3. 运行MyServer类,通过浏览器分别访问http://localhost:8080/和http://localhost:9090/。
public Connector[] getConnectors()