Jetty practice-embedded running jetty multi-connector

Source: Internet
Author: User

Address: http://blog.csdn.net/kongxx/article/details/7218787

When embedding and running jetty, you sometimes want to start two ports or use one jetty server to provide multiple different services. For example, you can use 8080 to specify the default access port, you can use 8433 to specify the HTTPS access port. In this case, you can create multiple ctor.

1. First, create a server class with two ctor instances, one bound to port 8080 and the other bound to port 9090:

<textarea readonly name="code" class="java">Package COM. google. code. garbagecan. jettystudy. sample2; </P> <p> Import Org. eclipse. jetty. server. connector; <br/> Import Org. eclipse. jetty. server. server; <br/> Import Org. eclipse. jetty. server. NIO. selectchannelconnector; </P> <p> public class myserver {<br/> Public static void main (string [] ARGs) throws exception {<br/> server Server = new server (); </P> <p> selectchannelconnector oror1 = new selectchannelconnector (); <br/> connector1.setport (8080 ); </P> <p> selectchannelconnector connector2 = new selectchannelconnector (); <br/> connector2.setport (9090); </P> <p> server. setconnectors (new connector [] {connector1, connector2}); </P> <p> server. sethandler (New hellohandler (); </P> <p> server. start (); <br/> server. join (); <br/>}< br/>}</textarea> 2. Create a handler class and process all client requests
Package COM. google. code. garbagecan. jettystudy. sample2; </P> <p> Import Java. io. ioexception; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Import Org. eclipse. jetty. server. request; <br/> Import Org. eclipse. jetty. server. handler. abstracthandler; </P> <p> public class hellohandler extends acthandler {<br/> Public void handle (string target, request baserequest, httpservletrequest request, httpservletresponse response) <br/> throws ioexception, servletexception {<br/> response. setcontenttype ("text/html; charset = UTF-8"); <br/> response. setstatus (httpservletresponse. SC _ OK); <br/> baserequest. sethandled (true); <br/> response. getwriter (). println ("<p> Hello World </p>"); <br/> response. getwriter (). println ("<li> request URL:" + target + "</LI>"); <br/> response. getwriter (). println ("<li> server port:" + request. getserverport () + "</LI>"); <br/>}< br/>}3. Run the myserver class and access http: // localhost: 8080/and http: // localhost: 9090/in the browser /.

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.