1.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>filter</display-name> <filter> <filter-name>filter01</filter-name> <filter-class>com.myfilter.Filter01</filter-class> <init-param> <param-name>code</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>filter01</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>filter02</filter-name> <filter-class>com.myfilter.Filter02</filter-class> </filter> <filter-mapping> <filter-name>filter02</filter-name> <url-pattern>/pages/mainFilter.jsp</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
web.xml
2.过滤器1解析字符编码
/** * */ package com.myfilter; 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; /** * @author Administrator * */ public class Filter01 implements Filter { private String encoding=null; @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest)arg0; HttpServletResponse response=(HttpServletResponse)arg1; request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); response.setContentType("text/html charset="+encoding); arg2.doFilter(request, response); System.out.println(encoding); } @Override public void init(FilterConfig arg0) throws ServletException { String ww=arg0.getInitParameter("code"); if(StringUtils.isNoneBlank(ww)) { encoding=ww; } } }
Filter01.java
3.过滤器2
package com.myfilter; 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 Filter02 implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("第二个过滤器----00001"); arg2.doFilter(arg0, arg1); System.out.println("第二个过滤器----00002"); } @Override public void init(FilterConfig arg0) throws ServletException { System.out.println("02"); } }
Filter02.java
时间: 2024-10-14 09:34:42