JSP內置對象---request對象(用戶登錄頁面(返回值和數組:gerParameter,getParameterValues))


創建兩個jsp頁面:reg.jsp 和 request.jsp

reg.jsp:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>

<%@ page import="java.text.*" %>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
  <h1>用戶注冊</h1>
  <form action="request.jsp" name="loginForm" method="post">

  <table>
  <tr>
  <td>用戶名:</td>
  <td><input type="text" name="username"/></td>
  </tr>


  <tr>
  <td>愛好:</td>
  <td>
    <input type="checkbox" name="favorite" value="read"/>讀書
    <input type="checkbox" name="favorite" value="yujia"/>瑜伽
    <input type="checkbox" name="favorite" value="fadai"/>發呆
  </td>
  </tr>


  <tr>
  <td>顯示:</td>
  <td colspan="2"><input type="submit" name="提交"/></td>
  </tr>
  </table>
  </form>
</body>
</html>

 

 request.jsp:

 

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"
%>
<%@ page import="java.text.*" %>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
  <h1>request內置對象</h1>
  <%
  request.setCharacterEncoding("utf-8");//解決中文亂碼問題
  %>
  <!--用戶名返回的是一個值,用gerParameter,愛好返回的是一個字符串數組,用getParameterValues -->
  用戶名:<%=request.getParameter("username")%>
  愛好:<%
  String[] favorites = request.getParameterValues("favorite");
  for(int i=0;i<favorites.length;i++)
  {
  out.println(favorites[i]+"&nbsp;&nbsp;");
  }
  %>
</body>
</html>

 

運行結果:

 

 

 

中間出了一點小差錯:

(1)Tomcat工程被我closed掉了,所以啟動報錯,open project 即可

 

(2)將reg.jsp的<form action="request.jsp" name="loginForm" method="post">后面加了個</form>所以下面的代碼失效,狂點提交按鈕沒有反應,最后是用火狐調試發現的,繼續加油。

 

 

在reg.jsp中添加URL地址,查看鏈接

<body>
<h1>用戶注冊</h1>
<form action="request.jsp" name="loginForm" method="post">
<table>
<tr>
<td>用戶名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>愛好:</td>
<td>
<input type="checkbox" name="favorite" value="read"/>讀書
<input type="checkbox" name="favorite" value="yujia"/>瑜伽
<input type="checkbox" name="favorite" value="fadai"/>發呆
</td>
</tr>
<tr>
<td>顯示:</td>
<td colspan="2"><input type="submit" name="提交"/></td>
</tr>
</table>
</form>
<a href="request.jsp?username=張張">測試張張賬號</a>       <!-- 添加URL -->
</body>

 

當username是英文的時候正常顯示,當是中文時,顯示亂碼,因此可以得出 request.jsp 中的腳本(如下文),無法解決URL傳遞中文出現的亂碼問題

<%
request.setCharacterEncoding("utf-8");//解決中文亂碼問題
%>

解決如下:

修改Tomcat 配置文件 server.xml,地址:D:\Program Files\eclipse\apache-tomcat-7.0.69\conf  ,添加屬性: URIEcoding="utf-8",重啟Tomcat 服務器,才能生效

 

 

另外,因為添加了URL,URL中只有用戶名,沒有愛好,通過URL傳遞參數只有用戶名,沒有愛好,所以在獲取愛好時報空指針異常,所以在做遍歷輸出愛好時要加上判斷(不等於空繼續執行代碼,等於空則不輸出),request.jsp 輸出愛好部分代碼變更:

愛好:<%
if(request.getParameterValues("favorite")!=null){                       //加上判斷
String[] favorites = request.getParameterValues("favorite");
for(int i=0;i<favorites.length;i++)
{
out.println(favorites[i]+"&nbsp;&nbsp;");
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM