温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用Cookie实现一天免登录功能

发布时间:2020-07-08 22:16:34 来源:网络 阅读:773 作者:Bighead0829 栏目:开发技术

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程序,自己根据需求设置.

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI