Filter中对字符编码过滤解决中文乱码问题

时间:2021-05-12 18:25:32

1:a.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="b.jsp" method="post">
name:<input type="text" name="name"/><br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>


2:b.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- <%
  request.setCharacterEncoding("UTF-8");
%> --%>
Hello:${param.name}<br/>
</body>
</html>


 3:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Filter</display-name>
  <context-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </context-param>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>helloFilter</filter-name>
    <filter-class>com.filter.HelloFilter</filter-class>
    <init-param>
      <param-name>name</param-name>
      <param-value>root</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>helloFilter</filter-name>
    <url-pattern>/test.jsp</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>secondFilter</filter-name>
    <filter-class>com.filter.SecondFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>secondFilter</filter-name>
    <url-pattern>/test.jsp</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>
  <filter>
    <filter-name>UserNameFilter</filter-name>
    <filter-class>com.filter.UserNameFilter</filter-class>
    <init-param>
      <param-name>username</param-name>
      <param-value>Tom</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>UserNameFilter</filter-name>
    <url-pattern>/hello.jsp</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>PasswordFilter</filter-name>
    <filter-class>com.filter.PasswordFilter</filter-class>
    <init-param>
      <param-name>password</param-name>
      <param-value>12345</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>PasswordFilter</filter-name>
    <url-pattern>/hello.jsp</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>NOCacheFilter</filter-name>
    <filter-class>com.filter.NOCacheFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>NOCacheFilter</filter-name>
    <url-pattern>/a.jsp</url-pattern>
  </filter-mapping>
  <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.filter.EncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/encoding/*</url-pattern>
  </filter-mapping>
</web-app>


  4:HttpFilter

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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 自定义的HttpFilter,需要实现Filter接口
 * @author Administrator
 *
 */
public abstract class HttpFilter implements Filter {
private FilterConfig filterConfig=null;
    @Override
public void destroy() {
  
}
    @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
    doFilter(req, res,chain);
}
    public abstract void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain)throws IOException, ServletException;
    
    @Override
public void init(FilterConfig fConfig) throws ServletException {
this.filterConfig=fConfig;
init();
}
    /**
     * 通过调用getFilterConfig()方法
     */
   protected void init()
   {   
   }
   public FilterConfig getFilterConfig()
   {
  return filterConfig;
   }
}


 5:Encoding

package com.filter;


import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class EncodingFilter extends HttpFilter {
  private String encoding;
    @Override
   protected void init()
   {
 encoding=getFilterConfig().getServletContext().getInitParameter("encoding");
   }
@Override
public void doFilter(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}


}