我是初学者,正在尝试部署日志过滤器程序,但它显示错误:
HTTP Status 404 - /LogFilter
type Status report
message /LogFilter
description The requested resource is not available.
Apache Tomcat/8.0.5
这是我运行时的 URL:http://localhost:9999/LogFilter
这是我的 LogFilter
源代码:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class LogFilter implements Filter{
public void init(FilterConfig config)throws ServletException{
String testParam=config.getInitParameter("test-param");
//here we are printing the testParam...
System.out.println("Test param: "+testParam);
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws ServletException, IOException{
String ipAddress=request.getRemoteAddr();
System.out.println("IP address: "+ipAddress+" Data: "+new Date().toString());
chain.doFilter(request, response);
}
public void destroy(){}
}
这是我的 web.xml
源代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>LogFilter</filter-name>
<filter-class>LogFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization Paramter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
最佳答案
过滤器提供了一种在 Java Web 应用程序中执行过滤功能的有用方法。通常,过滤器本身不会生成内容。
当您想要根据特定条件过滤和/或修改请求时,请使用过滤器。当您想要控制、预处理和/或后处理请求时,请使用 Servlet。
关于java - 为什么我的 Servlet Filter 程序显示 404 http 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23537424/