Servlet web.xml过滤器 - 未找到类异常

时间:2012-07-25 21:39:58

标签: java web-services jsp servlets web.xml

我在设置过滤器时遇到问题 - 我在web.xml中定义的过滤器看起来像这样

  <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>

但是我收到以下错误 -

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)

我已经定义了类,但出于某种原因我得到了Classnotfoundexcetion。有人可以帮助我吗?

编辑---

我有一个名为test的包和一个名为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*/
  }
 }

谢谢,

6 个答案:

答案 0 :(得分:5)

有趣的是,同样的问题今天发生在我昨天工作的时候。在调查显示没有错之后,我得出结论这是一个日食环境问题:

  • 停止tomcat服务器
  • 清理项目(项目 - &gt;清洁......)
  • 刷新整个项目(右键单击项目名称 - >刷新)
  • 再次启动服务器。

问题消失了!

答案 1 :(得分:3)

filter-class参数必须是应该实现Filter接口的Java类。你能检查一下你的情况吗?

请查看此示例以获取指导:

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

从以上链接:

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

答案 2 :(得分:0)

对于一个简单的解决方案,请尝试将log.class文件放入此目录:

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

如果该目录不存在,则创建该目录。

<小时/> 如何查找.class文件:

大多数情况下,Eclipse的默认设置会在您保存源文件时将源文件(.java)编译为.class文件,否则您可能必须按CTRL-B(构建)才能强制执行它编译成.class文件。

完成后,您将能够在Eclipse工作区中找到log.class文件:[your workspace name]/[your eclipse project name]/bin/test/

工作区目录位置通常在您启动eclipse时可见。对于Windows 7,我认为它默认为Users目录,WinXP和2000默认为用户的Documents and Settings文件夹。

可替换地:

您应该在Eclipse窗口的左侧有一个“Package Explorer”视图。它将在包树中包含log.java叶。如果右键单击该叶子,然后单击Show In&gt; Navigator将在“导航器”视图中打开实际的文件结构。

该视图中应该有bin个节点。在里面你应该找到一个test节点,然后在里面你会看到log.class。你可以右键单击它来复制它,然后只需导航到我在顶部给出的目录并粘贴它。

一切都应该好。

答案 3 :(得分:0)

相关类的源代码可能有问题。

最近我碰巧看到了同样的问题。 经过长时间的艰苦调查,我发现异常在类中编码:

$ 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");
    }
...

因此,在使用必要的lib重新编译源代码后,问题就解决了。

答案 4 :(得分:0)

遇到问题一次,最后右键单击项目-> 构建项目有所帮助。

答案 5 :(得分:-1)

我猜你没有正确复制它包含在servlet类路径中。

请参阅此处了解构成servlet类路径的内容:

Controlling the classpath in a servlet