JavaWebCookie的应用

 

 

 

 

 

 

 

 

 

 

 

 

package com.xzit.erpmodel.users.controller;

import java.io.IOException;

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 javax.servlet.http.HttpSession;

import com.xzit.erpmodel.users.domain.SysUsers;
import com.xzit.erpmodel.users.service.UsersService;
import com.xzit.erpmodel.users.service.UsersServiceImp;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        /* 获取用户提交的登录信息 */
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        String savepwd = request.getParameter("savepwd");
        
        SysUsers user = new SysUsers();
        user.setName(name);
        user.setPassword(pwd);
        
        UsersService service = new UsersServiceImp();
        SysUsers current = service.validateLogin(user);
        
        if(current !=null) {//用户登录成功
            
                
                Cookie[] cookies = request.getCookies();//获取用户未过期的所有Cookie
                if(cookies != null && cookies.length !=0) {//有Cookie
                    int count = 0;    //计数器
                    for(int i=0;i<cookies.length;i++) {//遍历数组查找目标Cookie
                        
                        if(cookies[i].getName().equals("logininfo") || cookies[i].getName().equals("logininfo2")) {//如果找到了目标有效的Cookie
                            
                            if(savepwd == null) {
                                cookies[i].setMaxAge(0);//不保存Cookie
                                response.addCookie(cookies[i]);
                            }
                            count++;
                        }
                    }
                    if(count == 0) {//没有找到Cookie
                        if(savepwd != null && savepwd.equals("on")) {//用户欲保存密码及用户信息
                            Cookie newCookie = new Cookie("logininfo",name);
                            Cookie newCookie2 = new Cookie("logininfo2",pwd);
                            newCookie.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                            newCookie2.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                            response.addCookie(newCookie);
                            response.addCookie(newCookie2);
                        }
                    }
                }else {
                    if(savepwd != null && savepwd.equals("on")) {//用户欲保存密码及用户信息
                        Cookie newCookie = new Cookie("logininfo",name);
                        Cookie newCookie2 = new Cookie("logininfo2",pwd);
                        newCookie.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                        newCookie2.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                        response.addCookie(newCookie);
                        response.addCookie(newCookie2);
                    }
                }
            
            //将用户对象存储到会话域中
            HttpSession sess = request.getSession();//如果有Session直接返回,没有则创建新的Session对象
            sess.setAttribute("currentUser", current);
            response.sendRedirect("worker.jsp");
        }else {
            response.sendRedirect("login.jsp?loginfaild=error");//登录失败跳转到登录页面
        }
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" session="false"%>
<!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>
    <form action="UsersServlet" method="post">
        <%!
            String uname = "";
            String upwd = "";
        %>
        <%
            Cookie []cookies = request.getCookies();
        
            if(cookies !=null && cookies.length>0){
                
                for(int i=0;i<cookies.length;i++){
                    
                    if(cookies[i].getName().equals("logininfo")){
                        uname = cookies[i].getValue();//提取用户名
                    }else if(cookies[i].getName().equals("logininfo2")){
                        upwd = cookies[i].getValue();//提取密码
                    }
                }
            }
        %>
        用户名称:<input name="username" value="<%=uname%>"><br>
        登录密码:<input name="password" type="password" value="<%=upwd%>">
        <input type="checkbox" name="savepwd" />保存密码
        <br>
        <input name="login" value="登录" type="submit">
    </form>
    <%
        String mess = request.getParameter("loginfaild");
        if(mess != null && mess.equals("error")){
            
        %>
            <label style="color:red">系统提示:用户名不存在或密码错误,登录失败</label>
    <%    }
    %>
</body>
</html>

 

posted @ 2022-02-08 22:43  伊万  阅读(45)  评论(0编辑  收藏  举报