How can I implement HTTP keep-alive on the connection between my applet and servlet?
Created May 4, 2012
Rajah Kalipatnapu
You can use public void setRequestProperty(String key, String value) method on URLConnection.
Description of the method from Java API:
Sets the general request property.
Parameters:
key - the keyword by which the request is known (e.g., "accept").
value - the value associated with it.
Example:
This example uses the above method to set "connection" property to "Keep-Alive" and invokes the so called "RequestHeaderExample" servlet and prints the response. part of the output is given after the code.
While the example is an application, the same works in an applet.
public class TestConnectionKeepAlive { public static void main(String[] args) { try { java.net.URL url = new java.net.URL ("http://my.host.com/examples/servlet/Example"); java.net.URLConnection uc = url.openConnection(); uc.setRequestProperty("connection","Keep-Alive"); uc.connect(); java.io.BufferedReader br = new java.io.BufferedReader( new java.io.InputStreamReader(uc.getInputStream())); String str = null; while( true ) { str = br.readLine(); if( str == null) break; System.out.println(str); } }catch(Exception e) { e.printStackTrace(); } } }Output:
accept | text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 |
host | my.host.com |
connection | Keep-Alive |
user-agent | Java1.3.0rc3 |