cookie控制登陆时间

news/2024/7/4 9:55:13

使用cookie实现永久登陆

1,在cookie里面保存账号密码然后和数据库核对(由于我没有使用数据库,就不用了

2,在cookie里面保存时间戳和账号使用加密解密(我也没有使用时间戳

思路,request.getParameter("account");,获得账号,存入cookie,加入时间戳重定制网页内容

由于重定制的关系,这个时候我们不能用getParameter了,只能在cookie获得资料

原码

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%!private static final String KEY = "huanggabin";

    public final static String getPassword(String a) {
        return a;
    }%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    String action = request.getParameter("action");
    if ("login".equals(action)) {
        String account = request.getParameter("account");
        String account2 = request.getParameter("account");
        System.out.println(account2);
        String password = request.getParameter("password");
        int timeout = Integer.parseInt(request.getParameter("timeout"));
        String ssid = getPassword(account + KEY);
        Cookie accountCookie = new Cookie("account", account);
        accountCookie.setMaxAge(timeout);
        Cookie ssidCookie = new Cookie("ssid", ssid);
        ssidCookie.setMaxAge(timeout);

        response.addCookie(accountCookie);
        response.addCookie(ssidCookie);

        response.sendRedirect(request.getRequestURI() + "?"
                + System.currentTimeMillis());
        return;
    } else if ("logout".equals(action)) {
        Cookie accountCookie = new Cookie("account", "");
        accountCookie.setMaxAge(0);
        Cookie ssidCookie = new Cookie("ssid", "");
        ssidCookie.setMaxAge(0);
        response.addCookie(accountCookie);
        response.addCookie(ssidCookie);
        response.sendRedirect(request.getRequestURI() + "?"
                + System.currentTimeMillis());
        return;
    }
    boolean login = false;
    String account = null;
    String ssid = null;
    if (request.getCookies() != null) {
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("account")) {
                account = cookie.getValue();
            }
            if (cookie.getName().equals("ssid")) {
                ssid = cookie.getValue();
            }
        }
    }
    if (account != null && ssid != null) {
        login = ssid.equals(getPassword(account + KEY));
    }
%>
<%
    String account2 = request.getParameter("account");
    //System.out.println(account2);
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
alert(document.cookie);
</script>
</head>
<body>

    <%=login ? "欢迎回来" : "请先登录"%><br />
    <%
        if (login) {
    %>
    欢迎你!!!,<%=account%>
    <a href="${pageContext.request.requestURI}?action=logout">注销</a>
    <%
        } else {
    %>
    <form action="${pageContext.request.requestURI}?action=login"
        method='post'>
        账号:<input type="text" name="account"><br /> 密码: <input
            type="text" name="password"><br /> 有效期: <input type="radio"
            name="timeout" value="-1">关闭浏览器失效<br /> <input type="radio"
            name="timeout" value="<%=30 * 24 * 60 * 60%>">30天有效<br /> <input
            type="radio" name="timeout" value="<%=Integer.MAX_VALUE%>">永久有效<br />
        <input type="submit" value="登陆 " class="button">
    </form>
    <%
        }
    %>
</body>
</html>
View Code

 

转载于:https://www.cnblogs.com/vhyc/p/6432602.html


http://www.niftyadmin.cn/n/1748428.html

相关文章

arm linux 从入口到start_kernel 代码分析——head.S分析——6

arm linux 从入口到start_kernel 代码分析 - 6 (2008-07-30 16:07:15) 转载标签&#xff1a; it 分类&#xff1a;kernel 5. 开启mmu 开启mmu是又函数 __enable_mmu 实现的. 在进入 __enable_mmu 的时候, r0中已经存放了控制寄存器c1的一些配置(在上一步中…

BZOJ 2683: 简单题 [CDQ分治]

同上题 那你为什么又发一个&#xff1f; 因为我用另一种写法又写了一遍... 不用排序&#xff0c;$CDQ$分治的时候归并排序 快了1000ms... #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typed…

CMFCShellList和自定义ShellList结合使用,达到“直接浏览缩略图,双击打开图片”...

在GOPaint的设计研究过程中&#xff0c;我一直希望能够实现这样的结果&#xff08;A B C 3个步骤)在我之前的博客里面&#xff0c;曾经有过缩略图显示的现就(http://www.cnblogs.com/jsxyhelu/p/5493329.html ),也应用到了实际的项目中。但是现在过了一段时间后回头再看&…

利用mybatis-generator自动生成代码

2019独角兽企业重金招聘Python工程师标准>>> 1、准备生成代码需要的文件和jar包&#xff08;下载地址&#xff09; 2、配置generatorConfig.xml文件 <?xml version"1.0" encoding"UTF-8"?> <!DOCTYPE generatorConfigurationPUBLIC …

arm linux 从入口到start_kernel 代码分析——head.S分析——7end

arm linux 从入口到start_kernel 代码分析 - 7(end) (2008-07-30 16:08:30) 转载标签&#xff1a; it 分类&#xff1a;kernel 6. 切换数据 在 arch/arm/kernel/head-common.S 中: 00014: .type __switch_data, %object00015: __switch_data:00016: .long __mmap_switched000…

阿里云跨服务器文件拷贝

使用scp命令&#xff1a; 1、将当前一个文件copy到远程另外一台主机上&#xff1a; scp /home/daisy/full.tar.gz root远程ip:/home/root2、将文件从远程主机copy到当前系统上&#xff1a; scp root/full.tar.gz 远程ip:/home/root/full.tar.gz home/daisy/full.tar.gz尝试用ce…

基础算法----找出集合中和值为指定值的两个数

2019独角兽企业重金招聘Python工程师标准>>> 思想 假定集合为有序集合&#xff0c;对于有序集合来说&#xff0c;和值大于指定值则后位前移&#xff0c;否则则前位后移&#xff1b; 实现 int[] arr { 1, 3, 5, 7, 9, 15 }; // 找出和值为10的数static void find…

MyEclipse自动生成注释,编辑默认注释模板

自动生成方法注释&#xff1a;写完方法后&#xff0c;在该方法上方输入/**后&#xff0c;按下回车键&#xff0c;会自动生成该方法注释。/*** * param uid* return*/public Map<String, String> getSysUserId(String uid) {}编辑默认注释模板&#xff1a;1.类注释&#x…