- Original Message -
From: Endre Stølsvik [EMAIL PROTECTED]
To: Remy Maucherat [EMAIL PROTECTED]; Tomcat Users List
[EMAIL PROTECTED]
Sent: Tuesday, November 13, 2001 5:01 PM
Subject: Re: [TC4] Sending redirect fails if OutputStream isn't closed ..
On Mon, 12 Nov 2001, Remy Maucherat wrote:
| I'm using this program called loadsim to get a simulation of a bunch
of
| users using my webapp. This is a java program utilizing the
| java.net.HttpConnection to simulate the clients, built using large
parts
| of a apache loadsimulation system, right?
Make that java.net.HttpURLConnection (as the client).
| Why is that? Why do I have to get the outputstream, and then just
close it
| to get the sendRedirect away? Why isn't the stream closed on exit of
the
| doGet/Post method? Or whatever?
|
| The default servlet doesn't use the output stream at all, and appears to
| behave correctly (verified using a telnet session). This looks like a
| problem with the HTTP client you're using.
I also mentioned this in the first post (Mozilla behaves properly), but
why does it then work when I get and close the output stream as described?
Here's the code from loadsim that does the sampling, just if you don't
have much other to do today! (It's not that long, actually)..
It seems to close both its output to the server, and the input from the
server..
Extra-info: that page which sends the redirects is just a poster-page;
this is where the browser does GETs with parameters and POSTs: it just
takes all the parameters from the browser, and then sends a blank
redirect to the browser. This fools the (most) browsers to not store this
as a separate history event, and you don't get that reposting stuff on
back and reload going.
--
/**
* Send the link to the webserver using the codeVirtualUser/code's
* codeCookieManager/code.
*
* @param cookieManager The codeVirtualUser/code's
codeCookieManager/code.
* @param logThe log file to log information/errors to.
*
* @return The codeSampleResult/code object.
*
* @exception IOException if an error occurs reading from or writing
to the
*webserver.
*/
public SampleResult sample(CookieManager cookieManager, ILog log)
throws IOException {
long then;
URL url = null;
HttpURLConnection connection = null;
BufferedInputStream input = null;
SampleResult sample = null;
byte [] buf = null;
int bytesread = 0;
ByteArrayOutputStream outbuf = null;
// Init the sample result
sample = new SampleResult();
sample.setLabel(id_);
// Create the HTTP connection
then = System.currentTimeMillis();
url = new URL(linkStr_);
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false); // ***
[EMAIL PROTECTED]: if followredirects is true we miss out on the cookies
set by the first page!
// Setup cookies
if (cookieManager != null) {
String cookieHeader =
cookieManager.getCookieHeaderForURL(url);
if (cookieHeader != null) {
connection.setRequestProperty(Cookie, cookieHeader);
}
}
if (formDataStr_ == null) {
connection.connect();
sample.setTTOpen(System.currentTimeMillis() - then);
log.log(ILog.INFORMATION, ' url = + linkStr_);
} else {
try {
DataOutputStream output = null;
connection.setRequestProperty(Content-Type,
application/x-www-form-urlencoded);
connection.setDoOutput(true);
connection.setRequestMethod(POST);
connection.connect();
sample.setTTOpen(System.currentTimeMillis() - then);
output = new
DataOutputStream(connection.getOutputStream());
output.writeBytes(formDataStr_);
output.flush();
output.close();
log.log(ILog.INFORMATION, ' url = + linkStr_ + POST =
+ formDataStr_ + ( +
formDataStr_.length() + ));
} catch (Exception e) {
log.log(ILog.ERROR, Caught exception: + e.getMessage()
+ ignoring sample);
sample.setTTLB(-1);
sample.setTTFB(-1);
sample.setSize(-1);
sample.setLastPage(null);
return sample;
}
}
// this is where 'then = currentTime' used to be.
sample.setTTReq(System.currentTimeMillis() - then);
// Check for cookies coming back
try {
if (cookieManager != null) {
// check for Set-Cookie headers