tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: AW: How to use setRequestedSessionId
Date Mon, 03 Jun 2002 18:22:56 GMT

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

View raw message