You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Yair Zohar <ya...@ard.huji.ac.il> on 2007/03/29 15:13:07 UTC

How do I get the response status code?

Hello,
I'm trying to create a filter that will do the access logging for my web 
application
(I would like to write the information directly to the database not to a 
file).
I have a problem to get the status code of the response.
The filter receives a ServletResponse object that do not have a 
getStatus() method.
Any idea ?
Yair Zohar.




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I get the response status code?

Posted by Brantley Hobbs <sb...@uga.edu>.
Martin,

I'm afraid I don't understand.  Would the request length be the same as 
the response?  I'm totally not following.

Brantley

Martin Gainty wrote:
> Brantley-
> 
> as the ServletResponse.getOutputStream() returning a javax.servlet.ServletOutputStream is not RLE (RunLengthEncoded)
> unless of course you implement some form of Tokenizer such as http://forum.java.sun.com/thread.jspa?threadID=734167&tstart=420
> Quickest would be to call HttpServletRequest.getContentLength() and xmit variable back in the response 
> 
> Yair?
> M--

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I get the response status code?

Posted by Martin Gainty <mg...@hotmail.com>.
Brantley-

as the ServletResponse.getOutputStream() returning a javax.servlet.ServletOutputStream is not RLE (RunLengthEncoded)
unless of course you implement some form of Tokenizer such as http://forum.java.sun.com/thread.jspa?threadID=734167&tstart=420
Quickest would be to call HttpServletRequest.getContentLength() and xmit variable back in the response 

Yair?
M--
--------------------------------------------------------------------------- 
This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited.
--------------------------------------------------------------------------- 
Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
----- Original Message ----- 
From: "Brantley Hobbs" <sb...@uga.edu>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Thursday, March 29, 2007 10:12 AM
Subject: Re: How do I get the response status code?


> Ahh.....please ignore my last.
> 
> I see that you're doing the same thing I mentioned (setting a private 
> variable and returning that as the status).
> 
> Is this not working for you?
> 
> Brantley
> 
> Yair Zohar wrote:
>> Hi Brantley,
>> Thanks for replying.
>> I've tried to pass a wrapper to the filter's chain, here is the 
>> wrapper's code:
>> 
>> import java.io.IOException;
>> import javax.servlet.http.*;
>> 
>> public class TestResponse extends HttpServletResponseWrapper {
>>    private int statusCode;
>>    public TestResponse(HttpServletResponse response) {
>>        super(response);
>>    }
>>    public int getStatus() {
>>        return statusCode;
>>    }
>>    public void sendError(int errorCode) throws IOException {
>>        this.statusCode = errorCode;
>>        super.sendError(errorCode);      }
>>    public void sendError(int errorCode, String errorMessage) throws 
>> IOException {
>>        this.statusCode = errorCode;
>>        super.sendError(errorCode, errorMessage);      }
>>    public void setStatus(int statusCode) {
>>        this.statusCode = statusCode;
>>        super.setStatus(statusCode);
>>    }
>> }
>> 
>> I hopped tomcat will use the wrapper's setStatus() method and then I 
>> will be able to get the status code.
>> What actually happened is that sometimes the status code returned was 0 
>> and sometimes 404 or 304. It seems tomcat used the wrapper's setStatus() 
>> method only in part of the cases (maybe only when there was a problem 
>> getting the page).
>> 
>> How does the byte count gives information on the status code ?
>> How do you get the byte count from the output stream ?
>> 
>> Yair.
>> 
>> 
>> 
>> Brantley Hobbs wrote:
>>> Yair,
>>>
>>> I too would be interested in this.  I wrote a logging filter that does 
>>> what you describe, but the best that I could come up with was a 
>>> response wrapper that was passed along the filter chain.  In the 
>>> wrapper, I could set a status, thus guaranteeing that I would end up 
>>> with a status at the end.  The wrapper extends 
>>> HttpServletResponseWrapper.
>>>
>>> You may also find a wrapper useful because response sizes are not 
>>> always set either, at least in my experience.  With the wrapper, you 
>>> can monitor the output stream to get a byte count.
>>>
>>> B.
>>>
>>> Yair Zohar wrote:
>>>> Hello,
>>>> I'm trying to create a filter that will do the access logging for my 
>>>> web application
>>>> (I would like to write the information directly to the database not 
>>>> to a file).
>>>> I have a problem to get the status code of the response.
>>>> The filter receives a ServletResponse object that do not have a 
>>>> getStatus() method.
>>>> Any idea ?
>>>> Yair Zohar.
>>>>
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To start a new topic, e-mail: users@tomcat.apache.org
>>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To start a new topic, e-mail: users@tomcat.apache.org
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>>>
>> 
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
>

