中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用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

嫩江县| 武宁县| 兰坪| 枞阳县| 兖州市| 北流市| 安泽县| 改则县| 宁都县| 旬邑县| 徐汇区| 大理市| 通州区| 肥城市| 大冶市| 连州市| 宾川县| 古浪县| 孙吴县| 浮山县| 莱西市| 兴国县| 台前县| 海盐县| 铁力市| 兴安盟| 屏东市| 萨迦县| 辰溪县| 镇江市| 石阡县| 安溪县| 浪卡子县| 万源市| 黑山县| 平昌县| 永安市| 莎车县| 吉安县| 竹北市| 瓦房店市|