session 登陸瀏覽,並實現session注銷登陸


 

<%@ 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>session1.jsp</title>
</head>
<body>
<%
String cardid = null ;
Cookie[] cks = request.getCookies() ;

if(cks != null)
{ 
     // 如果已經設置了cookie , 則得到它的值,並保存到變量pName中
        for(int i=0; i<cks.length; i++)
        {
               if(cks[i].getName().equals("cardid"))

            cardid = cks[i].getValue();
        }
     }
%>
<form action="session2.jsp" method="post">
卡號<input type="text" name="cardid" value="<% if(cardid != null) out.println(cardid); %>"><br>
密碼<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>

</body>
</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>session2.jsp</title>
</head>
<body>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.shuyinghengxie.bank.CardDAO"%>
<%@ page language="java" contentType="text/html; charset=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>
<%
//設置不緩存頁面
response.setHeader("Cache-Control", "no-cache") ;

//登陸成功定時跳轉
//response.setHeader("refresh", "2;URL=http://www.baidu.com") ;

//頁面跳轉 
//response.sendRedirect("success.jsp") ;


String kahao = request.getParameter("cardid") ;

String password = request.getParameter("password") ;

if(kahao==null || password==null ||
        kahao == "" || password == "" )
{
    out.write("請正確登錄") ;
}
else
{
    CardDAO cd = new CardDAO() ;
    
    if(cd.checkLogin(kahao, password))
    {
        //out.write("登陸成功") ;
        response.getWriter().write("驗證通過") ;
        
        //創建Cookie
        Cookie ck = new Cookie("kah888o",kahao) ;
        
        //設置過期時間
        ck.setMaxAge(10*24*60*60)  ;
        
        //發送
        response.addCookie(ck) ;
        
        //創建session
        session.setAttribute("kahao", kahao) ;
        session.setAttribute("username","李四") ;
        
        
        //設置session超時時間
        //默認設置是20分鍾
        //如果超過20分鍾沒有任何請求發送給服務器,session就失效
        session.setMaxInactiveInterval(30) ;
        
        
        response.sendRedirect("session3.jsp") ;
        
    }
    else
    {
        out.write("登錄失敗") ;
    }
}


%>
</body>
</html>
</body>
</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>session3.jsp</title>
</head>
<body>
<%
Object  kahao = session.getAttribute("kahao") ;

if(kahao != null)
{
    out.write("您已登陸") ;
}
else
{
    out.write("尚未登陸") ;
}
%>
<a href="session4.jsp">退出登錄</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=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>session4.jsp</title>
</head>
<body>
已退出!
<%

session.invalidate() ;   //銷毀session

//2秒后跳轉
response.setHeader("refresh","2 ; url= session3.jsp") ;


%>

</body>
</html>

這時點擊退出,跳到session4頁面,此頁面定時2秒跳轉session3

這是可以看到已退出登陸

 


免責聲明!

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



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