Re: How do I get the response status code?

Posted by Brantley Hobbs <sb...@uga.edu>.
Yeah.  I think that's what I ran across (I wrote that code more than a 
year ago).  I think that's why I ended up initializing that internal 
variable to 200 so I'd be guaranteed to get something.

It looks as if the AccessLog valve does something like this:

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;

public void invoke(Request request, Response response) throws 
IOException, ServletException {

//snip tons of string concatenation....
result.append("\" ");
result.append(response.getStatus());
result.append(space);
//snip rest of string concatenation and write to log....

}

So, the AccessLog valve is using some "custom" wrapper around a 
request/response to do the same thing we're doing.  What I'm guessing is 
that servlets aren't *required* to set a status, so it's possible that 
any attempt at a getStatus() can't be guaranteed to return anything, so 
it's just not implemented in either the spec or the reference 
implementation (tomcat itself).

I've seen solutions that range from the technique that I use (set a 
value so you know that it's not in an indeterminate state) or even to 
grab the output stream and parse the status header out.  Again, it's not 
clear if the status header has even been set yet.

I think that what's happening is that tomcat wraps any 
requests/responses in that 
org.apache.catalina.connection.Request/Response object so that as a 
point of last resort it can set a status code if it hasn't been set 
already.  I don't know if that's what the spec says to do or not.  It 
certainly seems that if the spec doesn't *require* setStatus() to be 
called on an HttpServletResponse object, then there's a hole in the 
spec, IMHO.

My $0.02

B.



Yair Zohar wrote:
> Well, it does, partially.
> I sometimes get a non zero status code, but it's not zero only when 
> there is an error (status code s: 404, 304).
> Yair.
> 
> 
> Brantley Hobbs wrote:
>> Ahh.....please ignore my last.
>>
>> I see that you're doing the same thing I mentioned (setting a private 
>> variable and returning that as the status).
>>
>> Is this not working for you?
>>
>> Brantley
>>
>> Yair Zohar wrote:
>>> Hi Brantley,
>>> Thanks for replying.
>>> I've tried to pass a wrapper to the filter's chain, here is the 
>>> wrapper's code:
>>>
>>> import java.io.IOException;
>>> import javax.servlet.http.*;
>>>
>>> public class TestResponse extends HttpServletResponseWrapper {
>>>    private int statusCode;
>>>    public TestResponse(HttpServletResponse response) {
>>>        super(response);
>>>    }
>>>    public int getStatus() {
>>>        return statusCode;
>>>    }
>>>    public void sendError(int errorCode) throws IOException {
>>>        this.statusCode = errorCode;
>>>        super.sendError(errorCode);      }
>>>    public void sendError(int errorCode, String errorMessage) throws 
>>> IOException {
>>>        this.statusCode = errorCode;
>>>        super.sendError(errorCode, errorMessage);      }
>>>    public void setStatus(int statusCode) {
>>>        this.statusCode = statusCode;
>>>        super.setStatus(statusCode);
>>>    }
>>> }
>>>
>>> I hopped tomcat will use the wrapper's setStatus() method and then I 
>>> will be able to get the status code.
>>> What actually happened is that sometimes the status code returned was 
>>> 0 and sometimes 404 or 304. It seems tomcat used the wrapper's 
>>> setStatus() method only in part of the cases (maybe only when there 
>>> was a problem getting the page).
>>>
>>> How does the byte count gives information on the status code ?
>>> How do you get the byte count from the output stream ?
>>>
>>> Yair.
>>>
>>>
>>>
>>> Brantley Hobbs wrote:
>>>> Yair,
>>>>
>>>> I too would be interested in this.  I wrote a logging filter that 
>>>> does what you describe, but the best that I could come up with was a 
>>>> response wrapper that was passed along the filter chain.  In the 
>>>> wrapper, I could set a status, thus guaranteeing that I would end up 
>>>> with a status at the end.  The wrapper extends 
>>>> HttpServletResponseWrapper.
>>>>
>>>> You may also find a wrapper useful because response sizes are not 
>>>> always set either, at least in my experience.  With the wrapper, you 
>>>> can monitor the output stream to get a byte count.
>>>>
>>>> B.
>>>>
>>>> Yair Zohar wrote:
>>>>> Hello,
>>>>> I'm trying to create a filter that will do the access logging for 
>>>>> my web application
>>>>> (I would like to write the information directly to the database not 
>>>>> to a file).
>>>>> I have a problem to get the status code of the response.
>>>>> The filter receives a ServletResponse object that do not have a 
>>>>> getStatus() method.
>>>>> Any idea ?
>>>>> Yair Zohar.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To start a new topic, e-mail: users@tomcat.apache.org
>>>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To start a new topic, e-mail: users@tomcat.apache.org
>>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>>
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To start a new topic, e-mail: users@tomcat.apache.org
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I get the response status code?

