JavaWeb_字符编码过滤器
字符编码过滤器
- 1.字符编码的过滤器
通过配置参数encoding指明使用何种字符编码,以处理Html Form请求参数的中文问题。 - 2.示例
a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="b.jsp" method="post">
name:<input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
b.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--
//编写一个 EncodingFilter
//1. 读取 web.xml 文件中配置的当前 WEB 应用的初始化参数 encoding
//2. 指定请求的字符编码为 1 读取到的编码
//3. 调用 chain.doFilter() 方法 "放行" 请求
request.setCharacterEncoding("UTF-8");
--%>
Hello,${param.name }
</body>
</html>
EncodingFilter.java
package com.xs.javaweb.encoding;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xs.javaweb.HttpFilter;
/**
* Servlet Filter implementation class EncodingFilter
*/
@WebFilter("/encoding/*")
public class EncodingFilter extends HttpFilter {
private String encoding;
/**
* <p>Title: init</p>
* <p>Description: </p>
* @see com.xs.javaweb.HttpFilter#init(javax.servlet.FilterConfig)
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getServletContext().getInitParameter("encoding");
}
/**
* <p>Title: doFilter</p>
* <p>Description: </p>
* @see com.xs.javaweb.HttpFilter#doFilter(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
* @param request
* @param response
* @param chain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println(encoding);
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
}
HttpFilter.java
/**
* All rights Reserved,Designed By XS
* @Title: HttpFilter.java
* @Package com.xs.javaweb
* @Description: TODO
* @author: XS
* @date: 2019年3月17日 上午10:05:01
* @version V1.0
*/
package com.xs.javaweb;
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;
/**
* @ClassName: HttpFilter
* @Description: TODO 自定义的HttpFilter,实现 Filter 接口
* @author: XS
* @date: 2019年3月17日 上午10:05:01
* @version V1.0
*/
public abstract class HttpFilter implements Filter {
// 用于保存FilterConfig对象
private FilterConfig FilterConfig;
/**
* <p>
* Title: init
* </p>
* <p>
* Description: 不建议子类直接覆盖,若直接覆盖,将可能会导致filterConfig成员变量初始化失败。
* </p>
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.FilterConfig = filterConfig;
init();
}
/**
* @Title: init
* @Description: TODO 供子类继承的初始化方法。可以通过getFilterConfig() 获取FilterConfig对象。
* @return void
*/
protected void init() {
}
/**
* @Title: getFilterConfig
* @Description: TODO 直接返回init(ServletConfig)的FilterConfig对象
* @return
* @return FilterConfig
*/
public FilterConfig getFilterConfig() {
return FilterConfig;
}
/**
* <p>
* Title: destroy
* </p>
* <p>
* Description: 空地destroy方法。
* </p>
*
* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
}
/**
* <p>
* Title: doFilter
* </p>
* <p>
* Description: 原生的doFilter方法,在方法内部把ServletRequest和ServletResponse
* 转为了HttpServletRequest和HttpServletResponse,并调用了 doFilter(HttpServletRequest
* request, HttpServletResponse response, FilterChain chain)
*
* 若编写Filter的过滤方法不建议直接继承该方法,而建议继承 doFilter(HttpServletRequest request,
* HttpServletResponse response, FilterChain chain) 方法
* </p>
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
* @param request
* @param response
* @param chain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
doFilter(request, response, chain);
}
/**
* @Title: doFilter
* @Description: TODO 抽象方法,为Http请求定制,必须实现的方法。
* @param request
* @param response
* @param chain
* @throws IOException
* @throws ServletException
* @return void
*/
public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException;
}
web.xml中添加以下信息
<context-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</context-param>