Servlet filter web.xml - class exception not found

I am having a problem setting up a 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 a specific class, but for some reason I am getting Classnotfoundexcetion. Can anyone help me?

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,

+2


source to share


6 answers


The filter-class parameter must be a Java class that must implement the Filter interface. Can you check if this is really with you?

Check out this example for guidance:

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



From the link above:

<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 {...
    }
 }

      

+3


source


Funny, this same problem just happened to me today while it was working yesterday. After research showed that nothing happened, I concluded that this is an eclipse environment issue:

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


The problem is gone!

+5


source


For a simple solution, try putting your file log.class

in this directory:

tomcat/webapps/[your app name]/WEB-INF/classes/test 

      

Create this directory if it doesn't exist.


How to find your .class file:

In most cases, the default for Eclipse will compile the source file ( .java

) to a file .class

whenever you save it, otherwise you may need to press CTRL-B (build) to force it to compile .class

.

Once this is done, you should be able to find the file log.class

in the Eclipse workspace at:[your workspace name]/[your eclipse project name]/bin/test/

The location of the workspace directory is usually displayed on the right when starting eclipse. For Windows 7 I think the default for directory Users

, WinXP and 2000 defaults to a custom folder Documents and Settings

.

As an alternative:

The Package Explorer should appear on the left side of the Eclipse window. It will contain the leaf log.java

in the package tree. If you right-click this sheet and then click Show In

> Navigator

, which will open the actual file structure in the Navigator view.

This view should have bin

node. Inside you should find a test

node and then inside where you will see log.class

. You can right click it to copy it and then just go to the directory I gave at the top and paste it.

Everything should be fine.

0


source


It might be something wrong in the source code of the associated class.

I recently happened to see the same problem. After a lot of research I found that the exception is coded in the class:

$ jad BaseController.class 
Parsing BaseController.class...The class file version is 51.0 (only 45.3, 46.0 and 47.0 are supported)
 Generating BaseController.jad
$ cat BaseController.jad 
// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
...
import HttpServletRequest;
import HttpServletResponse;
import HttpSession;
...
public class BaseController
{
    public BaseController()
    {
        throw new Error("Unresolved compilation problems: \n\tThe import javax.servlet.http.HttpServletRequest cannot be resolved\n\tThe import javax.servlet.http.HttpServletResponse cannot be resolved\n\tThe import javax.servlet.http.HttpSession cannot be resolved\n\tHttpSession cannot be resolved to a type\n\tHttpServletRequest cannot be resolved to a type\n\tHttpSession cannot be resolved to a type\n\tHttpSession cannot be resolved to a type\n\tHttpServletRequest cannot be resolved to a type\n\tHttpServletRequest cannot be resolved to a type\n\tHttpServletResponse cannot be resolved to a type\n");
    }
...

      

So after recompiling the source with the required libraries, the problem is resolved.

0


source


Once there was a problem, finally right click on the project -> Build project helped.

0


source


I assume you didn't copy it correctly to include it in the servlet classpath.

See here what constitutes the servlet classpath:

Controlling the classpath in servlets

-1


source







All Articles