页面乱码

在页面输入中文到后台变成乱码

1.在web.xml中添加编码集过滤器

<filter>
	<filter-name>EncodingFilter</filter-name>
	<filter-class>com.cloud.base.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>EncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>


过滤器
public class CharacterEncodingFilter implements Filter {

	/**
	 * 默认编码集
	 */
	private String encoding = "UTF-8";
	
	@Override
	public void destroy() {
		
		encoding = null;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		request.setCharacterEncoding(encoding);
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		
		String enc = filterConfig.getInitParameter("encoding");
		if (MethodUtil.isNotNull(enc)) {
			encoding = enc;
		}
	}
}


2.页面编码集设置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>


注:页面和过滤器的编码集必须保持一致

你可能感兴趣的:(java,xml,Web)