tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: Filters and ServletContext
Date Fri, 09 Nov 2001 01:05:19 GMT


On 8 Nov 2001, Dr. Evil wrote:

> Date: 8 Nov 2001 22:20:52 -0000
> From: Dr. Evil <drevil@sidereal.kz>
> Reply-To: Tomcat Users List <tomcat-user@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:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message