You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by joelsherriff <jo...@comcast.net> on 2005/04/28 15:36:32 UTC

How to use servlet filters without modifying webapp

Hello,
    I'm experimenting with applying a servlet filter to an existing webapp and I'm getting a ClassCastException upon startup.
Can I do this without modifying the webapp source and adding my filter in there?  If so, what else could be causing this?

I'm not sure where it looks for the filter .class file but I put it in the webapp's WEB-INF/classes directory - I guess it finds it
since I'm getting this error.  The filter really does nothing, I'm just trying to get A filter in place before making it more complicated.

Re: How to use servlet filters without modifying webapp

Posted by joelsherriff <jo...@comcast.net>.
And to save anyone the trouble, I also tried substituting the url-pattern
element for a servlet-name element and named the
servlet I'm trying to filter.  Same result - ClassCastException.  I get
nothing helpful from the log, but just in case someone
with more experience can, here's a snip:

2005-04-28 10:16:59 StandardContext[/blojsom]: Starting filters
2005-04-28 10:16:59 StandardContext[/blojsom]:  Starting filter
'timerFilter'
2005-04-28 10:16:59 StandardContext[/blojsom]: Exception starting filter
timerFilter
java.lang.ClassCastException
 at
org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilter
Config.java:206)
 at
org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFil
terConfig.java:280)
 at
org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterCon
fig.java:73)
 at
org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:31
01)


----- Original Message ----- 
From: "joelsherriff" <jo...@comcast.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, April 28, 2005 10:08 AM
Subject: Re: How to use servlet filters without modifying webapp


