我是初学者并尝试为Log fileter部署程序,但它显示错误:
HTTP Status 404 - /LogFilter
type Status report
message /LogFilter
description The requested resource is not available.
Apache Tomcat/8.0.5
以下是我的网址: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>
答案 0 :(得分:1)
过滤器提供了一种在Java Web应用程序中执行过滤功能的有用方法。通常,过滤器本身不会生成内容。
如果要根据特定条件过滤和/或修改请求,请使用过滤器。当您想要控制,预处理和/或后处理请求时,请使用Servlet。