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 customize session management? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to customize session management?" Watch "How to customize session management?" New topic
Author

How to customize session management?

harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
Hi all,

Could someone tell me how I can customize session management?

The HttpSession has a lot of features, but I would like to add some of my customized methods and attributes to the session. I can't extend HttpSession as it is an interface.

So what can I do?

The only solution that I came up with is if I make my own session management and that would be a lot of work and problem.

I am using "Tomcat" as my web server.

thanks in advance.

regards,
harke
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What kind of functionality do you want to add? Would it be easier to just decorate the regular HttpSession object and create a servlet base class that uses it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

What problem are you trying to solve?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
I wanted to have other ids besides the session id. eg: view id -> that changes for every view during a session. And methods for lets say keeping track of roles and permissions for a username.

One way of achieving this was by setting an attribute called viewID and similarly other setattributes. But is there a way in which I could have this as an actual attribute of the httpsession class?

Basically, I wanted to expand the functionality of the httpsession object.

thanks,
harke
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

I'd recommend just using session-scoped variables like everyone else.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yeah, I don't really see the need for what you're trying to do either, or at least not in the way you're trying to do it. As I suggested previously, if you want a bunch of this "magic" stuff to happen, abstract it away, put it in a filter or base servlet or somewhere. This also has the benefit of working across deployment environments.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
It is certainly possible to create your own "session" management - Just:

1. Define the class that does all you want, make it Serializable so the individual user sessions can be stored on disk or in a database.
2. On initial entry of your user, create an instance and give it a unique id string that is also a valid file name and write it to disk
3. IF you want to use the servlet session mechanism at all, store the id in the user's session, otherwise use the cookie mechanism to ensure that every user request will contain the unique id.
4. On subsequent requests recover the serialized object, modify and re-write.

Add some caching and you will have duplicated the servlet session with your own code. An interesting programming exercise but probably not necessary.

Bill
harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
Thanks all for your answers. I now have a lot better idea what to do.

One more quick question, is the session id (JSESSIONID) created automatically when a user makes a first time request?

Thanks,
harke
Nandkishore Dhilde
Greenhorn

Joined: May 09, 2002
Posts: 20
A session is begun when servlet (or filter) code invokes the HttpServletRequest.getSession() method. And that time web container generates unique id as name JSESSIONID and embeded as value of a cookie


SCJP1.4 certified, IBM SOA Solution Designer[2007]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to customize session management?