> I've done that, thanks.  Here's what I added for the filter:
>
>     <filter>
>         <filter-name>timerFilter</filter-name>
>         <filter-class>TimerFilter</filter-class>
>     </filter>
>
>     <filter-mapping>
>         <filter-name>timerFilter</filter-name>
>         <url-pattern>/*</url-pattern>
>     </filter-mapping>
>
> to my web.xml.  It appears to be finding it properly.  If I change the
> filter name to a non-existent filter, I
> properly get a ClassNotFoundException.  So it's something IN the filter,
or
> so it would seem.  All I've
> done is copy an example filter from the web - it looks like:
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class TimerFilter implements Filter {
>
>   private FilterConfig config = null;
>
>   public void init(FilterConfig config) throws ServletException {
>     this.config = config;
>   }
>
>   public void destroy() {
>     this.config = null;
>   }
>
>   public void doFilter(ServletRequest request, ServletResponse response,
>                      FilterChain chain) throws IOException,
ServletException
> {
>     long before = System.currentTimeMillis();
>     chain.doFilter(request, response);
>     long after = System.currentTimeMillis();
>
>     String name = "";
>     if (request instanceof HttpServletRequest) {
>       name = ((HttpServletRequest)request).getRequestURI();
>     }
>     config.getServletContext().log(name + ": " + (after - before) + "ms");
>   }
> }
>
>
>
>
> ----- Original Message ----- 
> From: "Anhony" <an...@comcast.net>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Thursday, April 28, 2005 9:48 AM
> Subject: Re: How to use servlet filters without modifying webapp
>
>
> > Greetings,
> >
> > Try adding a <filter> block to your web.xml. Your JSP container locates
> your
> > filters thru these sections in the web.xml. I included a small sample
> > <filter> block below.
> >
> > <filter>
> >      <filter-name>processingFilter</filter-name>
> >      <filter-class>servletFilters.ProcessingFilter</filter-class>
> > </filter>
> >
> > I hope this helps.
> >
> > Anthony-
> >
> >
> >
> > ----- Original Message ----- 
> > From: "joelsherriff" <jo...@comcast.net>
> > To: "Tomcat Users List" <to...@jakarta.apache.org>
> > Sent: Thursday, April 28, 2005 9:36 AM
> > Subject: How to use servlet filters without modifying webapp
> >
> >
> > Hello,
> >     I'm experimenting with applying a servlet filter to an existing
webapp
> > and I'm getting a ClassCastException upon startup.
> > Can I do this without modifying the webapp source and adding my filter
in
> > there?  If so, what else could be causing this?
> >
> > I'm not sure where it looks for the filter .class file but I put it in
the
> > webapp's WEB-INF/classes directory - I guess it finds it
> > since I'm getting this error.  The filter really does nothing, I'm just
> > trying to get A filter in place before making it more complicated.
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>



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


Re: How to use servlet filters without modifying webapp

Posted by joelsherriff <jo...@comcast.net>.
Thanks - that seemed to do the trick.

----- Original Message ----- 
From: "Robert r. Sanders" <ro...@ipov.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, April 28, 2005 10:26 AM
Subject: Re: How to use servlet filters without modifying webapp


> Try the following:
>
>     - Put you filter into a package, I've seen some versions of the JVM
> that really don't like non-package classes.
>     - Make sure you are using settings compatible with the JVM that
> Tomcat is running under when compiling the class.
>
> joelsherriff wrote:
>
> >I've done that, thanks.  Here's what I added for the filter:
> >
> >    <filter>
> >        <filter-name>timerFilter</filter-name>
> >        <filter-class>TimerFilter</filter-class>
> >    </filter>
> >
> >    <filter-mapping>
> >        <filter-name>timerFilter</filter-name>
> >        <url-pattern>/*</url-pattern>
> >    </filter-mapping>
> >
> >to my web.xml.  It appears to be finding it properly.  If I change the
> >filter name to a non-existent filter, I
> >properly get a ClassNotFoundException.  So it's something IN the filter,
or
> >so it would seem.  All I've
> >done is copy an example filter from the web - it looks like:
> >
> >import java.io.*;
> >import javax.servlet.*;
> >import javax.servlet.http.*;
> >
> >public class TimerFilter implements Filter {
> >
> >  private FilterConfig config = null;
> >
> >  public void init(FilterConfig config) throws ServletException {
> >    this.config = config;
> >  }
> >
> >  public void destroy() {
> >    this.config = null;
> >  }
> >
> >  public void doFilter(ServletRequest request, ServletResponse response,
> >                     FilterChain chain) throws IOException,
ServletException
> >{
> >    long before = System.currentTimeMillis();
> >    chain.doFilter(request, response);
> >    long after = System.currentTimeMillis();
> >
> >    String name = "";
> >    if (request instanceof HttpServletRequest) {
> >      name = ((HttpServletRequest)request).getRequestURI();
> >    }
> >    config.getServletContext().log(name + ": " + (after - before) +
"ms");
> >  }
> >}
> >
> >
> >
> >
> >----- Original Message ----- 
> >From: "Anhony" <an...@comcast.net>
> >To: "Tomcat Users List" <to...@jakarta.apache.org>
> >Sent: Thursday, April 28, 2005 9:48 AM
> >Subject: Re: How to use servlet filters without modifying webapp
> >
> >
> >
> >
> >>Greetings,
> >>
> >>Try adding a <filter> block to your web.xml. Your JSP container locates
> >>
> >>
> >your
> >
> >
> >>filters thru these sections in the web.xml. I included a small sample
> >><filter> block below.
> >>
> >><filter>
> >>     <filter-name>processingFilter</filter-name>
> >>     <filter-class>servletFilters.ProcessingFilter</filter-class>
> >></filter>
> >>
> >>I hope this helps.
> >>
> >>Anthony-
> >>
> >>
> >>
> >>----- Original Message ----- 
> >>From: "joelsherriff" <jo...@comcast.net>
> >>To: "Tomcat Users List" <to...@jakarta.apache.org>
> >>Sent: Thursday, April 28, 2005 9:36 AM
> >>Subject: How to use servlet filters without modifying webapp
> >>
> >>
> >>Hello,
> >>    I'm experimenting with applying a servlet filter to an existing
webapp
> >>and I'm getting a ClassCastException upon startup.
> >>Can I do this without modifying the webapp source and adding my filter
in
> >>there?  If so, what else could be causing this?
> >>
> >>I'm not sure where it looks for the filter .class file but I put it in
the
> >>webapp's WEB-INF/classes directory - I guess it finds it
> >>since I'm getting this error.  The filter really does nothing, I'm just
> >>trying to get A filter in place before making it more complicated.
> >>
> >>
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >>
> >>
> >>
> >>
> >
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
> >
> >
>
> -- 
>     Robert r. Sanders
>     Chief Technologist
>     iPOV
>     (334) 821-5412
>     www.ipov.net
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>



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


Re: How to use servlet filters without modifying webapp

Posted by "Robert r. Sanders" <ro...@ipov.net>.
Try the following:

    - Put you filter into a package, I've seen some versions of the JVM 
that really don't like non-package classes.
    - Make sure you are using settings compatible with the JVM that 
Tomcat is running under when compiling the class.

joelsherriff wrote:

>I've done that, thanks.  Here's what I added for the filter:
>
>    <filter>
>        <filter-name>timerFilter</filter-name>
>        <filter-class>TimerFilter</filter-class>
>    </filter>
>
>    <filter-mapping>
>        <filter-name>timerFilter</filter-name>
>        <url-pattern>/*</url-pattern>
>    </filter-mapping>
>
>to my web.xml.  It appears to be finding it properly.  If I change the
>filter name to a non-existent filter, I
>properly get a ClassNotFoundException.  So it's something IN the filter, or
>so it would seem.  All I've
>done is copy an example filter from the web - it looks like:
>
>import java.io.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>
>public class TimerFilter implements Filter {
>
>  private FilterConfig config = null;
>
>  public void init(FilterConfig config) throws ServletException {
>    this.config = config;
>  }
>
>  public void destroy() {
>    this.config = null;
>  }
>
>  public void doFilter(ServletRequest request, ServletResponse response,
>                     FilterChain chain) throws IOException, ServletException
>{
>    long before = System.currentTimeMillis();
>    chain.doFilter(request, response);
>    long after = System.currentTimeMillis();
>
>    String name = "";
>    if (request instanceof HttpServletRequest) {
>      name = ((HttpServletRequest)request).getRequestURI();
>    }
>    config.getServletContext().log(name + ": " + (after - before) + "ms");
>  }
>}
>
>
>
>
>----- Original Message ----- 
>From: "Anhony" <an...@comcast.net>
>To: "Tomcat Users List" <to...@jakarta.apache.org>
>Sent: Thursday, April 28, 2005 9:48 AM
>Subject: Re: How to use servlet filters without modifying webapp
>
>
>  
>
>>Greetings,
>>
>>Try adding a <filter> block to your web.xml. Your JSP container locates
>>    
>>
>your
>  
>
>>filters thru these sections in the web.xml. I included a small sample
>><filter> block below.
>>
>><filter>
>>     <filter-name>processingFilter</filter-name>
>>     <filter-class>servletFilters.ProcessingFilter</filter-class>
>></filter>
>>
>>I hope this helps.
>>
>>Anthony-
>>
>>
>>
>>----- Original Message ----- 
>>From: "joelsherriff" <jo...@comcast.net>
>>To: "Tomcat Users List" <to...@jakarta.apache.org>
>>Sent: Thursday, April 28, 2005 9:36 AM
>>Subject: How to use servlet filters without modifying webapp
>>
>>
>>Hello,
>>    I'm experimenting with applying a servlet filter to an existing webapp
>>and I'm getting a ClassCastException upon startup.
>>Can I do this without modifying the webapp source and adding my filter in
>>there?  If so, what else could be causing this?
>>
>>I'm not sure where it looks for the filter .class file but I put it in the
>>webapp's WEB-INF/classes directory - I guess it finds it
>>since I'm getting this error.  The filter really does nothing, I'm just
>>trying to get A filter in place before making it more complicated.
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>>
>>
>>    
>>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>  
>

-- 
    Robert r. Sanders
    Chief Technologist
    iPOV
    (334) 821-5412
    www.ipov.net


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


Re: How to use servlet filters without modifying webapp

Posted by joelsherriff <jo...@comcast.net>.
I've done that, thanks.  Here's what I added for the filter:

    <filter>
        <filter-name>timerFilter</filter-name>
        <filter-class>TimerFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>timerFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

to my web.xml.  It appears to be finding it properly.  If I change the
filter name to a non-existent filter, I
properly get a ClassNotFoundException.  So it's something IN the filter, or
so it would seem.  All I've
done is copy an example filter from the web - it looks like:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TimerFilter implements Filter {

  private FilterConfig config = null;

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

  public void destroy() {
    this.config = null;
  }

  public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException
{
    long before = System.currentTimeMillis();
    chain.doFilter(request, response);
    long after = System.currentTimeMillis();

    String name = "";
    if (request instanceof HttpServletRequest) {
      name = ((HttpServletRequest)request).getRequestURI();
    }
    config.getServletContext().log(name + ": " + (after - before) + "ms");
  }
}




----- Original Message ----- 
From: "Anhony" <an...@comcast.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, April 28, 2005 9:48 AM
Subject: Re: How to use servlet filters without modifying webapp


> Greetings,
>
> Try adding a <filter> block to your web.xml. Your JSP container locates
your
> filters thru these sections in the web.xml. I included a small sample
> <filter> block below.
>
> <filter>
>      <filter-name>processingFilter</filter-name>
>      <filter-class>servletFilters.ProcessingFilter</filter-class>
> </filter>
>
> I hope this helps.
>
> Anthony-
>
>
>
> ----- Original Message ----- 
> From: "joelsherriff" <jo...@comcast.net>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Thursday, April 28, 2005 9:36 AM
> Subject: How to use servlet filters without modifying webapp
>
>
> Hello,
>     I'm experimenting with applying a servlet filter to an existing webapp
> and I'm getting a ClassCastException upon startup.
> Can I do this without modifying the webapp source and adding my filter in
> there?  If so, what else could be causing this?
>
> I'm not sure where it looks for the filter .class file but I put it in the
> webapp's WEB-INF/classes directory - I guess it finds it
> since I'm getting this error.  The filter really does nothing, I'm just
> trying to get A filter in place before making it more complicated.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>



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


Re: How to use servlet filters without modifying webapp

Posted by Anhony <an...@comcast.net>.
Greetings,

Try adding a <filter> block to your web.xml. Your JSP container locates your 
filters thru these sections in the web.xml. I included a small sample 
<filter> block below.

<filter>
     <filter-name>processingFilter</filter-name>
     <filter-class>servletFilters.ProcessingFilter</filter-class>
</filter>

I hope this helps.

Anthony-



----- Original Message ----- 
From: "joelsherriff" <jo...@comcast.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Thursday, April 28, 2005 9:36 AM
Subject: How to use servlet filters without modifying webapp


Hello,
    I'm experimenting with applying a servlet filter to an existing webapp 
and I'm getting a ClassCastException upon startup.
Can I do this without modifying the webapp source and adding my filter in 
there?  If so, what else could be causing this?

I'm not sure where it looks for the filter .class file but I put it in the 
webapp's WEB-INF/classes directory - I guess it finds it
since I'm getting this error.  The filter really does nothing, I'm just 
trying to get A filter in place before making it more complicated. 



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