On Mon, 3 Jun 2002, Ralph Einfeldt wrote:

> Date: Mon, 3 Jun 2002 19:03:46 +0200
> From: Ralph Einfeldt <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: AW: How to use setRequestedSessionId
>
> Have a look at:
> http://www.mail-archive.com/tomcat-user@jakarta.apache.org/msg41615.html
>

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
this:

    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
(http://java.sun.com/webservices/), 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.

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to