Forums Register Login

accessing a session attribute from a class that implements Filter

+Pie Number of slices to send: Send
Hi, how can i access a session attribute from a class that implements the Filter interface???. I'm able to get de ServletContext but i cannot access the session. I want to check the existence of a session attribute named user so i can filter the access to certain servlets.
Is this correct?:



Thanks.
[ March 28, 2006: Message edited by: Anibal Smith ]
+Pie Number of slices to send: Send
How do you know, you dont have access to session in there?

P.S. getAttribute returns Object. You need to cast it.
+Pie Number of slices to send: Send
I suppose that because the reference i got in the doFilter method is of the type ServletRequest and that reference doesn't have a reference to a session, the method getAttribute of the reference returns a request attribute, So i thought i could make a cast like this:
HttpServletRequest httpRequest = (HttpServletRequest) request;
and then access a session attribute through httpRequest like this:
httpRequest.getSession().getAttribute("user");
Is this a valid way to do it?.
+Pie Number of slices to send: Send
Yes.
+Pie Number of slices to send: Send
Thanks Bear, i'm glad to receive a confirmation, your reply is really appreciated.
[ March 28, 2006: Message edited by: Anibal Smith ]
+Pie Number of slices to send: Send
Are we saying, "because the reference in doFilter() method is of type ServletRequest therefore it doesn't have a reference to a session"?

If yes then why is it like that. I never read something like this anywhere or might be i missed the point. But when the argument is a HttpServletRequest instance, no matter if it takes it in a parent type, it should return the session object.

Bear, could you please explain this? and please refer me something to read. I have gone through the servlet 2.4 specs, but unable to find this.

[Edited]
I just tried to get the session from request attribute in doFilter() method, and its working for me like boom.
[ March 29, 2006: Message edited by: Adeel Ansari ]
+Pie Number of slices to send: Send
 

Originally posted by Anibal Smith:
... the method getAttribute of the reference returns a request attribute, So i thought . ..



Are we discussing request attribute?
+Pie Number of slices to send: Send
HttpServletRequest extends ServletRequest. If check the Java API

ServletRequest does not have a getSession method. it is specific to HttpServletRequest (http is a stateless protocol so that you can use session object to maintain client state)
+Pie Number of slices to send: Send
 

Originally posted by ak pillai:
HttpServletRequest extends ServletRequest. If check the Java API

ServletRequest does not have a getSession method. it is specific to HttpServletRequest (http is a stateless protocol so that you can use session object to maintain client state)



What are you talking about?
Please read previous posts carefully.
+Pie Number of slices to send: Send
No, request.getSession() without parameters will create a new session, so it can be not desirable side effect. I'd recommend to use request.getSession(false) and then check for null.
+Pie Number of slices to send: Send
 

Originally posted by dema rogatkin:
No, request.getSession() without parameters will create a new session, so it can be not desirable side effect. I'd recommend to use request.getSession(false) and then check for null.



Agreed. But its a different story here, I guess.
+Pie Number of slices to send: Send
 

But when the argument is a HttpServletRequest instance, no matter if it takes it in a parent type, it should return the session object.



Only if you cast it to HttpServletRequest. ServetRequest does not have a getSession method.
+Pie Number of slices to send: Send
 

Originally posted by Pradip Bhat:
Only if you cast it to HttpServletRequest. ServetRequest does not have a getSession method.



Yes, thats what he was already doing.

[Edited]
See OP's first post. Though he/she edited the post, but the code in that post is there from the very begining.
[ March 29, 2006: Message edited by: Adeel Ansari ]
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5265 times.
Similar Threads
Filter Problem
Filter to check if user is logged in
User filter
org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found
problem while giving a url pattern in filter mapping
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:39:55.