views:

500

answers:

5

Hi! I have one servlet taking care of multiple sites and therefore I want to have different sessions for different sites, even if its the same user.

Is there any support for this in Java or do I need to prefix the attribute names instead? I guess prefixing is not a good idea.

/Br Johannes

A: 

I think you're looking for something like Apache Tomcat. It will manage individual sessions for individual servlet applications.

Peter Di Cecco
Yeah Im using Tomcat.The same servlet will show different html-pages depending on different url-parameters. For different url-parameters I would like to have different Sessions, even though its the same user
Johannes
A: 

as far as I know, the session has nothing to do with which user or site. You can just get session object as usual and do your business logic.

If your goal is to distinguish the source of those requests, you could either check ip address or add a flag in request. Not sure if this is what you want.

btw, What is a 'site' in your domain?

Kent
As far as I know the Session is bound to my servlet, because the path variable in the Cookie is set to the name of the servlet.My servlet will show different html-pages depending on different url-parameters. For different url-parameters I would like to have different Sessions, even though its the same user
Johannes
A: 

The session is unique for a combination of user and web application. You can of course deploy your servlet in several web applications on the same Tomcat instance, but you will not be able to route the HTTP request to different web applications simply based on URL parameters unless you evaluate the URL parameters in a second servlet and redirect the browser to a new URL for the specific web app.

Different servlet containers or J2EE app servers may have different options for routing requests to specific web applications, but AFAIK out of the box, Tomcat can only delegate the request based on either host name or base directory, e.g.:

jarnbjo
+1  A: 

This CANNOT be done in the servlet container based on URL parameters alone; you'll have to do it yourself. Instead of dealing with attribute prefixes in your servlet, however, the easiest way to manage "separate" sessions is via filter:

  1. Write a simple wrapper class for HttpSession. Have it hold a Map of attributes and back all attribute / value methods by said map; delegate all the other methods to the actual session you're wrapping. Override invalidate() method to remove your session wrapper instead of killing the entire "real" session.
  2. Write a servlet filter; map it to intercept all applicable URLs.
  3. Maintain a collection of your session wrappers as an attribute within the real session.
  4. In your filter's doFilter() method extract the appropriate session wrapper from the collection and inject it into the request you're passing down the chain by wrapping the original request into HttpServletRequestWrapper whose getSession() method is overwritten.
  5. Your servlets / JSPs / etc... will enjoy "separate" sessions.

Note that Sessions's "lastAccessedTime" is shared with this approach. If you need to keep those separate you'll have to write your own code for maintaining this setting and for expiring your session wrappers.

ChssPly76
A: 
http://localhost:8081/TestChat/chatroom2![alt text][1]
fgf