向jsp页面传值时出现乱码

在一个html页面中用表单向jsp页面传值:

这是html页面

<html>
  <head>
    <title>MyBeans.html</title>
    
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
  <form method="post" action="../jsp/MyBeans.jsp">
      学号:<input name="sno" type="text">
      姓名:<input name="name" type="text">
  <input value="submit" type="submit"> <input value="reset" type="reset">
  </form>
    This is my HTML page. <br>
  </body>
</html>

这是jsp页面中的解码:

<%String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8"); %>
    <%System.out.println("name"+name); %>

这里注意:

<%String name=new String(request.getParameter("name").getBytes("A"),"B"); %>
A要为ISO-8859-1

B可以是自己决定的编码格式:为<meta http-equiv="content-type" content="text/html; charset=utf-8"> 中的charset的值。http-equiv的content-type的作用是设定页面使用的字符集。(个人猜测是html会将发送的内容先编码为B格式,再发送到服务器,jsp的接受数据的编码是ISO-8859-1,所以先要用ISO-8859-1解码,再用B格式转化为原来的数据)

原文地址:https://www.cnblogs.com/l1019/p/6728404.html