Java Chinese garbled solution (8) ----- solve the URL Chinese garbled problem, javaurl

Source: Internet
Author: User

Java Chinese garbled solution (8) ----- solve the URL Chinese garbled problem, javaurl

We mainly send requests to the server in two forms: URL and form. Forms generally do not have garbled characters. garbled characters are mainly related to URLs. Through the introduction of the previous blogs, we know that the process of sending Request Encoding from URLs to servers is too messy. Different operating systems, different browsers, and different Web character sets will lead to completely different encoding results. If programmers want to take every result into account, isn't it too scary? Is there a way to ensure that the client uses only one encoding method to send requests to the server?

Yes! Here I mainly provide the following methods:

I. javascript

If javascript encoding is used, the browser is not allowed to intervene. After encoding, the request is sent to the server and decoded on the server. When learning this method, we need three methods of material and javascript encoding: escape (), encodeURI (), encodeURIComponent ().

Escape

The specified string is encoded using the SiO2 Latin character set. All non-ASCII characters are encoded as % xx strings. xx indicates the hexadecimal numbers corresponding to this character in the character set. For example, the encoding format is % 20. The corresponding decoding method is unescape ().

In fact, escape () cannot be directly used for URL encoding. Its real function is to return the Unicode encoded value of a character. For example, the above "I am cm" result is % u6211 % u662Fcm, where "I" corresponds to 6211, "yes" encoded as 662F, and "cm" encoded as cm.

Note that escape () is not "+" encoded. However, we know that when a webpage submits a form, if there is a space, it will be converted to a + character. When the server processes the data, it will process the plus sign as a space. Therefore, be careful when using it.

EncodeURI

Encode the entire URL, which uses the UTF-8 format to output the encoded string. However, apart from ASCII encoding, encodeURI does not encode some special characters, such :! @ # $ & * () = :/;? + '.

EncodeURIComponent

Convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI, encodeURIComponent is more powerful, and it will not be encoded in encodeURI () symbols (;/? : @ & =+ $, #) All are encoded. However, encodeURIComponent only encodes the URL components individually, not the whole URL. Corresponding to the decodeURIComponent method.

Of course, we generally use encodeURI for encoding. This method is used for two backend decoding of the so-called javascript code. Javascript solves this problem by one transcoding and two transcoding methods.

One Transcoding

Javascript transcoding:

Var url = '<s: property value = "webPath"/>/ShowMoblieQRCode. servlet? Name = I am cm '; window. location. href = encodeURI (url );

Transcoded URL: http: // 127.0.0.1: 8080/perbank/ShowMoblieQRCode. servlet? Name = % E6 % 88% 91% E6 % 98% AFcm

Background processing:

String name = request. getParameter ("name"); System. out. println ("foreground input parameter:" + name); name = new String (name. getBytes ("ISO-8859-1"), "UTF-8"); System. out. println ("decoded parameter:" + name );

Output result:

Front-end input parameter :?????? Cm
Decoded parameter: cm

Secondary Transcoding

Javascript

Var url = '<s: property value = "webPath"/>/ShowMoblieQRCode. servlet? Name = I am cm '; window. location. href = encodeURI (url ));

Transcoded url: http: // 127.0.0.1: 8080/perbank/ShowMoblieQRCode. servlet? Name = % 25E6% 2588% 2591% 25E6% 2598% 25 AFcm

Background processing:

String name = request. getParameter ("name"); System. out. println ("foreground input parameter:" + name); name = URLDecoder. decode (name, "UTF-8"); System. out. println ("decoded parameter:" + name );

Output result:

Frontend input parameter: E68891E698AFcm

Decoded parameter: cm


Filter

The filter LZ provides two types of encoding: The first encoding and the second encoding.

Filter 1

This filter directly sets the request Encoding format.

Public class CharacterEncoding implements Filter {private FilterConfig config; String encoding = null; public void destroy () {config = null;} public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request. setCharacterEncoding (encoding); chain. doFilter (request, response);} public void init (FilterConfig config) throws Serv LetException {this. config = config; // obtain the configuration parameter String str = config. getInitParameter ("encoding"); if (str! = Null) {encoding = str ;}}}

Configuration:

<! -- Chinese filter configuration --> <filter-name> chineseEncoding </filter-name> <filter-class> com. test. filter. characterEncoding </filter-class> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init- param> </filter> <filter-mapping> <filter-name> chineseEncoding </filter-name> <url-pattern>/* </url-pattern> </filter- mapping>

Filter 2

The filter directly decodes parameters in the processing method, and then resets the decoded parameters to the attribute of the request.

Public class CharacterEncoding implements Filter {protected FilterConfig filterConfig; String encoding = null; public void destroy () {this. filterConfig = null;}/*** initialize */public void init (FilterConfig filterConfig) {this. filterConfig = filterConfig ;} /*** convert inStr to the encoding form of the UTF-8 *** @ param inStr input String * @ return UTF-8 encoded String * @ throws UnsupportedEncodingException */private String toUT F (String inStr) throws UnsupportedEncodingException {String outStr = ""; if (inStr! = Null) {outStr = new String (inStr. getBytes ("iso-8859-1"), "UTF-8");} return outStr;}/*** Chinese garbled filtering Processing */public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; // method for obtaining the request (1.p Ost or 2.get), which processes String method = request according to different request methods. getMethod (); // 1. request Submitted in post mode, directly set encoding as UTF-8 if (method. equalsIgnoreCase ("post") {try {request. setCharacterEncoding ("UTF-8");} catch (UnsupportedEncodingException e) {e. printStackTrace () ;}// 2. request else submitted in get mode {// retrieves the parameter set Enumeration submitted by the customer <String> paramNames = request. getParameterNames (); // retrieve the name and value of each parameter while (paramNames. has MoreElements () {String name = paramNames. nextElement (); // retrieve the parameter name String values [] = request. getParameterValues (name); // retrieve its value based on the parameter name // if the parameter value set is not empty if (values! = Null) {// traverses the parameter value set for (int I = 0; I <values. length; I ++) {try {// call toUTF (values [I]) string vlustr = toUTF (values [I]); values [I] = vlustr;} catch (UnsupportedEncodingException e) {e. printStackTrace () ;}// hide the value in the form of an attribute in the request. setAttribute (name, values) ;}}// set the response method and the Chinese Character Set response. setContentType ("text/html; charset = UTF-8"); // continue to execute the next filter. If there is no filter, the request chain is executed. doFilter (request, response );}}

Configuration:

<! -- Chinese filter configuration --> <filter-name> chineseEncoding </filter-name> <filter-class> com. test. filter. characterEncoding </filter-class> </filter> <filter-mapping> <filter-name> chineseEncoding </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>

Others

1. Set pageEncoding and contentType

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

2. Set the URIEncoding of tomcat

By default, the tomcat server uses the ISO-8859-1 encoding format to encode, The URIEncoding parameter to encode the get request URL, so we only need to in the tomcat server. add URIEncoding = "UTF-8" to the <Connector> label of the xml file.

----- Original from: http://cmsblogs.com /? P = 1526Please respect the author's hard work and repost the source.

----- Personal site:Http://cmsblogs.com

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.