• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make Filter getting called only once for every User

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much .
 
Radovan Vukovic
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 10336
Eclipse IDE Hibernate Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic