Hello all,
I'm trying to use a java app that worked fine (and still does) with
Apache and the old tomcat (pre-coyote) http connectors against
tomcat 4.1.5.
My application essentially emulates a browser using HttpURLConnection
and caches the session id cookies returned by tomcat and use them to
maintain a 'sessioned' converation between the client application and
the server.
The problem I'm seeing is, that the subsequent invocations (i.e. the
ones that try to use the cached cookie text) all succeed (i.e. return
HTTP_OK) but are unable to do any input - i.e when I try to a read on
the input stream returned by getInputStream() method of the
HttpURLConnection, I get null returned.
Can anyone advise me if they've seen this behaviour before (and solved
it) . It appears to be a problem with either the back-end or the client
side recognizing that I"m done with the connection as it's almost as if
I"m getting back the 'old' connections InputStream reference.
A simplified example of what I'm doing (using the tomcat examples) is
attached at the end of this message.
Using apache as an in-between proxy and the old HTTP 1.1 and 1.0
connectors all work just with the example.
Can someone tell me what I might be doing wrong as I'm going nuts trying
to find out why this program won't work, and yet all the browsers I
point at Tomcat 4.1.5 seem to be just fine.
-Thom
-------- example program follows ------------------------
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.servlet.http.Cookie;
/**
*
* Simple Java program to demonstrate weird cookie behaviour of Coyote
Http Connector.
*
* Symptoms:
*
* When using the coyote connector with this example, all output from
the second invocation
* of the examples/servlet/SessionExample is not returned to the client.
Any attempt to read the
* BufferReader derived from the InputStream returns null.
*
* This program works correctly with both the 'old' HTTP 1.1 connector
and Apache to tomcat
* (via mod_jk).
*
*/
public class TestUrlConn {
// change host and port numbers for your server
private static final String TEST_URL =
"http://myhost:8080/examples/servlet/SessionExample" ;
private URL _url = null;
private HttpURLConnection _conn = null;
private Cookie _sessionCookie = null;
// this method does the lot, connects to the URL, executes send/read
operation
// closes the connection.
public void connectAndRead( String url ) {
InputStreamReader in = null;
BufferedReader br = null;
String buffer = null;
try {
// create URL from url string
_url = new URL(url);
// get url connection 'handle'
_conn = (HttpURLConnection)_url.openConnection();
// set conn attributes
_conn.setRequestMethod("GET");
_conn.setDoInput(true);
_conn.setDoOutput(false);
_conn.setDefaultUseCaches(false);
// if there's a session cookie cached, associate it's value with
this web
// connection in a Cookie header
if ( null != _sessionCookie ) {
_conn.setRequestProperty("Cookie",_sessionCookie.getName() + "="
+ _sessionCookie.getValue() );
}
// connect to url (this could be done implicitly by calling one of the
// 'response' methods)
_conn.connect();
// get the session cookie from the "Set-Cookie" header
Cookie sessionCookie = null;
sessionCookie = getSessionCookie( _conn.getHeaderField(
"Set-Cookie" ) );
// save cookie in instance member
if ( null != sessionCookie ) {
_sessionCookie = sessionCookie;
}
// read any stuff from the server
in = new InputStreamReader(_conn.getInputStream());
br = new BufferedReader( in ) ;
for ( int i=0; ; i++ ) {
buffer = br.readLine();
if ( buffer == null ) {
break;
} else {
System.out.println( buffer );
}
}
// close (at least try to) underlying input stream
br.close();
// disconnect (hah!) from web-server
_conn.disconnect();
} catch ( MalformedURLException e ) {
System.err.println("Bad URL syntax: " + url );
} catch ( Exception e ) {
System.err.println( "Exception thrown!" ) ;
e.printStackTrace( System.err );
}
}
// create a cookie from the string returned with the "Set-Cookie" header
protected Cookie getSessionCookie( String sessionCookieText ) {
Cookie sessionCookie = null;
String cookieValue = null;
String cookieName = null;
if ( null != sessionCookieText ) {
int equalPos = sessionCookieText.indexOf("=");
if ( equalPos > 0 ) {
cookieName = sessionCookieText.substring(0,equalPos);
if ( cookieName.compareToIgnoreCase("jsessionid") == 0 ) {
// matched - string following "=" is our session id
cookieValue = sessionCookieText.substring(equalPos+1);
sessionCookie = new Cookie(cookieName, cookieValue);
}
}
}
return sessionCookie;
}
public TestUrlConn() {
}
public static void main(String[] args) {
TestUrlConn tuc = new TestUrlConn();
System.out.println( "First call:" );
tuc.connectAndRead(TestUrlConn.TEST_URL);
System.out.println("");
System.out.println( "Second Call:" );
tuc.connectAndRead(TestUrlConn.TEST_URL);
System.out.println("");
}
}
--------------- end example program
------------------------------------------
--
"My friend, you would not tell with such high zest
To children ardent for some desperate glory,
The old Lie: Dulce et decorum est
Pro patria mori." -- Wilfred Owen (1893-1918)
http://www.etsu.edu/english/muse/owen.htm
--
Borland -- Enabling a new digital world where our customers
have the freedom of choice to develop, deploy, and integrate
applications across the enterprise and the Internet.
http://www.borland.com
This e-mail, and any attachments thereto, is intended only
for use by the addressee(s) named herein and may contain
legally privileged and/or confidential information. If you
are not the intended recipient of this e-mail, you are hereby
notified that any dissemination, distribution or copying of
this e-mail, and any attachments thereto, is strictly prohibited.
If you have received this e-mail in error, please immediately
and permanently delete the original and any copy of any e-mail
and any printout thereof.
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
