You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Marten Lehmann <le...@cnm.de> on 2005/07/26 15:35:20 UTC

attributes in servletcontext and servletrequest

Hello,

within the init() of a filter, I'm storing a reference to the 
servletContext:

filterConfig.getServletContext().setAttribute("servletContext", 
filterConfig.getServletContext());

But when I'm trying to access this attribute in the doFilter-method, I 
get a NullPointerException:

ServletContext servletContext = (ServletContext) 
request.getAttribute("servletContext");

Why doesn't this work?

Regards
Marten

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Re: attributes in servletcontext and servletrequest

Posted by David Smith <dn...@cornell.edu>.
A better way to handle this stuff is to just get the servlet context
when you need it and not store it in the request.  If you have access to
the request, you have access to the servlet context.

ie ServletContext servContext = request.getSession().getServletContext() ;

--David

Marten Lehmann wrote:

> Hello,
>
> within the init() of a filter, I'm storing a reference to the
> servletContext:
>
> filterConfig.getServletContext().setAttribute("servletContext",
> filterConfig.getServletContext());
>
> But when I'm trying to access this attribute in the doFilter-method, I
> get a NullPointerException:
>
> ServletContext servletContext = (ServletContext)
> request.getAttribute("servletContext");
>
> Why doesn't this work?
>
> Regards
> Marten
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>


-- 
=======================================
David Smith
Network Operations Supervisor
Department of Entomology
College of Agriculture & Life Sciences
Cornell University
2132 Comstock Hall
Ithaca, NY  14853
Phone: 607.255.9571
Fax: 607.255.0939



---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org