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

这是可以看到已退出登陆

 

posted @ 2016-10-30 09:20  丶疏影横斜  阅读(3178)  评论(0编辑  收藏  举报