Adding new header in doFilter()

Hi all..

I'm having problem.. with adding customized header in my application...

Here's a sample of my code....

try {

HttpServletResponse httpRES = (HttpServletResponse) res;

if (!httpRES.containsHeader("cpa_cp_id"))

httpRES.addHeader("cpa_cp_id","hi");

chain.doFilter(req, res);

} catch (Exception ex) {

}

}

Is there something else I had left out?

Thanks..

[440 byte] By [Simon_Sewa] at [2007-9-23]
# 1
> Is there something else I had left out? Yes, you didn't say what the problem was.
serlanka at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
> Is there something else I had left out? One thing I could obviously notice is that you are suppressing the exceptions in that block. It wouldn't be much help in finding out of your code is working correctly or not.
xHackera at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3
Sorry for my late reply...My problem was.. I'm unable to retrieve the new header value...that I had set in the doFilter().
Simon_Sewa at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4
How are you trying to retrieve the header value?And what are you trying to achieve?
serlanka at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hi.. I'm trying to add a new header to the bunch of headers that already existed. I would like to retrieve the value that I had set earlier in another jsp page.Or is it, you can override the existing header values but not add new header?Thanks
Simon_Sewa at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6

This is how the HttpServletRequest and HttpServletResponse works ... with the request, you can retrieve the headers, but not set them. With the response, you can set the headers but not retrieve them.

So, if you want to retrieve the headers later, you would need to write a HttpServletResponseWrapper that provides you with that functionality.

But based on the non-standard header name you are using, I'm assuming you just want to pass a value to the next resource in that chain.

For that, you should rather use the request or session scopes.

serlanka at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7

Any sample code for the HtttpServletResponseWrapper would be appreciated.

The problem is "the next resource" requires the value to be stored inside the headers (I know request.getParameter() would make my life easier but then I couldn't use it)

That page (inside another server) which I'm calling requires me to add new header information (once again, not session nor request.getParameter()).

Hope you'll understand my situation...

Simon_Sewa at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 8

> That page (inside another server) which I'm calling

> requires me to add new header information (once

> again, not session nor request.getParameter()).

>

> Hope you'll understand my situation...

Your original code does that. So no, I don't understand your situation. Unless it's this:

> I'm unable to retrieve the new header value

So what? You can't retrieve a header from the servlet response in any case. Using a HttpServletResponseWrapper doesn't change that fact. If you've got a design that wants to retrieve headers from the servlet response then you've got a design that wants to be changed.

Note that you DO have a "containsHeader(String)" method to see if the response contains a certain header.

DrClapa at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 9

>Using a HttpServletResponseWrapper doesn't change that fact.

That's exactly what the wrappers are there for, so you can extend the functionality of the original request and response objects.

What I don't get is why it needs to be in the response headers? Tell us what you are trying to do!!!

serlanka at 2007-7-13 > top of java,Enterprise & Remote Computing,Web Tier APIs...