Servlet Chinese garbled Tomcat8
question:
ServletGetPost
Code:
JSP:
<meta charset="UTF-8">
<html>
<body>
<h2>Hello World!</h2>
<form action="/login" method="post">
username:<input type="text" name="username"><br/>
pwd:<input type="password" name="pwd"><br/>
<input type="submit" value="login" />
</form>
</body>
</html>
Servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
System.out.println(username + " " + pwd);
}
Screenshot:
try method:
requestsetCharacterEncodingUTF-8 UT-8 JSPUTF-8 Tomcat8
modify server
under conf
- should try to avoid using non-ASCII characters in URL, otherwise you may encounter garbled code. It is best to set the parameters URIEncoding and useBodyEncdingForURI in our server
.
- be sure to set request .setCharacterEncoding (charset), before calling the request.getParameter method for the first time, otherwise the data submitted by your POST form may be garbled
is embarrassing. The browser uses ASCII to decode by default, and you can adjust the browser decoding method to UTF-8
< H1 > check the code < / H1 >
- Open a file
- modify the encoding
code
by soarkey
java files, it is best to use UTF-8 encoding for all jsp file encodings.
for post requests
set request .setCharacterEncoding ("UTF-8") so that there is no garbled code in Chinese!
you can uniformly write a filter setting. If you use spring-mvc, you can configure it in web.xml
.
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
for get requests
if your request has a get, parameter in Chinese, for example, on the name= page, re-encode the url as follows:
frontend encodeURIComponent (encodeURIComponent (url)),.
backend: get the parameter value and decode it using URLDecoder.decode (s, "UTF-8"). This method is really feasible, and the respondent has always used it in the actual project.
give an example:
Front end:
<script type="text/javascript">
var url = "/xxx/param/test";
var name = "";
name = encodeURIComponent(name);
name = encodeURIComponent(name);//
alert(name);
url = url + "?name="+name;
window.location.href = url;
</script>
backend:
@Controller
@RequestMapping(value="/param")
public class ParamController extends BaseController<ParamEntity> {
/**
* @throws UnsupportedEncodingException
*
*/
@RequestMapping(value="/test",method=RequestMethod.GET)
public String test(@RequestParam("name") String name) throws UnsupportedEncodingException{
name = URLDecoder.decode(name, "UTF-8");//
System.out.println(name);
return "index";
}
}
Note: the get request has Chinese parameters, and you can specify which encoding rules the container uses to decode the submitted parameters (someone replied to use this method, that is, to modify the URIEncoding parameter in the Connector in the tomcat configuration file), but this is not recommended. Secondary encoding is recommended.
Why do I need secondary coding? You can read the following blog posts:
eURIComponent Encoding twice