tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Subir Sengupta <>
Subject RE: AW: How to use setRequestedSessionId
Date Tue, 04 Jun 2002 19:42:52 GMT
I'm still unclear about how to do this.  Once a request has been associated
with a session, how do we associate it with a different session.  I'm
probably wrong but it seems the only way is to use setRequestedSessionId().
Once I call getSession(), the session gets associated with the request, and
I'm not sure how to associate it with another session.  I tried setting up
an HttpRequestWrapper but it needs a bunch of abstract methods implemented.

Here's what I'm trying to do:
- In a filter, get the session ID (getSession().getId()) and compare the
value with a value set in another cookie
- if they don't match, create a new session
- associate the new session with the request

So, it's the last step that I don't know how to do.

Any help is appreciated.


-----Original Message-----
From: Craig R. McClanahan []
Sent: Monday, June 03, 2002 11:23 AM
To: Tomcat Users List
Subject: Re: AW: How to use setRequestedSessionId

On Mon, 3 Jun 2002, Ralph Einfeldt wrote:

> Date: Mon, 3 Jun 2002 19:03:46 +0200
> From: Ralph Einfeldt <>
> Reply-To: Tomcat Users List <>
> To: Tomcat Users List <>
> Subject: AW: How to use setRequestedSessionId
> Have a look at:

Ralph, thanks for pointing people to the archives :-).  There is lots of
good stuff there.  This one, happens to be a ResponseWrapper -- the
principles of writing a request wrapper are really similar.

The outline of a "conditionally create the session" filter might be lke

    public class MyFilter implements Filter {

        public void doFilter(ServletRequest request,
                             ServletResponse response,
                             FilterChain chain)
          throws IOException, ServletException {

            HttpServletRequestWrapper wrapper =
              new MyWrapper((HttpServletRequest) request;
            chain.doFilter(wrapper, response);



and the corresponding wrapper class would look like:

    public class MyWrapper extends HttpServletRequestWrapper {

        public HttpSession getSession() {
            if (ok to create session) {
                return super.getSession();
            } else {
                return (null);

        public HttpSession getSession(boolean create) {
            if (ok to create session) {
                return super.getSession(create);
            } else {
                return (null);


All of the rest of the HttpServletRequest methods get delegated through to
the original request -- you only need to override the stuff you want to
customize.  The wrapper instance is ultimately passed on to your servlet
or JSP page, which doesn't necessarily know that the wrapping took place.

Filters and wrappers are very cool -- it's worth your time learning how to
use them.  One source of information is the tutorial on web applications
that is availavle with the Java Web Services Developer Pack
(, which includes Tomcat 4 as its server.

Useful homework exercise - write a Filter and request wrapper that add a
new request parameter to the request, under certain conditions.


To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message