遇到request.getParameter()獲取空值

編程學習就是一個不斷填坑的過程大哭
今天遇到request.getParameter()獲取空值的情況,各種查,最後終於找到原因,問題解決掉了!
說白了就是pageEncoding和charset的用法不同,以下是它們的區別:
pageEncoding表示這個頁面接收到參數以什麼字符集來編碼,charset爲指定此頁面的編碼方式。前者指明瞭此頁面對request裏的參數如何編碼,後者指明瞭如何對本頁面裏的字符如何編碼。
上源碼:
parameter_1.jsp

<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
    <title>jsp:parameter動作</title></head>
<body>
     <jsp:include page="parameter_2.jsp" flush="true">
         <jsp:param name="username" value="Tom" />
         <jsp:param name="password" value="123"/>
     </jsp:include>
</body>
</html>

parameter_2.jsp

<%@ page contentType="text/html" ****pageEncoding="UTF-8"**** %>
<html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>jsp:parameter動作</title>
  </head>
  <body>
  <hr>
  用戶名:<%=request.getParameter("username")%><br>
  密碼:<%=request.getParameter("password")%>
  <hr>
  </body>
</html>

parameter_2.jsp中的pageEncoding=”UTF-8”千萬不要寫成charset,否則就會獲取到空值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章