Jetty Multi-connector

Source: Internet
Author: User

Sometimes you want to start two ports, or you can provide several different services through a jetty server, such as using 8080来 to specify the default access port, using 8433 to specify the HTTPS access port, and so on, it is possible to create multiple connector to resolve this.

1. First create a server class, where two connector instances are created, one is bound to 8080 ports, and the other is bound to 9090 ports:

[Java]View Plaincopy
  1. Package com.google.code.garbagecan.jettystudy.sample2;
  2. Import Org.eclipse.jetty.server.Connector;
  3. Import Org.eclipse.jetty.server.Server;
  4. Import Org.eclipse.jetty.server.nio.SelectChannelConnector;
  5. public class MyServer {
  6. public static void Main (string[] args) throws Exception {
  7. Server server = new server ();
  8. Selectchannelconnector Connector1 = new Selectchannelconnector ();
  9. Connector1.setport (8080);
  10. Selectchannelconnector Connector2 = new Selectchannelconnector ();
  11. Connector2.setport (9090);
  12. Server.setconnectors (new connector[] {connector1, connector2});
  13. Server.sethandler (New Hellohandler ());
  14. Server.start ();
  15. Server.join ();
  16. }
  17. }

2. Create a handler class that uses requests to process all clients

[Java]View Plaincopy
  1. Package com.google.code.garbagecan.jettystudy.sample2;
  2. Import java.io.IOException;
  3. Import javax.servlet.ServletException;
  4. Import Javax.servlet.http.HttpServletRequest;
  5. Import Javax.servlet.http.HttpServletResponse;
  6. Import Org.eclipse.jetty.server.Request;
  7. Import Org.eclipse.jetty.server.handler.AbstractHandler;
  8. public class Hellohandler extends Abstracthandler {
  9. public void handle (String target, request baserequest, HttpServletRequest request, httpservletresponse response)
  10. Throws IOException, Servletexception {
  11. Response.setcontenttype ("Text/html;charset=utf-8");
  12. Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);
  13. Baserequest.sethandled (TRUE);
  14. Response.getwriter (). println ("
  15. Response.getwriter (). println ("<li>request URL:" + target + "</li>");
  16. Response.getwriter (). println ("<li>server port:" + request.getserverport () + "</li>");
  17. }
  18. }

3. Run the MyServer class to access http://localhost:8080/and http://localhost:9090/, respectively, through the browser.

Related: How Jetty works and how it compares to Tomcat

Jetty Multi-connector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.