zoukankan      html  css  js  c++  java
  • 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

    这是可以看到已退出登陆

  • 相关阅读:
    安装 elasticsearch For LINUX
    java 读取文件最佳实践
    mysql alter 语句用法,添加、修改、删除字段等
    Linux type命令
    在mahout安装目录下输入mahout 提示 ERROR: Could not find mahout-examples-*.job
    Ubuntu中安装eclipse ,双击eclipse出现invalid configuration location问题
    Ubuntu中查看32还是64
    转载--JAVA读取文件最佳实践
    Ubuntu中添加eclipse
    Hadoop 如何查看是否32位
  • 原文地址:https://www.cnblogs.com/20gg-com/p/6012468.html
Copyright © 2011-2022 走看看