Jetty actual embedded run Jetty multi Connector.

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> 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(“

  • Request url: “+ target + “
  • “); response .getWriter().println(“

  • Server port: “+ request.getServerPort() + “
  • “);}}

    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("
  • Request url: "+ target + "
  • "); response. getWriter().println("
  • Server port: "+ req uest.getServerPort() + "
  • ");}}

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

    WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
    SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4560 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

    Leave a Comment

    Your email address will not be published.