Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet web.xml filter -- Class not found exception

I am having a problem setting up the filter - the filter I have defined in web.xml looks like this

  <filter>
   <filter-name>Log</filter-name>
   <filter-class>test.log</filter-class> 
 </filter>
 <filter-mapping>
   <filter-name>Log</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

But I am getting the following error -

SEVERE: Exception starting filter Log
java.lang.ClassNotFoundException: test.log
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:133)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:256)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:103)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

I have the class defined but for some reason I am getting the Classnotfoundexcetion. Can someone help me out?

EDIT ---

I have a package called test and a class called log.

             package test;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;

    // Implements Filter class
     public class log implements Filter  {
         public void  init(FilterConfig config) {

          }
         public void  doFilter(ServletRequest request, 
             ServletResponse response,
             FilterChain chain) 
             throws java.io.IOException, ServletException {

  // Get the IP address of client machine.   
  String ipAddress = request.getRemoteAddr();

  // Log the IP address and current timestamp.
  System.out.println("IP "+ ipAddress + ", Time "
                                   + new Date().toString());

  // Pass request back down the filter chain
  chain.doFilter(request,response);
  }
   public void destroy( ){
      /* Called before the Filter instance is removed 
      from service by the web container*/
  }
 }

Thank You,

like image 562
Fox Avatar asked Jul 12 '26 14:07

Fox


2 Answers

Funny enough, this same issue just happened to me today while it was working on yesterday. After investigation showing nothing was wrong, I concluded this was an eclipse environment issue:

  • Stop the tomcat server
  • Cleaned the project (Project->Clean...)
  • Refresh the whole project (right click on project name -> Refresh)
  • Start the server on again.

Issue gone!

like image 73
eric A Avatar answered Jul 15 '26 05:07

eric A


The filter-class parameter needs to be a Java class that should implement the Filter interface. Can you check if that's the case with you?

Check this example for guidance:

http://viralpatel.net/blogs/tutorial-java-servlet-filter-example-using-eclipse-apache-tomcat/

From the above link:

<filter>
    <filter-name>LogFilter</filter-name>
    <filter-class>
        net.viralpatel.servlet.filters.LogFilter
    </filter-class>
    <init-param>
        <param-name>test-param</param-name>
        <param-value>This parameter is for testing.</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>LogFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>



public class LogFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {...
    }
 }
like image 42
Sid Avatar answered Jul 15 '26 03:07

Sid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!