我在 Tomcat 7 中的过滤器实现存在问题。我有欢迎页面index.jsp
。没有过滤器它工作正常。但是当我向它添加一个小过滤器时,我得到了404-Not Found
错误。
我有一个过滤器类(在 /WEB-INF/classes/filt/Background.java 中),如下所示:
package filt;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.*;
import java.util.*;
import java.net.*;
public class Background implements Filter {
private FilterConfig filterConfig;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
try
{
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
filterChain.doFilter(request, response);
}
public FilterConfig getFilterConfig() {
return filterConfig;
}
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
public void destroy() {}
}
我的 web.xml :
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee">
<application>insights</application>
<version>1</version>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Backgd</filter-name>
<filter-class>filt.Background</filter-class>
</filter>
<filter-mapping>
<filter-name>Backgd</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
</web-app>
来自日志的错误:
SEVERE: Exception starting filter Backgd
java.lang.ClassNotFoundException: filt.Background
即使现在,如果我删除 web.xml 中的过滤器部分,它也能正常工作。问题出在过滤器上。
是什么导致错误?过滤器实现中的任何错误?