You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Seth Ladd <se...@ehawaii.gov> on 2003/12/19 01:12:33 UTC

Way to get Reference to Servlet from Filter?

Hello,

I don't think this is possible, but I'm giving it a shot anyway. :)

I'd like to get ahold of a servlet reference from within a filter.  Is 
there a way?

I have a filter that creates objects and places them within the request 
scope, but it does it differently for each end-result servlet.  I'd like 
the filter to look at the URI, determine the servlet, and grab the 
servlet.  Then, it can either grab its init-params or just call a 
special method to get the data it needs.

Now, this idea might be broken to begin with.  The other idea is to just 
create a super class for the servlet and handle this base logic from 
there.  If I can use a filter, though, that would be great.

Any tips or hints?  I hope I wasn't too vague.

Thanks very much!
Seth


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


Re: Way to get Reference to Servlet from Filter?

Posted by Seth Ladd <se...@ehawaii.gov>.
Tim Funk wrote:
> You won't be able to get the servlet reference. A possibility is to 
> define the filter multiple times with different parameters. For example, 
> here is the same class name defined 4 times as 4 different filters.

Good idea.  Thanks for the helpful response.  Since I can't get a 
reference to the servlet, I've resorted to reading in the web.xml file 
itself to get any init-params I would need.

Thanks!
Seth


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


Re: Way to get Reference to Servlet from Filter?

Posted by Sean Dockery <us...@sbdconsultants.com>.
"I've got a fever... and the only cure is more cowbell!"

LOL.  :-D

"Tim Funk" <fu...@joedog.org> wrote in message
news:3FE2FC7F.2080409@joedog.org...
> You won't be able to get the servlet reference. A possibility is to define
> the filter multiple times with different parameters. For example, here is
the
> same class name defined 4 times as 4 different filters.
>
> <filter>
>    <filter-name>filter1</filter-name>
>    <filter-class>more.Cowbell</filter-class>
>    <init-param>
>      <param-name>my</param-name>
>      <param-value>attribute</param-value>
>    </init-param>
> </filter>
> <filter>
>    <filter-name>filter2</filter-name>
>    <filter-class>more.Cowbell</filter-class>
> </filter>
> <filter>
>    <filter-name>filter3</filter-name>
>    <filter-class>more.Cowbell</filter-class>
>    <init-param>
>      <param-name>another</param-name>
>      <param-value>some value</param-value>
>    </init-param>
> </filter>
> <filter>
>    <filter-name>filter4</filter-name>
>    <filter-class>more.Cowbell</filter-class>
> </filter>
>
> I'll leave it up to you to figure out the mappings according to your
constraints.
>
> -Tim
>




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


Re: Way to get Reference to Servlet from Filter?

Posted by Tim Funk <fu...@joedog.org>.
You won't be able to get the servlet reference. A possibility is to define 
the filter multiple times with different parameters. For example, here is the 
same class name defined 4 times as 4 different filters.

<filter>
   <filter-name>filter1</filter-name>
   <filter-class>more.Cowbell</filter-class>
   <init-param>
     <param-name>my</param-name>
     <param-value>attribute</param-value>
   </init-param>
</filter>
<filter>
   <filter-name>filter2</filter-name>
   <filter-class>more.Cowbell</filter-class>
</filter>
<filter>
   <filter-name>filter3</filter-name>
   <filter-class>more.Cowbell</filter-class>
   <init-param>
     <param-name>another</param-name>
     <param-value>some value</param-value>
   </init-param>
</filter>
<filter>
   <filter-name>filter4</filter-name>
   <filter-class>more.Cowbell</filter-class>
</filter>

I'll leave it up to you to figure out the mappings according to your constraints.

-Tim

Seth Ladd wrote:

> Hello,
> 
> I don't think this is possible, but I'm giving it a shot anyway. :)
> 
> I'd like to get ahold of a servlet reference from within a filter.  Is 
> there a way?
> 
> I have a filter that creates objects and places them within the request 
> scope, but it does it differently for each end-result servlet.  I'd like 
> the filter to look at the URI, determine the servlet, and grab the 
> servlet.  Then, it can either grab its init-params or just call a 
> special method to get the data it needs.
> 
> Now, this idea might be broken to begin with.  The other idea is to just 
> create a super class for the servlet and handle this base logic from 
> there.  If I can use a filter, though, that would be great.
> 
> Any tips or hints?  I hope I wasn't too vague.



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