You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Dr. Evil" <dr...@sidereal.kz> on 2001/11/08 23:20:52 UTC

Filters and ServletContext

Hi, I want to create a filter (in Tomcat 4) which will fetch an object
from the ServletContext, use some information from that, and put that
information in the HttpServletRequest object before passing the
request on.  However, I can't find a way to get access to the
ServletContext object from a filter.  Is it possible to do this?

Thanks

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Filters and ServletContext

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On 9 Nov 2001, Dr. Evil wrote:

> Date: 9 Nov 2001 02:18:46 -0000
> From: Dr. Evil <dr...@sidereal.kz>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: Re: Filters and ServletContext
>
>
> Thanks Craig.  That's just what I needed to know.  Filters are an
> awesome way of doing a lot of things.

I agree -- when we were first designing the Filter API for servlet 2.3 (in
the JSR-053 expert group), I predicted that this would be the best of the
new features.  It's incredibly powerful, but still conceptually simple
enough to understand.

>  Basically, what I will do is
> have a big store of objects in a hashmap in the ServletContext.  When
> a request comes in, a filter will pull the appropriate object from the
> hashmap and puts it in the HttpServletRequest object before passing it
> on.  This is excellent.  Now I have to make sure that I fully
> understand all the thread issues associated with using a HashMap for
> this so that I don't get a corrupted HashMap.
>

Filters have the same threading characterstics as servlets -- there is a
single instance per filter definition.

For your HashMap stored as a servlet context attribute, there are two
scenarios:

* If the HashMap can be modified on the fly while the
  application runs, you should synchronize all gets
  and sets.

* If the HashMap is created at startup time (perhaps
  in a ServletContextListener.contextStarted() method),
  and *never* modified, then you don't have to synchronize
  the gets.

> Thanks!

Craig



--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Filters and ServletContext

Posted by "Dr. Evil" <dr...@sidereal.kz>.
Thanks Craig.  That's just what I needed to know.  Filters are an
awesome way of doing a lot of things.  Basically, what I will do is
have a big store of objects in a hashmap in the ServletContext.  When
a request comes in, a filter will pull the appropriate object from the
hashmap and puts it in the HttpServletRequest object before passing it
on.  This is excellent.  Now I have to make sure that I fully
understand all the thread issues associated with using a HashMap for
this so that I don't get a corrupted HashMap.

Thanks!

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Filters and ServletContext

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On 8 Nov 2001, Dr. Evil wrote:

> Date: 8 Nov 2001 22:20:52 -0000
> From: Dr. Evil <dr...@sidereal.kz>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: Filters and ServletContext
>
>
> Hi, I want to create a filter (in Tomcat 4) which will fetch an object
> from the ServletContext, use some information from that, and put that
> information in the HttpServletRequest object before passing the
> request on.  However, I can't find a way to get access to the
> ServletContext object from a filter.  Is it possible to do this?
>

Yep :-).  Here's a trivial example:

public class MyFilter implements Filter {

  protected FilterConfig config = null;

  protected ServletContext context = null;

  public void init(FilterConfig config) throws ServletException {
    this.config = config;
    context = config.getServletContext();
  }

  public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    String value = (String) context.getAttribute("foo");
    request.setAttribute("bar", value);
    chain.doFilter(request, response);
  }

  public void destroy() {
  }


}




> Thanks
>

Craig


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>