JavaWeb同步学习笔记之七十四、JavaWeb_字符编码过滤器

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值