【get/post】方式的中文乱码
2012-10-29 19:20
393 查看
首先:pageEncoding,contentType,request.setCharacterEncoding();区别。
1.pageEncoding:表示为jsp在编译成java文件时候应该采取的何种编码方式。如果为gbk则会根据gbk的编码为java文件
2.contentType:表示为输出浏览器的html的编码方式。也会为表单提交时要采用的编码方式。如果为utf-8则会用utf-8的编码方式传递表单参数
3. request.setCharacterEncoding();:表示设置请求的参数的编码方式(只会对post方式有效,get方式无效)。
get方式:
如用get方式提交表单,只需要在Tomcat的server.xml文件中添加一句"URLEcoding="UTF-8""即可!
因为它会将请求的url进行utf-8的编码。
post方式:
如果为post方式提交表单,每个jsp的contentType=“text/html;charset=utf-8”必须这样设置
还有就是处理请求的jsp中必须request.setCharacterEncoding("utf-8");(要与contentType中的设置的一致)。
如果程序里则必须String str = new String(name.getBytes("ISO-8859-1"),"utf-8");进行转码。
过滤器方法:
1.pageEncoding:表示为jsp在编译成java文件时候应该采取的何种编码方式。如果为gbk则会根据gbk的编码为java文件
2.contentType:表示为输出浏览器的html的编码方式。也会为表单提交时要采用的编码方式。如果为utf-8则会用utf-8的编码方式传递表单参数
3. request.setCharacterEncoding();:表示设置请求的参数的编码方式(只会对post方式有效,get方式无效)。
get方式:
如用get方式提交表单,只需要在Tomcat的server.xml文件中添加一句"URLEcoding="UTF-8""即可!
因为它会将请求的url进行utf-8的编码。
post方式:
如果为post方式提交表单,每个jsp的contentType=“text/html;charset=utf-8”必须这样设置
还有就是处理请求的jsp中必须request.setCharacterEncoding("utf-8");(要与contentType中的设置的一致)。
如果程序里则必须String str = new String(name.getBytes("ISO-8859-1"),"utf-8");进行转码。
过滤器方法:
package com.accphr.util; import java.io.IOException; import java.util.Iterator; import java.util.Map; 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; /** * * 字符集过滤器 * */ public class CharacterEncodingFilter implements Filter { /* 设置的字符集 */ private String encoding = "UTF-8"; public void destroy() { } @SuppressWarnings("unchecked") public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 处理服务器到客户端 response.setContentType("text/html;charset=" + this.encoding); // 处理客户端到服务器 HttpServletRequest req = (HttpServletRequest) request; String s = req.getMethod();// 获得表单提交方式 if (s.equalsIgnoreCase("post")) { req.setCharacterEncoding(this.encoding); } else if (s.equalsIgnoreCase("get")) { Map<String, String[]> map = (Map<String, String[]>) req.getParameterMap(); Iterator<String[]> it = map.values().iterator(); while (it.hasNext()) { String[] paramValues = it.next(); for(int i = 0; i < paramValues.length; i++) { byte[] b = paramValues[i].getBytes("ISO-8859-1"); paramValues[i] = new String(b, this.encoding); } } } chain.doFilter(request, response); } public void init(FilterConfig config) throws ServletException { // String encoding = config.getInitParameter("encoding"); // if (StringUtils.isNotBlank(encoding)) { // this.encoding = StringUtils.trimToBlank(encoding); } } }
相关文章推荐
- 解决servlet中文乱码问题及get、post请求方式的讲解
- http协议4---GET方式和POST方式获取表单数据举例3(统一方式获取,解决中文乱码问题)
- java中form以post、get方式提交数据中文乱码问题总结
- JSP中解决中文乱码问题 对于post有效 对于get请求方式无效。
- java中form以post、get方式提交数据中文乱码问题总结
- GET&POST方式解决中文乱码问题
- request的中文乱码get、post处理方式
- 在jsp编程中,用get提交表单提交正常,而用post方式出现中文乱码-问题
- 总结:SpringMVC 中 GET 和 POST 方式请求中的中文乱码问题
- java中form以post、get方式提交数据中文乱码问题总结
- post方式与get方式提交到Jsp页面中文乱码的处理
- 过滤器解决get和post方式提交的中文乱码
- JSP页面post和get方式提交请求,请求参数出现中文乱码
- 终结了form表单的get和post提交方式的中文乱码现象!
- 解决用jquery的get或者post提交方式提交到struts2中的action中文乱码问题
- get和post方式提交中文乱码问题解决方案
- 找到一篇关于form以post、get方式提交数据中文乱码问题总结
- request请求post、get方式中文乱码处理
- 解决中文乱码的filter(包括get和post提交两种方式)
- post跟get方式中文乱码以及解决方法