- 通過上一篇文章JSP內置物件 https://blog.csdn.net/weixin_44989801/article/details/112132063的學習,我們了解了request、response、 session、 application等內置物件以及相關的使用方法,接下來,我們用幾個小案例來練習使用以上內置物件及內置物件的相關方法,
練習1 實作用戶的登錄功能(request.getParameter()和request.setAttribute()方法的使用)
- 功能描述: 實作用戶的簡單登錄功能,用戶通過用戶名和密碼登錄登錄,驗證成功后,跳轉到相應的界面,如果用戶名或密碼不正確,則回傳用戶名或密碼錯誤的提示詞,要求用戶重新輸入用戶名或密碼,例如,只有用戶名為,user 密碼為123456 的用戶才能登陸成功!
- 登錄分析: 用戶在登錄界面輸入用戶名和密碼,在檢查界面判定用戶名或密碼是否正確,也就是說,需要在檢查界面接收登錄界面用戶輸入的用戶名和密碼,才能夠與正確的賬號密碼相對比,正確則跳轉到成功后的界面,失敗則回傳相應資訊到登錄界面,
- 登錄界面原始碼 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>login</title>
</head>
<body>
<%
/* 使用error1和error接收request.setAttribute 方法保存的內容
此功能是用來接收檢查界面判定用戶名或密碼為慷訓者不正確后回傳的內容(String 型別)
從而實作,用戶登錄失敗后的相應提示,
*/
String error1 = (String)request.getAttribute("error1");
String error = (String)request.getAttribute("error");
%>
<h3>用戶登錄</h3>
<hr>
/* action 的值指跳轉到哪個界面,這里用userinfo.jsp界面作為檢查界面*/
<form action = "userinfo.jsp" method = "post">
<div>
用戶名: <input type = "text" name = "uname"/>
</div>
<div>
密碼:<input type = "password" name = "upwd"/>
</div>
<div>
<input type = "submit" value = "登錄">
<input type = "reset" value = "重置">
</div>
</form>
/*
輸出錯誤資訊,但同時也判定該error 和error1的值是否為空,如果為空,則不顯示,不為空,則顯示相應資訊,這樣
寫是為了避免在第一次訪問頁面時,輸出null(第一次訪問界面時,error的值是為null的) ,
*/
<%=error1==null?"":error1 %>
<%=error==null?"":error %>
</body>
</html>
- 登錄界面原始碼 userinfo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%!
/*首先設定指定的用戶名和密碼,只有與該方法的用戶名密碼一致,才能登陸成功!*/
boolean ValidateUser(String name,String pwd){
if(name.equals("user")&& pwd.equals("123456"))
return true ;
else return false ;
}
%>
<%
/*使用request.getParameter("uname");方法獲取用戶輸入的值,此處的uname 與input輸入框的name屬性的值一致,否則無法成功接收到用戶輸入的值!*/
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
String error = "";
String error1 ="";
/*使用equals("")方法判定用戶是否沒有輸入值,如果沒有輸入,則提示,不能為空*/
if(name.equals("")|| pwd.equals("")){
error1 = "用戶名或密碼不能為空";
request.setAttribute("error1", error1);
request.getRequestDispatcher("login.jsp").forward(request, response);
}
/*使用equals("")方法判定用戶是否輸入正確結果,如果不正確,則提示,用戶名密碼不正確!*/
else if(!ValidateUser(name,pwd)){
error = "用戶名或密碼不正確";
request.setAttribute("error", error);
request.getRequestDispatcher("login.jsp").forward(request, response);
}else {
/*成功則進入下一界面*/
out.print("歡迎光臨山外書店!");
}
%>
</body>
</html>
-
運行結果如下
-
1.用戶名或密碼不正確

- 2.用戶名或密碼為空

- 用戶名密碼正確,成功進入

練習2 實作一個猜亂數的小游戲(session方法)
-
功能描述: 通過系統自動生成亂數,用戶猜測該亂數的值,系統會根據用戶輸入的大小提示用戶是猜大了還是猜小了,用戶根據系統提示資訊重新進行猜測,最終,系統會記錄用戶一共猜測了幾次猜到了正確結果,并給出該亂數的值,
-
案例分析: 通過功能描述,我們發現,第一個界面是說明游戲規則,讓用戶輸入猜測的值,第二個界面是檢查頁面,檢查用戶的值是猜大了還是猜小了,并給出提示資訊,然后用戶根據資訊提示,再次進行猜測,最后一個界面是猜測成功后的界面,列印猜測成功并給出猜測次數的資訊,
-
猜亂數的小游戲原始碼index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%
/* 獲取亂數并保存在session物件中,(session物件可以在所有界面中進行獲取)*/
int n = (int)(Math.random()*100)+1;
session.setAttribute("sn",String.valueOf(n));
session.setAttribute("count","0");
%>
<p>猜數字游戲</p>
<p>隨機分給你一個1-100之間的數,請猜!</p>
<form action="result.jsp" method="post">
<input type="text" name="number">
<input type="submit" value="提交">
</form>
</body>
</html>
- 猜亂數的小游戲原始碼result.jsp(判定頁面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>head</title>
</head>
<body>
<%
int number = Integer.parseInt(request.getParameter("number"));
int n = Integer.parseInt((String)session.getAttribute("sn"));
int count = Integer.parseInt((String)session.getAttribute("count"));
count++;
session.setAttribute("count",String.valueOf(count));
out.print("我輸入的是:"+number);
out.print("亂數是:"+n);
String url ;
if(number>n){
response.sendRedirect("bigger.jsp");
}else if(number<n){
response.sendRedirect("smaller.jsp");
}else{
response.sendRedirect("successful.jsp");
}
%>
</body>
</html>
- 猜亂數的小游戲原始碼bigger.jsp(猜大了界面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>bigger.jsp</title>
</head>
<body>
<p>你猜大了,小一點試一試!</p>
<br/>
<p>隨機分給你一個1-100之間的數,請猜!</p>
<form action="result.jsp" method="post">
<input type="text" name="number">
<input type="submit" value="提交">
</form>
</body>
</html>
- 猜亂數的小游戲原始碼smaller.jsp(猜小了也頁面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>smaller.jsp</title>
</head>
<body>
<p>你猜小了,大一點試一試!</p>
<br/>
<p>隨機分給你一個1-100之間的數,請猜!</p>
<form action="result.jsp" method="post">
<input type="text" name="number">
<input type="submit" value="提交">
</form>
</body>
</html>
- 猜亂數的小游戲原始碼successful.jsp(成功頁面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>successful.jsp</title>
</head>
<body>
<p>恭喜你!猜對了!</p>
<%
/*獲取session物件保存的值*/
int n = Integer.parseInt((String)session.getAttribute("sn"));
int count = Integer.parseInt((String)session.getAttribute("count"));
%>
<p>該亂數是:<%=n %></p>
<p>次數是:<%=count %></p>
</body>
</html>
-
運行結果如下
-
1.首頁

-
2.猜測程序


-
2.成功頁面

- 通過以上案例,我們使用了request的常用方法和session的常用方法來完成相關案例練習,希望對大家有所幫助,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/244753.html
標籤:其他
上一篇:游戲客戶端更新版本號的安全措施。
