java代码:
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HelloServlet");
}
}
package com.filter;
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;
public class HelloFilter implements Filter {
public void destroy() {
System.out.println("HelloFilter.destroy");
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("HelloFilter.doFilter before");
chain.doFilter(request, response);
System.out.println("HelloFilter.doFilter after");
}
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("HelloFilter.init");
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>servlet-filter</display-name>
<welcome-file-list>
<welcome-file>HelloServlet</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>HelloServlet</display-name>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<filter>
<display-name>HelloFilter</display-name>
<filter-name>HelloFilter</filter-name>
<filter-class>com.filter.HelloFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HelloFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
输出:
2014-5-5 22:38:09 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_05\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jdk1.6.0_05/bin/../jre/bin/client;C:/Program Files/Java/jdk1.6.0_05/bin/../jre/bin;C:/Program Files/Java/jdk1.6.0_05/bin/../jre/lib/i386;F:\software\apache-maven\apache-maven-3.2.1\bin;C:\Program Files\Java\jdk1.6.0_05\bin;C:\Program Files\Java\jdk1.6.0_05\jre\bin;C:\oracle\product\10.2.0\db_1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Rational\common;F:\software\java\eclipse-jee-juno-SR2-win32\eclipse;
2014-5-5 22:38:09 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:servlet-filter' did not find a matching property.
2014-5-5 22:38:10 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2014-5-5 22:38:10 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 601 ms
2014-5-5 22:38:10 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2014-5-5 22:38:10 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.29
HelloFilter.init
2014-5-5 22:38:10 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2014-5-5 22:38:10 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2014-5-5 22:38:10 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/47 config=null
2014-5-5 22:38:10 org.apache.catalina.startup.Catalina start
信息: Server startup in 494 ms
HelloFilter.doFilter before
HelloServlet
HelloFilter.doFilter after
看到:
HelloFilter.doFilter before
HelloServlet
HelloFilter.doFilter after