Posted by Yair Zohar <ya...@ard.huji.ac.il>.
Well, it does, partially.
I sometimes get a non zero status code, but it's not zero only when 
there is an error (status code s: 404, 304).
Yair.


Brantley Hobbs wrote:
> Ahh.....please ignore my last.
>
> I see that you're doing the same thing I mentioned (setting a private 
> variable and returning that as the status).
>
> Is this not working for you?
>
> Brantley
>
> Yair Zohar wrote:
>> Hi Brantley,
>> Thanks for replying.
>> I've tried to pass a wrapper to the filter's chain, here is the 
>> wrapper's code:
>>
>> import java.io.IOException;
>> import javax.servlet.http.*;
>>
>> public class TestResponse extends HttpServletResponseWrapper {
>>    private int statusCode;
>>    public TestResponse(HttpServletResponse response) {
>>        super(response);
>>    }
>>    public int getStatus() {
>>        return statusCode;
>>    }
>>    public void sendError(int errorCode) throws IOException {
>>        this.statusCode = errorCode;
>>        super.sendError(errorCode);      }
>>    public void sendError(int errorCode, String errorMessage) throws 
>> IOException {
>>        this.statusCode = errorCode;
>>        super.sendError(errorCode, errorMessage);      }
>>    public void setStatus(int statusCode) {
>>        this.statusCode = statusCode;
>>        super.setStatus(statusCode);
>>    }
>> }
>>
>> I hopped tomcat will use the wrapper's setStatus() method and then I 
>> will be able to get the status code.
>> What actually happened is that sometimes the status code returned was 
>> 0 and sometimes 404 or 304. It seems tomcat used the wrapper's 
>> setStatus() method only in part of the cases (maybe only when there 
>> was a problem getting the page).
>>
>> How does the byte count gives information on the status code ?
>> How do you get the byte count from the output stream ?
>>
>> Yair.
>>
>>
>>
>> Brantley Hobbs wrote:
>>> Yair,
>>>
>>> I too would be interested in this.  I wrote a logging filter that 
>>> does what you describe, but the best that I could come up with was a 
>>> response wrapper that was passed along the filter chain.  In the 
>>> wrapper, I could set a status, thus guaranteeing that I would end up 
>>> with a status at the end.  The wrapper extends 
>>> HttpServletResponseWrapper.
>>>
>>> You may also find a wrapper useful because response sizes are not 
>>> always set either, at least in my experience.  With the wrapper, you 
>>> can monitor the output stream to get a byte count.
>>>
>>> B.
>>>
>>> Yair Zohar wrote:
>>>> Hello,
>>>> I'm trying to create a filter that will do the access logging for 
>>>> my web application
>>>> (I would like to write the information directly to the database not 
>>>> to a file).
>>>> I have a problem to get the status code of the response.
>>>> The filter receives a ServletResponse object that do not have a 
>>>> getStatus() method.
>>>> Any idea ?
>>>> Yair Zohar.
>>>>
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To start a new topic, e-mail: users@tomcat.apache.org
>>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To start a new topic, e-mail: users@tomcat.apache.org
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I get the response status code?

Posted by Brantley Hobbs <sb...@uga.edu>.
Ahh.....please ignore my last.

I see that you're doing the same thing I mentioned (setting a private 
variable and returning that as the status).

Is this not working for you?

Brantley

