wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlets and session...Do I need to? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets and session...Do I need to?" Watch "Servlets and session...Do I need to?" New topic
Author

Servlets and session...Do I need to?

Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Hi all,

I am right now working on an application which has some 20 JSPs and 5 servlets. It is an appln where user logs into the system in two different modes and perform the operations. The back End is Oracle.

My concern now is, is it necessary to maintain a session. Though it may sound absurd, i am quite new to developing applications and this is my first one.

If so, how to get the session persisting for the entire application?

Kindly help me in this regard.

Thanks in advance.....

Cheers,
Swamy
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
you can maintain the session yourself if you really want to. otherwise the servlet will maintain its own session HttpSession anyway.

you can get that session like this:

request.getSession(true)

or session id like this:

request.getSession(true).getId()
[ September 13, 2004: Message edited by: adeel ansari ]
Ramaswamy Srinivasan
Ranch Hand

Joined: Aug 31, 2004
Posts: 295
Thanks Adeel.

I shall try this out and get back to you. Any ways, it would be great if you can expand a bit on Why do we go for a session, when servlet can maintain its own? How long that session is active?

Is there any difference between the user maintained and the servlet maintained session?

Thanks a lot in advance.

Cheers,
Swamy
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
1 - this is the optional element in web.xml by which we can tell our container how long a session will exists.

<session-config>
<session-timeout>60</session-timeout>
</session-config>

2 - by saying "we maintain session" i mean, incase we want to do somthing on session creation or destruction, like maintain logs of session in some file or in DB. then we have to do it for ourselves by implementing HttpSessionListener. this class is not found in the previous release of servlet specification. so we used to use HttpSessionBindingListener for this kind of implementation.
Sham Grandhe
Ranch Hand

Joined: Dec 16, 2003
Posts: 73
hai adeeel
This is sham kumar from bangalore. our chat on the topic is helping me, thank u, can you plz just specify which version of servlet the Listener are present

thank you
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
we have HttpSessionListener in servlet 2.4, previous version i am not sure. we can use it with tomcat 5.0.

as i said for previous version we can go for HttpSessionBindingListener. i haven't tried this but hope it would work.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
HttpSessionListener since Servlets 2.3; HttpSessionBindingListener since Servlets 2.0. HttpSessionBindingListener notifies when attributes are bound or unbound to/from the session, rather than when the session itself is created/destroyed. I guess you could contrive this pre-2.3 to perform the same functionality as the newer HttpSessionListener.

Jules
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets and session...Do I need to?