Background
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.
Understanding
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 java.net. 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"/> |
Details
1. Simulate the client
This is to simulate a client sending a request to the web server. URL. Try {url = new URL ("http: // 127.0.0.1: 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.CometProcessor
Interface. This interface requires the implementation ofevent()
. Callevent()
Method to process the request, insteaddoGet
OrDopost 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.