File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to customize session management?

 
harke baj
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problem are you trying to solve?

 
harke baj
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd recommend just using session-scoped variables like everyone else.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic