延伸阅读:
今天在找了一天EXT提交表单乱码问题,解决方案N多,均无效:
1、前后台所有文件统一用utf-8编码方式。
2、在Request Headers中设置Content-Type:application/x-www-form-urlencoded; charset=utf-8
3、在Response Headers中设置Content-Type:text/json;charset=utf-8,在JAVA中实现方法
但是如果你也试过如上方法,也不好用的话,可以试试下面的方法,问题原因及解决思路:
从Tomcat5.x开始,GET和POST方法提交的信息,Tomcat采用了不同的方式来处理编码,
对于POST请求,Tomcat会仍然使用request.setCharacterEncoding方法所设置的编码来处理,如果未设置,则使用默认的ISO-8859-1编码。
GET请求则不同,Tomcat对于GET请求并不会考虑使用request.setCharacterEncoding方法设置的编码,而会永远使用iso-8859-1编码
所以,修改Tomcat的POST请求时参数的编码,由ISO-8859-1到UTF-8。
1.过滤器 (包含get与post方式)SetCharacterEncodingFilter.java
package com.qingfeng.filter; 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.UnavailableException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class SetCharacterEncodingFilter implements Filter { /** * * 对 HttpServletRequestWrapper 进行扩充, 不影响原来的功能并能提供所有的 HttpServletRequest * 接口中的功能. 它可以统一的对 Tomcat 默认设置下的中文问题进行解决而只需要用新的 Request 对象替换页面中的 * request 对象即可. */ protected String encoding = null; protected FilterConfig filterConfig = null; //protected boolean ignore = true;
public void destroy() { this.encoding = null; this.filterConfig = null; } class Request extends HttpServletRequestWrapper { public Request(HttpServletRequest request) { super(request); } /** * 转换由表单读取的数据的内码. * 从 ISO 字符转到 utf-8(或gbk). */ public String encoding(String input) { //这个是核心方法!! try { byte[] bytes = input.getBytes("ISO-8859-1"); return new String(bytes, encoding); } catch (Exception ex) { } return null; } /** * Return the HttpServletRequest holded by this object. */ private HttpServletRequest getHttpServletRequest() { return (HttpServletRequest)super.getRequest(); } /** * 读取参数 -- 修正了中文问题. */ public String getParameter(String name) { //核心方法调用 return encoding(getHttpServletRequest().getParameter(name)); } /** * 读取参数列表 - 修正了中文问题. */ public String[] getParameterValues(String name) { String values[] = getHttpServletRequest().getParameterValues(name); if (values != null) { for (int i = 0; i < values.length; i++) { values[i] = encoding(values[i]); //核心方法调用 } } return values; } } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // if (ignore || (request.getCharacterEncoding() == null)) {
// String encoding = selectEncoding(request);
/*if (encoding != null) request.setCharacterEncoding(encoding);*/ // }
HttpServletRequest httpreq = (HttpServletRequest)request; if(httpreq.getMethod().equals("POST")) { request.setCharacterEncoding(encoding); } else { request = new Request(httpreq); } // Pass control on to the next filter
chain.doFilter(request, response); } /** * Place this filter into service. * * @param filterConfig The filter configuration object */ public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); } }
|
2.web.xml 中增加过滤器配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_4.xsd"> <filter> <filter-name>encodingfilter</filter-name> <filter-class>com.filter.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/jsp/encoding/*</url-pattern> </filter-mapping> <servlet> <description>changecode</description> <display-name>changecode</display-name> <servlet-name>CodeServlet</servlet-name> <servlet-class>com.filter.CodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CodeServlet</servlet-name> <url-pattern>/servlet/CodeServlet</url-pattern> </servlet-mapping> </web-app>
|
3.测试页面
(1) test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'test.jsp' starting page</title> </head> <body> <form action="action.jsp"> GET 方式: <input type=text name="name" value="GET 中文"> <input type=submit> </form> <form action="action.jsp" method="POST"> POST 方式: <input type=text name="name" value="POST 中文"> <input type=submit> </form> <form action="/Test_2/servlet/CodeServlet"> GET 方式: <input type=text name="name" value="GET 中文"> <input type=submit> </form> <form action="/Test_2/servlet/CodeServlet" method="POST"> POST 方式: <input type=text name="name" value="POST 中文"> <input type=submit> </form> </body> </html>
|
(2)action.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'action.jsp' starting page</title> </head> 参数: <%=request.getParameter("name") %> <br> </body> </html>
|
(3)CodeServlet.java
package com.filter.codetest; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CodeServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); System.out.println("get方式:"+name); } /** * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); System.out.println("post方式:"+name); } }
|
阅读(2762) | 评论(0) | 转发(0) |