POST乱码
1 package six.four.five.web; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 import java.io.PrintWriter; 10 import java.net.URLDecoder; 11 12 @WebServlet(name = "ServletDemo3") 13 public class ServletDemo3 extends HttpServlet { 14 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 // 设置响应内容类型 16 response.setContentType("text/html;charset=UTF-8"); 17 18 PrintWriter out = response.getWriter(); 19 String title = "使用 POST 方法读取表单数据"; 20 // String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8"); 21 // 处理中文 22 String docType = "<!DOCTYPE html> \n"; 23 out.println(docType + 24 "<html>\n" + 25 "<head><title>" + title + "</title></head>\n" + 26 "<body bgcolor=\"#f0f0f0\">\n" + 27 "<h1 align=\"center\">" + title + "</h1>\n" + 28 "<ul>\n" + 29 " <li><b>站点名</b>:" 30 + request.getParameter("name") + "\n" + 31 " <li><b>网址</b>:" 32 + request.getParameter("url") + "\n" + 33 "</ul>\n" + 34 "</body></html>"); 35 } 36 37 // 处理 POST 方法请求的方法 38 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 39 doGet(request, response); 40 } 41 }
1 <%-- 2 Created by IntelliJ IDEA. 3 User: 17999 4 Date: 2018/6/5 5 Time: 20:40 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %> 9 <html> 10 <head> 11 <title>$Title$</title> 12 </head> 13 <body> 20 <form action="http://localhost:8080/TomcatTest/Form" method="POST"> 21 网址名:<input type="text" name="name"> 22 <br /> 23 网址:<input type="text" name="url" /> 24 <input type="submit" value="提交" /> 25 </form> 36 </body> 37 </html>
解决方法是:
String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
1 package six.four.five.web; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import java.io.IOException; 9 import java.io.PrintWriter; 10 import java.net.URLDecoder; 11 12 @WebServlet(name = "ServletDemo3") 13 public class ServletDemo3 extends HttpServlet { 14 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 // 设置响应内容类型 16 response.setContentType("text/html;charset=UTF-8"); 17 18 PrintWriter out = response.getWriter(); 19 String title = "使用 POST 方法读取表单数据"; 20 String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8"); 21 // 处理中文 22 String docType = "<!DOCTYPE html> \n"; 23 out.println(docType + 24 "<html>\n" + 25 "<head><title>" + title + "</title></head>\n" + 26 "<body bgcolor=\"#f0f0f0\">\n" + 27 "<h1 align=\"center\">" + title + "</h1>\n" + 28 "<ul>\n" + 29 " <li><b>站点名</b>:" 30 + name + "\n" + 31 " <li><b>网址</b>:" 32 + request.getParameter("url") + "\n" + 33 "</ul>\n" + 34 "</body></html>"); 35 } 36 37 // 处理 POST 方法请求的方法 38 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 39 doGet(request, response); 40 } 41 }
第20行