In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you the solution of java request.getParameter Chinese garbled code, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
When browsing the project today, I encountered a problem. The page uses ${requestScope. The value obtained by the parameter name} is garbled, and then it is searched. Finally, it is said that it is a coding problem, and the search result is attached:
In Java development, if the framework is not perfect or beginners in the learning process, Chinese garbled is a common thing (ha. It is not the Chinese who invented the java language.
Today, I would like to share with you several solutions to garbled Chinese parameters obtained by request.getParameter () in java Web development.
To solve the problem, we must first study the problem. Why are the Chinese parameters transmitted by URL garbled?
Reason: when Http requests transmission, url is encoded in ISO-8859-1. After receiving the byte stream, the server will use ISO-8859-1 encoding by default to decode the character stream (resulting in Chinese garbled).
Solution: we need to encode the string obtained by request.getParameter ("parameter name") into a byte stream with ISO-8859-1, and then decode it into a character stream with utf-8.
Code:
String str = new String (request.getParameter ("parameter name") .getBytes ("iso-8859-1"), "utf-8")
This is to deal with the problem of garbled code by transcoding. We can also set the URL code set (URIEncoding) and set the encoding through the Tomcat configuration file. This method is also once and for all.
Modify server.xml under Tomcat/conf directory
Focus on the userBodyEncodingForURI and URIEncoding attributes
Let's explain the meaning of these two attributes.
The useBodyEncodingForURI parameter indicates whether to use the request.setCharacterEncoding parameter to re-encode the data submitted by URL and the data submitted by GET in the form. By default, this parameter is false. The URIEncoding parameter specifies the encoding to uniformly re-encode (decode) all GET mode requests.
The difference between URIEncoding and useBodyEncodingForURI is
URIEncoding is to uniformly re-encode the data of all GET requests, while useBodyEncodingForURI is to re-encode the data according to the request.setCharacterEncoding parameters of the page responding to the request. Different pages can have different recoding codes.
There is also a way to solve the garbled problem by rewriting the getParameter method and setting the decoding / mode.
The above content is the solution to java request.getParameter Chinese garbled code. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.