zoukankan      html  css  js  c++  java
  • 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行

    
    
  • 相关阅读:
    Android 针对单个Activity设置状态栏颜色
    Android Studio 无法预览xml布局视图:failed to load AppCompat ActionBar with unkNown error
    浅谈EditText控件的inputType类型
    Android项目实战(四十三):夜神模拟器
    浅谈SnackBar(Toast大兄弟)
    打包错误--Error:A problem was found with the configuration of task ':app:packageRelease'.
    Android项目实战(四十二):启动页优化,去除短暂白屏或黑屏
    Android项目实战(四十一):游戏和视频类型应用 状态栏沉浸式效果
    04-01 集成学习基础
    02-24 决策树总结
  • 原文地址:https://www.cnblogs.com/AI-Cobe/p/9290987.html
Copyright © 2011-2022 走看看