用Cookie实现一天免登录功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Cookie实现一天免登录功能相关的知识,希望对你有一定的参考价值。

Servlet程序:

package com.westos.untitle2;

import com.westos.untitle2.CityService.UserService;
import com.westos.untitle2.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "Cookie4Servlet")
public class Cookie4Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setCharacterEncoding("utf-8");
         request.setCharacterEncoding("utf-8");
         response.setContentType("text/html;charset=utf-8");
         String username=request.getParameter("username");
         String password=request.getParameter("password");
         UserService userService=new UserService();
         List<User> list=userService.setUser();
         if (username==null||password==null){
            Cookie[] cookie=request.getCookies();
            for (Cookie c:cookie){
                if(c.getName().equals("username")){
                    username=c.getValue();
                }else if(c.getName().equals("password")){
                    password=c.getValue();
                }
            }
        }
        if(username.equals("")||password.equals("")){
            request.getRequestDispatcher("login2.html").forward(request,response);
        }else{
            for(User user:list){
                if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
                    Cookie cookie1=new Cookie("username",username);
                    Cookie cookie2=new Cookie("password",password);
                    cookie1.setMaxAge(60*60);
                    cookie2.setMaxAge(60*60);
                    response.addCookie(cookie1);
                    response.addCookie(cookie2);
                    request.getRequestDispatcher("success.html").forward(request,response);
                }else{
                    request.getRequestDispatcher("login2.html").forward(request,response);
                }
            }
        }
    }
}

登录页面程序:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<form action="./cookie4" method="Post" name="login">
    昵称:<input name="username" type="text" placeholder="请输入昵称" id="nicheng"/><br/><br/>
    密码:<input name="password" type="password" placeholder="请输入密码" id="mima"/><br/><br/>
    请输入验证码:<br/>
    <img src="./yzm01" height="32" id="myyzm">
    <button type="button" value="看不清楚,再来一张" onclick="identifyload()">看不清楚,再来一张</button>
    <input name="yzm01" type="text"/><br/>
    <button type="submit" value="登录" >登录</button>
    <button type="reset" value="重置">重置</button>
</form>
</body>
</html>

User:

public class User {
    private String Username;
    private String password;

    public String getUsername() {
        return Username;
    }

    public String getPassword() {
        return password;
    }

    public void setUsername(String username) {
        Username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

UserService:

package com.westos.untitle2.CityService;

import com.westos.untitle2.bean.User;
import sun.rmi.log.LogInputStream;

import java.util.ArrayList;
import java.util.List;

public class UserService {
    public List<User> setUser(){
        List<User> list=new ArrayList<>();
        User u1=new User();
        u1.setUsername("Bighead");
        u1.setPassword("4214963");
        list.add(u1);
        User u2=new User();
        u2.setUsername("lyy");
        u2.setPassword("0829");
        list.add(u2);
        return list;
    }
}

success.html程序,自己根据需求设置.

以上是关于用Cookie实现一天免登录功能的主要内容,如果未能解决你的问题,请参考以下文章

7天免登陆

Session的应用——三天免登录

进销存系统_实现7天免登陆

[py][mx]django的cookie和session操作

Cookie和Session

Cookie和Session使用