aspose file tools*
The moose likes Servlets and the fly likes Setting response headers conditionally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Setting response headers conditionally" Watch "Setting response headers conditionally" New topic
Author

Setting response headers conditionally

Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

Hi Ranchers ,

I need to set the response headers to prevent caching of the data. I am using the following code in a filter:



This code works fine.
Now, my requirement is to set the headers conditionally like:



But the headers are not being set in this case. Any way through which I can make it work?

Thanks and regards,
Nitin.


SCJP 1.4, SCWCD 5
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

The obvious thing would be to assign true to that "condition" variable. If the headers aren't being generated then you aren't doing that.
Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

The scenario is :

When the user logs out , I am setting the session variable to null. If the user tries to view the previously visited page using the internet explorer's Back button, In a filter I am checking


if(session==null). If session is null , I am setting the header variables.


But , the header variables are not being set in this case.

Is the response commited before checking the condition?

Thanks and regards,
Nitin.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Patil Niteen wrote:Is the response commited before checking the condition?


That's impossible to say. It's your code which is checking that condition, so perhaps your code might be committing the response before that. We can't see any of your code.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
When the user logs out , I am setting the session variable to null.


What exactly do you think that accomplishes?

The API provides the invalidate() method in javax.servlet.http.HttpSession - thats what you should be using.

What are you using to monitor the values of the response headers?

Bill
Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

I am setting the attribute of the session for logout to null. I am checking the same session variable in the filter for valid login. If the attribute is null, I am trying to prevent caching of the page. This way , I am trying to prevent the user to view the visited pages after logout using Internet Explorer's BACK button.

The conditionally setting of the response headers is not working for me.

Here,s the code: For logout:


I am checking the userLogger attribute of the session in the filter as:



If the condition is removed from the code. The response headers are set and the user is prevented to view the visited pages after logout.
Also, the print statement prints: Response Committed:false.

Note: I am using this in a GRAILS application. Since, the issue is with the response and setting the headers , I have posted it here.
Thanks and regards,
Nitin
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
If this was my problem I would use Firefox with live http headers turned on as the client to see what is actually being sent in the response headers.

That code you posted - in what method of the filter does it appear?

Bill
Patil Niteen
Ranch Hand

Joined: Dec 10, 2009
Posts: 48

The filter code is:




Also, I tried this in after method and it doesn't work.
Thanks.
Nitin.
 
 
subject: Setting response headers conditionally