Yair Zohar wrote:
> Hi Brantley,
> Thanks for replying.
> I've tried to pass a wrapper to the filter's chain, here is the 
> wrapper's code:
> 
> import java.io.IOException;
> import javax.servlet.http.*;
> 
> public class TestResponse extends HttpServletResponseWrapper {
>    private int statusCode;
>    public TestResponse(HttpServletResponse response) {
>        super(response);
>    }
>    public int getStatus() {
>        return statusCode;
>    }
>    public void sendError(int errorCode) throws IOException {
>        this.statusCode = errorCode;
>        super.sendError(errorCode);      }
>    public void sendError(int errorCode, String errorMessage) throws 
> IOException {
>        this.statusCode = errorCode;
>        super.sendError(errorCode, errorMessage);      }
>    public void setStatus(int statusCode) {
>        this.statusCode = statusCode;
>        super.setStatus(statusCode);
>    }
> }
> 
> I hopped tomcat will use the wrapper's setStatus() method and then I 
> will be able to get the status code.
> What actually happened is that sometimes the status code returned was 0 
> and sometimes 404 or 304. It seems tomcat used the wrapper's setStatus() 
> method only in part of the cases (maybe only when there was a problem 
> getting the page).
> 
> How does the byte count gives information on the status code ?
> How do you get the byte count from the output stream ?
> 
> Yair.
> 
> 
> 
> Brantley Hobbs wrote:
>> Yair,
>>
>> I too would be interested in this.  I wrote a logging filter that does 
>> what you describe, but the best that I could come up with was a 
>> response wrapper that was passed along the filter chain.  In the 
>> wrapper, I could set a status, thus guaranteeing that I would end up 
>> with a status at the end.  The wrapper extends 
>> HttpServletResponseWrapper.
>>
>> You may also find a wrapper useful because response sizes are not 
>> always set either, at least in my experience.  With the wrapper, you 
>> can monitor the output stream to get a byte count.
>>
>> B.
>>
>> Yair Zohar wrote:
>>> Hello,
>>> I'm trying to create a filter that will do the access logging for my 
>>> web application
>>> (I would like to write the information directly to the database not 
>>> to a file).
>>> I have a problem to get the status code of the response.
>>> The filter receives a ServletResponse object that do not have a 
>>> getStatus() method.
>>> Any idea ?
>>> Yair Zohar.
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To start a new topic, e-mail: users@tomcat.apache.org
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I get the response status code?

Posted by Brantley Hobbs <sb...@uga.edu>.
No, the byte count doesn't actually have anything to do with the status 
code.  I simply added that statement as another reason for using the 
wrapper.  Sorry for the confusion.

I agree with your earlier statement that sometimes the response code 
doesn't get set (or at least it gets set at some point farther 
downstream than the filters themselves).  What I did with my wrapper 
class was create a protected variable named status and initialized it 
with 200.  Then I overrode setStatus to look like this:

public void setStatus(int sc) {
	status = sc;
	super.setStatus(sc);
}

And overrode getStatus to look like this:

public int getStatus() {
	return status;
}

That way, I *always* get back a status code.  It's assumed to be a 200, 
which might not necessarily be correct, but at least it's a valid code.

This could be why the logging mechanism that comes with Tomcat was 
implemented as a valve; a response code may not necessarily be 
guaranteed to be set until the valve layer in the code.  Response size 
is the same way, as far as I can tell, which is why you have to do a 
similar trick with it.  I haven't torn apart the AccessLog valve source, 
so I'm only operating on assumptions here.  For all I know, the 
AccessLog valve may do the same thing.

Perhaps one of the old-timers could comment on this?

I have source, if you're interested.

B.




