Solution for Garbled text when jquery. form submits Chinese characters (GBK Garbled text)

Source: Internet
Author: User

When jsp is used, Chinese garbled characters may occur accidentally. You can use the following methods to solve the problem:

Solution 1:

From jsp to servlet, UTF-8 encoding can be used in a unified manner. Using UTF-8 encoding can save a lot of trouble, but one disadvantage is that UTF-8 encoding uses 3-4 bytes for Chinese characters, which will increase the amount of network transmission.

Method 2:

1. Use GBK for jsp pages

2. Use the servlet filter to set request. setCharacterEncoding ("GBK"). google will find a lot of ways to use the filter conversion encoding.

The above two methods can solve most of the garbled issues, especially the first method, can solve the problem of Chinese garbled characters when ajax is used for submission. If the second method is used
The form still contains Chinese garbled characters. This is because js uses UTF-8 encoding when submitting ajax. The filter uses gbk for transcoding is incorrect. UTF-8 should be used for transcoding.
To solve this problem, there are also many versions on the Internet, one of which is the one I used to use encodeURI on the client and then perform
URLDecoder. decode. This scheme is feasible in occasionally Processing Chinese characters. However, if there is a large amount of data input on the page, this scheme is not feasible. Is there a good solution?
What about it? After a new round of google, I found a method. Reference http://www.javaeye.com

/Topic/157698? Page = 1

. The principle is to identify ajax requests or common requests based on the content in httpheader. In jquery1.2.6, the default contentType of ajaxSettings is set to "application/x-www-form

-Urlencoded ",
Set xhr. setRequestHeader ("X-Requested-",
"XMLHttpRequest"); In the servlet, you only need to determine whether the request was initiated through xmlhttprequest based on the two parameter values. It is worth
Jquery
Before submitting the form
The parameter is called by encodeURIComponent. See the param method.
JQuery. each (a, function () {<br/> s. push (encodeURIComponent (this. name) + "=" + encodeURIComponent (this. value); <br/>}); <br/>

jQuery.each( a, function(){s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );});

So what we pass to servlet is UTF-8 encoding, so we must use UTF-8 for transcoding in the filter.

The filter is modified. In IE and chrome, the value of request. getContentType () is "application/x-www-form

-Urlencoded ", but in firefox, the value is" application/x-www-form

-Urlencoded; charset = UTF-8 ", not very clear why ff is this value.

The filter code is as follows:
Package com. ajax. demo. action; </p> <p> import java. io. IOException; </p> <p> import javax. servlet. filter; <br/> import javax. servlet. filterChain; <br/> import javax. servlet. filterConfig; <br/> import javax. servlet. servletException; <br/> import javax. servlet. servletRequest; <br/> import javax. servlet. servletResponse; <br/> import javax. servlet. http. httpServletRequest; <br/> import javax. servlet. http. httpServletResponse; </p> <p> public class AjaxPostFilter implements Filter {</p> <p> private static final String IE_CONTENT_TYPE = "application/x-www-form-urlencoded "; <br/> private static final String FF_AJAX_CONTENT_TYPE = "application/x-www-form-urlencoded; charsets = UTF-8"; <br/> private static final String XMLHTTP_REQUEST = "XMLHttpRequest "; <br/> private static final String AJAX_CHARACTER_ENCODING_UTF8 = "UTF-8"; </p> <p> public void destroy () {<br/> // TODO Auto-generated method stub </p> <p >}</p> <p> public void doFilter (ServletRequest servletRequest, <br/> ServletResponse servletResponse, FilterChain filterChain) <br/> throws IOException, ServletException {<br/> HttpServletRequest request = (HttpServletRequest) servletRequest; <br/> HttpServletResponse response = (HttpServletResponse) servletResponse; <br/> String requestedWith = request. getHeader ("x-requested-with"); <br/> String type = request. getContentType (); <br/> if (XMLHTTP_REQUEST.equals (requestedWith) & (types) <br/> | IE_CONTENT_TYPE.equals (type) {<br/> request. setCharacterEncoding (AJAX_CHARACTER_ENCODING_UTF8); <br/> response. setCharacterEncoding (AJAX_CHARACTER_ENCODING_UTF8); <br/> // request. getParameterMap (); <br/>}< br/> filterChain. doFilter (request, response); </p> <p >}</p> <p> public void init (FilterConfig arg0) throws ServletException {<br/> // TODO Auto-generated method stub </p> <p >}< br/>

package com.ajax.demo.action;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;public class AjaxPostFilter implements Filter {private static final String IE_CONTENT_TYPE = "application/x-www-form-urlencoded";private static final String FF_AJAX_CONTENT_TYPE = "application/x-www-form-urlencoded; charset=UTF-8";private static final String XMLHTTP_REQUEST = "XMLHttpRequest";private static final String AJAX_CHARACTER_ENCODING_UTF8 = "UTF-8";public void destroy() {// TODO Auto-generated method stub}public void doFilter(ServletRequest servletRequest,ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;String requestedWith = request.getHeader("x-requested-with");String type = request.getContentType();if (XMLHTTP_REQUEST.equals(requestedWith)&& (FF_AJAX_CONTENT_TYPE.equals(type)||IE_CONTENT_TYPE.equals(type))) {request.setCharacterEncoding(AJAX_CHARACTER_ENCODING_UTF8);response.setCharacterEncoding(AJAX_CHARACTER_ENCODING_UTF8);//request.getParameterMap();}filterChain.doFilter(request, response);}public void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}}

Web. xml configuration, I use struts
<Filter> <br/> <filter-name> ajaxEncodeFilter </filter-name> <br/> <filter-class> com. ajax. demo. action. ajaxPostFilter </filter-class> <br/> </filter> <br/> <filter-mapping> <br/> <filter-name> ajaxEncodeFilter </filter-name> <br/> <url-pattern> *. do </url-pattern> <br/> </filter-mapping> <br/> <filter-name> ajaxEncodeFilter </filter- name> <br/> <url-pattern> *. jsp </url-pattern> <br/> </filter-mapping> <br/>

<filter><filter-name>ajaxEncodeFilter</filter-name><filter-class>com.ajax.demo.action.AjaxPostFilter</filter-class></filter><filter-mapping><filter-name>ajaxEncodeFilter</filter-name><url-pattern>*.do</url-pattern></filter-mapping><filter-mapping><filter-name>ajaxEncodeFilter</filter-name><url-pattern>*.jsp</url-pattern></filter-mapping>

This filter should be followed by your EncodeFilter. I tried it before royal Max said.

After this setting, no Chinese garbled characters will exist when jsp uses gbk to submit via ajax.

When chrome is used for testing, we also find a strange problem in chrome. For the returned results, we use jquery

. Ajax Processing
Success: function showResponse (responseText, statusText) {<br/> // The name here is the input text id. If "aa" is before, the value of name is changed to aa + returned result <br/> $ ('# name '). val ("aa" + responseText); <br/> // if it is changed to $ ('# name '). val (responseText + "aa"), the name value is still // responseText, and "aa" is not added to the end. I don't know why <br/>}

 

Blog Source: http://jasin2008.javaeye.com/blog/312854

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.