返回值类型 | 方法 | 描述 |
---|---|---|
void | doFilter(ServletRequest request ,ServletResponse response) | 使用该方法可以调用过滤器链中的下一个 Filter 的 doFilter() 方法,若该 Filter 是链中最后一个过滤器,则调用目标资源的 service() 方法。 |
package net.biancheng.www.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author 编程帮 www.lmcjl.com */ @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("<h1>编程帮www.lmcjl.com 欢迎您</h1>"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package net.biancheng.www.filter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * 过滤器链 * * @author 编程帮 www.lmcjl.com */ public class FirstChainFilter implements Filter { public FirstChainFilter() { } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //设置向页面输出的格式与编码 response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); writer.write("FirstChainFilter 对请求进行处理<br/>"); chain.doFilter(request, response); writer.write("FirstChainFilter 对响应进行处理<br/>"); } public void init(FilterConfig fConfig) throws ServletException { } }
package net.biancheng.www.filter; import javax.servlet.*; import java.io.IOException; import java.io.PrintWriter; /** * 过滤器链 * * @author 编程帮 www.lmcjl.com */ public class SecondChainFilter implements Filter { public SecondChainFilter() { } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //设置向页面输出的格式与编码 response.setContentType("text/html;charset=UTF-8"); PrintWriter writer = response.getWriter(); writer.write("SecondChainFilter 对请求进行处理<br/>"); chain.doFilter(request, response); writer.write("SecondChainFilter 对响应进行处理<br/>"); } public void init(FilterConfig fConfig) throws ServletException { } }
<?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_4_0.xsd" version="4.0"> <!--过滤器链中FirstChainFilter配置 --> <filter> <filter-name>FirstChainFilter</filter-name> <filter-class>net.biancheng.www.filter.FirstChainFilter</filter-class> </filter> <filter-mapping> <filter-name>FirstChainFilter</filter-name> <url-pattern>/login</url-pattern> </filter-mapping> <!--过滤器链中SecondChainFilter配置 --> <filter> <filter-name>SecondChainFilter</filter-name> <filter-class>net.biancheng.www.filter.SecondChainFilter</filter-class> </filter> <filter-mapping> <filter-name>SecondChainFilter</filter-name> <url-pattern>/login</url-pattern> </filter-mapping> </web-app>
本文链接:http://task.lmcjl.com/news/13530.html