We mainly send requests to the server through two forms of submission: URL and form. The form form generally does not have garbled problems, and the garbled problems are mainly on the URL. Through the introduction of the previous blogs, we know that the process of sending request encoding to the server by URL is really too confusing. Different operating systems, different browsers, and different web character sets will lead to completely different encoding results. Isn’t it too scary if programmers want to take every result into account? Is there a way to ensure that the client only uses one encoding method to issue a request to the server?
have! Here I mainly provide the following methods
1. JavaScript
Using javascript encoding does not give the browser a chance to intervene. After encoding, send a request to the server and then decode it in the server. When mastering this method, we need three methods of javascript encoding: escape(), encodeURI(), and encodeURIComponent().
escape
The specified string is encoded using the SIO Latin character set. All non-ASCII characters are encoded as strings in %xx format, where xx represents the hexadecimal number corresponding to the character in the character set. For example, the encoding corresponding to the format is %20. Its corresponding decoding method is unescape().
In fact, escape() cannot be used directly for URL encoding, its real function is to return a character's Unicode-encoded value. For example, the result of "I am cm" above is %u6211%u662Fcm, where the corresponding encoding of "I" is 6211, the encoding of "Yes" is 662F, and the encoding of "cm" is cm.
Note that escape() is not correct for "+" encoding. But we know that if there are spaces on the web page when submitting the form, it will be converted into + characters. When the server processes data, the + sign will be processed into spaces. Therefore, be careful when using it.
encodeURI
Encoding the entire URL, it uses UTF-8 format to output the encoded string. However, encodeURI will not encode some special characters except ASCII encoding, such as: ! @ # $& * ( ) = : / ; ? + '.
encodeURIComponent()
Convert URI strings into escape format strings in UTF-8 encoding format. Compared to encodeURI, encodeURIComponent will be more powerful, and it will be encoded for symbols (; / ? : @ & = + $ , #) that are not encoded in encodeURI(). However, encodeURIComponent will only encode the components of the URL individually, and will not be used to encode the entire URL. The corresponding decode function method decodeURIComponent.
Of course, we usually use the encodeURI party to perform encoding operations. The so-called JavaScript encoding and decoding twice in the background is to use this method. There are two solutions to solve this problem in JavaScript: one transcoding and two transcoding methods.
Transcoding once
JavaScript transcoding:
var url = '/ShowMoblieQRCode.servlet?name=I am cm';window.location.href = encodeURI(url);
The transcoded URL: http://127.0.0.1:8080/perbank/ShowMoblieQRCode.servlet?name=%E6%88%91%E6%98%AFcm
Backend processing:
String name = request.getParameter("name"); System.out.println("Foreground incoming parameters: " + name);name = new String(name.getBytes("ISO-8859-1"),"UTF-8"); System.out.println("Decoded parameters: " + name);Output result:
Incoming parameters in the front desk: ??????cm
After decoding parameters: I am cm
Secondary transcoding
javascript
var url = '/ShowMoblieQRCode.servlet?name=I am cm';window.location.href = encodeURI(encodeURI(url));
Transcoded url: http://127.0.0.1:8080/perbank/ShowMoblieQRCode.servlet?name=%25E6%2588%2591%25E6%2598%25AFcm
Backend processing:
String name = request.getParameter("name"); System.out.println("Foreground incoming parameters: " + name); name = URLDecoder.decode(name,"UTF-8"); System.out.println("Decoded parameters: " + name);Output result:
Front-end incoming parameters: E68891E698AFcm
After decoding parameters: I am cm
filter
Using filters, filter LZ provides two types, the first is to set encoding, and the second is to perform decoding operations directly in the filter.
Filter 1
This filter directly sets the encoding format of the request.
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 ServletException { this.config = config; //Get configuration parameters String str = config.getInitParameter("encoding"); if(str!=null){ encoding = str; } }} Configuration:
<filter> <filter-name>chineseEncodingfilter-name> <filter-class>com.test.filter.CharacterEncodingfilter-class> <init-param> <param-name>encodingparam-name> <param-value>utf-8param-value> init-param> filter> <filter-mapping> <filter-name>chineseEncodingfilter-name> <url-pattern>/*url-pattern> filter-mapping>
Filter 2
In the processing method, the filter directly decodes the parameters, and then resets the decoded parameters to the request attribute.
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 into UTF-8's encoding form* * @param inStr Enter string* @return UTF - 8's encoding form string* @throws UnsupportedEncodingException */ private String toUTF(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; // The method to obtain the request (1.post or 2.get), and different processing is performed according to different request methods String method = request.getMethod(); // 1. For requests submitted in post, directly set the encoding to UTF-8 if (method.equalsIgnoreCase("post")) { try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } // 2. Request submitted in get else { // Get out the parameter set submitted by the client Enumeration paramNames = request.getParameterNames(); // traverse the parameter set to get out the name and value of each parameter while (paramNames.hasMoreElements()) { String name = paramNames.nextElement();// Take out the parameter name String values[] = request.getParameterValues(name);// Take out its value according to the parameter name // If the parameter value set is not empty if (values != null) { // traverse the parameter value set for (int i = 0; i < values.length; i++) { try { // Circle back and call each value toUTF(values[i]) method to convert the character encoding of the parameter value String vlustr = toUTF(values[i]); values[i] = vlustr; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } // Hide the value in the form of an attribute in request request.setAttribute(name, values); } } } // Set the response method and support Chinese character set response.setContentType("text/html;charset=UTF-8"); // Continue to execute the next filter. If there is no filter, the request chain.doFilter(request, response); }}Configuration:
<filter> <filter-name>chineseEncodingfilter-name> <filter-class>com.test.filter.CharacterEncodingfilter-class>filter> <filter-mapping> <filter-name>chineseEncodingfilter-name> <url-pattern>/*url-pattern> filter-mapping>
other
1. Set pageEncoding and contentType
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
2. Set up the URIEncoding of tomcat
By default, the tomcat server uses the ISO-8859-1 encoding format to encode the URL requested by the URIEncoding parameter, so we only need to add URIEncoding=”utf-8” to the tag of the server.xml file of tomcat.
The above is all about this article. I hope it will be helpful for everyone to learn java Chinese garbled questions.