File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to make Filter getting called only once for every User Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to make Filter getting called only once for every User" Watch "How to make Filter getting called only once for every User" New topic
Author

How to make Filter getting called only once for every User

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi , i have a filter configured as shown below inside my web.xml file ,




I see that this Filter is being called for every request and response .

Actually , my requirement is that , the Filter servlet should be called only once for every user log in .

Please help as how can i do this ??

Thanks .

Save India From Corruption - Anna Hazare.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Filters are called for every request made to the filtered resource - that is their purpose, there is no way to declaretively specify a filter for the first request by a client and ignore all others. The logic you write in your filter can do what it likes though.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you very much .
Radovan Vukovic
Greenhorn

Joined: Nov 03, 2010
Posts: 19
Ravi Kiran Va wrote:
Actually , my requirement is that , the Filter servlet should be called only once for every user log in .


Do this means that you have some kind login transaction (servlet or whatever )?
If it’s so I think that you can map this for some specific servlet



Before couple of years I was working with acegi security and should be that acegi filter chain proxy is defined on similar way

It’s just idea could be that am wrong
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Why use a filter at all when the action maps to a distinct Servlet? What does this give you that you can't do directly in the Servlet?
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234



In my case it is something related to setting the character set to UTF-8 .

As per your previous answer
"The logic you write in your filter can do what it likes though."


You mean to use in this way inside doFilter()



}

Please correct me if i am wrong . Thanks in advance .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


In my case it is something related to setting the character set to UTF-8 .

Is that safe? Why do that rather than setting the content-type header? And why set the character set just for a single request - surely if you are doing it at all you need it for all requests?
Todd Buell
Greenhorn

Joined: May 31, 2005
Posts: 21
Could you just set a session attribute as an indicator that the filter has been executed for this user's session. If it's not present, execute the filter code, set the value then continue on. If the session value is present this session must have already executed so short-circuit any processing and just continue.

example:
 
 
subject: How to make Filter getting called only once for every User
 
Similar Threads
ServletFilter not being called
Can't get my login servlet to be called
Filter Problems in Web.xml
[Solved][HFSJ] Filter Preferences
Struts Filter in Web.xml