Yair Zohar wrote:
> Hi Brantley,
> Thanks for replying.
> I've tried to pass a wrapper to the filter's chain, here is the 
> wrapper's code:
> 
> import java.io.IOException;
> import javax.servlet.http.*;
> 
> public class TestResponse extends HttpServletResponseWrapper {
>    private int statusCode;
>    public TestResponse(HttpServletResponse response) {
>        super(response);
>    }
>    public int getStatus() {
>        return statusCode;
>    }
>    public void sendError(int errorCode) throws IOException {
>        this.statusCode = errorCode;
>        super.sendError(errorCode);      }
>    public void sendError(int errorCode, String errorMessage) throws 
> IOException {
>        this.statusCode = errorCode;
>        super.sendError(errorCode, errorMessage);      }
>    public void setStatus(int statusCode) {
>        this.statusCode = statusCode;
>        super.setStatus(statusCode);
>    }
> }
> 
> I hopped tomcat will use the wrapper's setStatus() method and then I 
> will be able to get the status code.
> What actually happened is that sometimes the status code returned was 0 
> and sometimes 404 or 304. It seems tomcat used the wrapper's setStatus() 
> method only in part of the cases (maybe only when there was a problem 
> getting the page).
> 
> How does the byte count gives information on the status code ?
> How do you get the byte count from the output stream ?
> 
> Yair.
> 
> 
> 
> Brantley Hobbs wrote:
>> Yair,
>>
>> I too would be interested in this.  I wrote a logging filter that does 
>> what you describe, but the best that I could come up with was a 
>> response wrapper that was passed along the filter chain.  In the 
>> wrapper, I could set a status, thus guaranteeing that I would end up 
>> with a status at the end.  The wrapper extends 
>> HttpServletResponseWrapper.
>>
>> You may also find a wrapper useful because response sizes are not 
>> always set either, at least in my experience.  With the wrapper, you 
>> can monitor the output stream to get a byte count.
>>
>> B.
>>
>> Yair Zohar wrote:
>>> Hello,
>>> I'm trying to create a filter that will do the access logging for my 
>>> web application
>>> (I would like to write the information directly to the database not 
>>> to a file).
>>> I have a problem to get the status code of the response.
>>> The filter receives a ServletResponse object that do not have a 
>>> getStatus() method.
>>> Any idea ?
>>> Yair Zohar.
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To start a new topic, e-mail: users@tomcat.apache.org
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I get the response status code?

Posted by Yair Zohar <ya...@ard.huji.ac.il>.
Hi Brantley,
Thanks for replying.
I've tried to pass a wrapper to the filter's chain, here is the 
wrapper's code:

import java.io.IOException;
import javax.servlet.http.*;

public class TestResponse extends HttpServletResponseWrapper {
    private int statusCode;
    public TestResponse(HttpServletResponse response) {
        super(response);
    }
    public int getStatus() {
        return statusCode;
    }
    public void sendError(int errorCode) throws IOException {
        this.statusCode = errorCode;
        super.sendError(errorCode);   
    }
    public void sendError(int errorCode, String errorMessage) throws 
IOException {
        this.statusCode = errorCode;
        super.sendError(errorCode, errorMessage);   
    }
    public void setStatus(int statusCode) {
        this.statusCode = statusCode;
        super.setStatus(statusCode);
    }
}

I hopped tomcat will use the wrapper's setStatus() method and then I 
will be able to get the status code.
What actually happened is that sometimes the status code returned was 0 
and sometimes 404 or 304. It seems tomcat used the wrapper's setStatus() 
method only in part of the cases (maybe only when there was a problem 
getting the page).

How does the byte count gives information on the status code ?
How do you get the byte count from the output stream ?

Yair.



Brantley Hobbs wrote:
> Yair,
>
> I too would be interested in this.  I wrote a logging filter that does 
> what you describe, but the best that I could come up with was a 
> response wrapper that was passed along the filter chain.  In the 
> wrapper, I could set a status, thus guaranteeing that I would end up 
> with a status at the end.  The wrapper extends 
> HttpServletResponseWrapper.
>
> You may also find a wrapper useful because response sizes are not 
> always set either, at least in my experience.  With the wrapper, you 
> can monitor the output stream to get a byte count.
>
> B.
>
> Yair Zohar wrote:
>> Hello,
>> I'm trying to create a filter that will do the access logging for my 
>> web application
>> (I would like to write the information directly to the database not 
>> to a file).
>> I have a problem to get the status code of the response.
>> The filter receives a ServletResponse object that do not have a 
>> getStatus() method.
>> Any idea ?
>> Yair Zohar.
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To start a new topic, e-mail: users@tomcat.apache.org
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How do I get the response status code?

Posted by Brantley Hobbs <sb...@uga.edu>.
Yair,

I too would be interested in this.  I wrote a logging filter that does 
what you describe, but the best that I could come up with was a response 
wrapper that was passed along the filter chain.  In the wrapper, I could 
set a status, thus guaranteeing that I would end up with a status at the 
end.  The wrapper extends HttpServletResponseWrapper.

You may also find a wrapper useful because response sizes are not always 
set either, at least in my experience.  With the wrapper, you can 
monitor the output stream to get a byte count.

B.

Yair Zohar wrote:
> Hello,
> I'm trying to create a filter that will do the access logging for my web 
> application
> (I would like to write the information directly to the database not to a 
> file).
> I have a problem to get the status code of the response.
> The filter receives a ServletResponse object that do not have a 
> getStatus() method.
> Any idea ?
> Yair Zohar.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org