解决post方式传递中文时乱码问题

为了避免中文乱码问题,需要在web.xml文件中增加编码过滤器,同时JSP页面编码设置为UTF-8,form表单的提交方式必须为post。

以下提供三种不同的解决方案:

 

一、SSM框架中:使用spring自带的编码过滤器

 
	CharacterEncodingFilter 
	org.springframework.web.filter.CharacterEncodingFilter 
	 
		encoding 
		utf-8 
	 
 
 
	CharacterEncodingFilter 
	/* 

 

二、Servlet中:使用自己编写的编码过滤器

web.xml


	EncodingFilter
	EncodingFilter
	jdbc.util.EncodingFilter
	
		pro_encoding
		UTF-8
	


	EncodingFilter
	/*

EncodingFilter.java

public class EncodingFilter implements Filter {
	private String encoding = null;

	public EncodingFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		System.out.println("encoding filter UTF-8");
		request.setCharacterEncoding(encoding);
		response.setCharacterEncoding(encoding);
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
		encoding = fConfig.getInitParameter("pro_encoding");
	}
}

 

三、每次用到时手动转码:

String str = new String(request.getParameter("user").getBytes("iso-8859-1"), "UTF-8");

 

你可能感兴趣的:(编程语言)