Tomcat 7 默认是按ISO-8859-1进行URL解码,ISO-8859-1并未包括中文字符,中文字符不能被正确解析,虽然在Tomcat8已经解决了这个问题,把默认解码格式设置成了utf-8,但是由于maven中目前最高只支持tomcat 7,所以这里说一下一个get和post都通用的方式
- @WebServlet("/Servlet5")
- public class Servlet5 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String name = request.getParameter("name");
- System.out.println(name);
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <form action="Servlet5" method="get">
- <input name="name"><input type="submit">
- form>
- body>
- html>
name = new String(name.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);