學習記錄 java session保存用戶登錄


 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11 
12 //檢查session,取得session信息
13 Object obj = session.getAttribute("username");
14 if(obj != null)
15 {
16     out.print("歡迎登錄 "+obj.toString());    
17 }
18 else
19 {
20 out.print("會話超時,請重新登錄系統");    
21 //3秒后跳轉到用戶界面
22 response.setHeader("refresh", "3;URL=Login.jsp");
23 }
24 %>
25 主頁面
26 <br>
27 <a href="Login.jsp">退出登錄</a>
28 </body>
29 </html>

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 登錄頁面
11 <br>
12 <%
13 //銷毀session
14 session.invalidate();
15 %>
16 
17 <form action="TestPW.jsp" method = "post">
18 用戶名:<input type="text"name = "username">
19 密碼:<input type="password"name = "password">
20 <input type="submit" value="登錄">
21 </form>
22 </body>
23 </html>
<%@ 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>
<%
//驗證用戶登錄信息是否正確

String un =  request.getParameter("username");
String pw =  request.getParameter("password");

if(un != null && pw != null)
{
    //如果正確  就創建session,並跳轉到main.jsp
    if(un.equals("asd") && pw.equals("123"))
    {
        //記錄用戶信息
        session.setAttribute("username", un);
        
        
        //跳轉到系統主頁面
        response.sendRedirect("Main.jsp");
        
    }
    else
    {
        //否則就提示登錄錯誤
        out.print("用戶名或密碼錯誤");
    }
}
else
{
    out.print("請以正常的方式訪問系統");
    }

%>



</body>
</html>

 

 

 

 

 

 


免責聲明!

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



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