Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes accessing a session attribute from a class that implements Filter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "accessing a session attribute from a class that implements Filter" Watch "accessing a session attribute from a class that implements Filter" New topic
Author

accessing a session attribute from a class that implements Filter

Anibal Smith
Greenhorn

Joined: Mar 23, 2006
Posts: 6
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 ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
How do you know, you dont have access to session in there?

P.S. getAttribute returns Object. You need to cast it.
Anibal Smith
Greenhorn

Joined: Mar 23, 2006
Posts: 6
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?.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Yes.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anibal Smith
Greenhorn

Joined: Mar 23, 2006
Posts: 6
Thanks Bear, i'm glad to receive a confirmation, your reply is really appreciated.
[ March 28, 2006: Message edited by: Anibal Smith ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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 ]
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Anibal Smith:
... the method getAttribute of the reference returns a request attribute, So i thought . ..


Are we discussing request attribute?
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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)


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
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.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

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.


Groovy
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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 ]
 
 
subject: accessing a session attribute from a class that implements Filter
 
Similar Threads
Filter to check if user is logged in
org.springframework.web.HttpSessionRequiredException: Session attribute 'user' required - not found
User filter
Filter Problem
problem while giving a url pattern in filter mapping