Persistent connection between the client and the server

Source: Internet
Author: User
In recent projects, we need to use the comet-"persistent connection" technology. First, we need to study HTTP persistent connections, and then Apache httpclient. My goodness is that I used to directly pass through.
The later solution was socket.
Here we will first introduce the concept of persistent connections. From my understanding, it is a short connection that does not require the client to keep requests, That Ajax's "request-response-request-response, the persistent connection is
You only need to make a request once to establish a connection with the server. Once the server detects the response data or changes, it will be automatically sent to the client (either a handheld terminal or a PC ).
This is just my perspective. It is enough for me.
Here I will use the persistent connection of the C/S architecture, not the B/S architecture. A lot of things about the B/S architecture on the Internet have led me to deviate from the learning direction at the beginning.
Let me start todayUse of httpurlconnection,PrerequisitesYou must modify the following settings in Tomcat:

First, find the connector in the server. xml file. It is similar:

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>      

Use the following content to replace the HTTP/1.1 protocol:

<Connector connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
1. Simulate the client
This is to simulate a client sending a request to the web server. URL. Try {url = new URL ("http: // 8080/testcomet/weather"); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("Post"); Conn. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded ");Conn. setrequestproperty ("connection", "kepp-alive ");// The above code is the key. This is the setting for maintaining persistent connections, but it does not work. setinstancefollowredirects (true); Conn. setusecaches (false); Conn. setdooutput (true); Conn. setdoinput (true); dataoutputstream out = new dataoutputstream (Conn. getoutputstream (); string content = "username =" + urlencoder. encode ("test HTTP .............. "," UTF-8 "); out. writebytes (content); out. flush (); out. close (); system. out. println (Conn. getresponsecode ();} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}
2. Server Side
Implement through Servletorg.apache.catalina.CometProcessorInterface. This interface requires the implementation ofevent(). Callevent()Method to process the request, insteaddoGetOrDopost Method. Note:This interface can be found in Catalina. jar in Tomcat Lib, so it needs to be imported.
Public void event (final cometevent event) throws ioexception, servletexception {httpservletrequest request = event. gethttpservletrequest (); httpservletresponse response = event. gethttpservletresponse (); If (event. geteventtype () = cometevent. eventtype. begin) {system. out. println ("begin");} else if (event. geteventtype () = cometevent. eventtype. error) {system. out. println ("error"); event. close ();} else if (event. geteventtype () = cometevent. eventtype. end) {system. out. println ("end"); event. close ();} else if (event. geteventtype () = cometevent. eventtype. read) {system. out. println ("read"); // here, do not receive data throw new unsupportedoperationexception ("this servlet does not accept data ");}}
This method does not seem to work. httpurlconnection cannot simulate persistent connections, that is, "keep-alive" in the header does not respond. So
This solution didn't work out. I knew why I was wrong or why I didn't know why. Later I used httpclient.

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: 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.