【jsp】session


利用session對象完成以下內容

login.jsp

完成登錄表單的顯示,同時想頁面本身進行數據提交,以完成登錄的驗證,如果登錄成功(用戶名和密碼固定:Joywy/1111),則保存屬性;如果登錄失敗,則顯示登錄失敗的信息。

  • login.jsp
<html>
<head><title>歡迎</title></head>
<body>
<form action="login.jsp" method="post">
	用戶名:<input type="text" name="uname"><br>
	密   碼:<input type="password" name="upass"><br>
	<input type="submit" name="登陸">
	<input type="reset" name="重置">
</form>
<%
	String name=(String).request.getParameter("uname");
	String password=(String).request.getParameter("upass");
	if(!(name==null || "".equals(name) || password==null || "".equals(password))){
		if("Joywy".equals(name) && "android".equals(password)){
			session.setAttribute("userid",name);
			response.setHeader("refresh","2,URL=welcome.jsp");
%>
<h3>登陸成功,即將跳轉</h3>
<h3>若未跳轉,點擊<a href="welcome.jsp">這裏</a></h3>
<%
		}else{
%>
			<h3>錯誤的用戶名或密碼!</h3>
<%
		}
	}
%>
</body>
</html>

Welome.jsp

此頁面要求在用戶登錄完成之後纔可以顯示登錄成功的信息,如果沒有登錄,則要給出未登錄的提示,同時給出一個登陸的連接地址。

  • welcome.jsp
<% page contgentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>歡迎</title></head>
<body>
<%
	if(session.getAttribute("userid") != null){
%>
<h3>歡迎<%=session.getAttribute("userid")%>來到<a href="logout.jsp">註銷</a></h3>
<%
	}else{
%>
	<h3>請先<a href="login.jsp">登錄</a></h3>
<%
	}
%>
</body>
</html>

logout.jsp

此功能完成登錄的註銷,註銷之後,頁面要跳轉會login.jsp,等待用戶繼續登錄。

  • logout.jsp
<% page contgentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>歡迎</title></head>
<body>
<%
	response.setHeader("refresh","2,URL=login.jsp");
	session.invalidate();
%>
<h3>成功退出,即將返回首頁</h3>	
<h3>如果沒跳轉,點擊<a href="login.jsp">這裏</a></h3>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章