POST乱码
Posted ai-cobe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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> "; 23 out.println(docType + 24 "<html> " + 25 "<head><title>" + title + "</title></head> " + 26 "<body bgcolor="#f0f0f0"> " + 27 "<h1 align="center">" + title + "</h1> " + 28 "<ul> " + 29 " <li><b>站点名</b>:" 30 + request.getParameter("name") + " " + 31 " <li><b>网址</b>:" 32 + request.getParameter("url") + " " + 33 "</ul> " + 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> "; 23 out.println(docType + 24 "<html> " + 25 "<head><title>" + title + "</title></head> " + 26 "<body bgcolor="#f0f0f0"> " + 27 "<h1 align="center">" + title + "</h1> " + 28 "<ul> " + 29 " <li><b>站点名</b>:" 30 + name + " " + 31 " <li><b>网址</b>:" 32 + request.getParameter("url") + " " + 33 "</ul> " + 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行
以上是关于POST乱码的主要内容,如果未能解决你的问题,请参考以下文章
关于java代码提交HTTP POST请求中文乱码的解决方法