JSP中创建与使用Cookie
创建Cookie对象
Cookie newCookie = new Cookie(String key, Object value);
写入Cookie对象
response.addCookie(newCookie);
读取Cookie对象
Cookie[] cookies = request.getCookies();
常用方法
void setMaxAge(int expiry) 设置cookie有效期,单位-秒
void setValues(String value) 对cookie进行赋值
String getName() 获取cookie的名称
String getValue() 获取cookie的值
int getMaxAge() 获取cookie有效时间,单位-秒
通过Cookie实现登录状态记录,示例代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <%@ page import="java.net.HttpCookie" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <!-- Page title --> <title>imooc - Login</title> <!-- End of Page title --> <!-- Libraries --> <link type="text/css" href="css/login.css" rel="stylesheet" /> <link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /> <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="js/easyTooltip.js"></script> <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script> <!-- End of Libraries --> </head> <body> <% String username = ""; String password = ""; Cookie[] cookies = request.getCookies(); if(cookies != null && cookies.length > 0){ for(Cookie c:cookies){ if(c.getName().equals("username")){ username = c.getValue(); } if(c.getName().equals("password")){ password = c.getValue(); } } } %> <div id="container"> <div class="logo"> <a href="#"><img src="assets/logo.png" alt="" /></a> </div> <div id="box"> <form action="do_login.jsp" method="post"> <p class="main"> <label>用户名: </label> <input name="username" value="<%=username%>" /> <label>密码: </label> <input type="password" name="password" value="<%=password%>"> </p> <p class="space"> <label>7天免登陆</label> <input type="checkbox" name="isUseCookie" checked="checked"> <input type="submit" value="登录" class="login" style="cursor: pointer;"/> </p> </form> </div> </div> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" %> <jsp:useBean id="loginUser" class="com.po.Users" /> <jsp:useBean id="userDAO" class="com.dao.UsersDAO" /> <jsp:setProperty name="loginUser" property="*" /> <% // 如果用户名和密码都等于admin,则登录成功 if(userDAO.usersLogin(loginUser)){ session.setAttribute("loginUser", loginUser.getUsername()); request.getRequestDispatcher("login_success.jsp").forward(request, response); }else { response.sendRedirect("login_failure.jsp"); } %>
<%@ page import="java.net.HttpCookie" %><%-- Created by IntelliJ IDEA. User: shongbing Date: 2019-01-05 Time: 14:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <!-- Page title --> <title>imooc - Login</title> <!-- End of Page title --> <!-- Libraries --> <link type="text/css" href="css/login.css" rel="stylesheet" /> <link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /> <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="js/easyTooltip.js"></script> <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script> <!-- End of Libraries --> </head> <body> <div id="container"> <div class="logo"> <a href="#"><img src="assets/logo.png" alt="" /></a> </div> <div id="box"> <% String loginUser = ""; if(session.getAttribute("loginUser") != null){ loginUser = session.getAttribute("loginUser").toString(); } String[] isUseCookies = request.getParameterValues("isUseCookie"); if(isUseCookies != null && isUseCookies.length >0){ // 把用户名和密码保存在Cookie对象中 String username = request.getParameter("username"); String password = request.getParameter("password"); Cookie usernameCookie = new Cookie("username", username); Cookie passwordCookie = new Cookie("password", password); usernameCookie.setMaxAge(864000); passwordCookie.setMaxAge(864000); response.addCookie(usernameCookie); response.addCookie(passwordCookie); }else { Cookie[] cookies = request.getCookies(); if(cookies != null && cookies.length > 0){ for(Cookie c:cookies){ if(c.getName().equals("username") || c.getName().equals("password")){ c.setMaxAge(0); // 设置Cookie失效 response.addCookie(c); // 重新保存 } } } } %> <a href="users.jsp" target="_blank">登录成功,查看用户信息</a> </div> </div> </body> </html>
<%@ page import="java.net.HttpCookie" %><%-- Created by IntelliJ IDEA. User: shongbing Date: 2019-01-06 Time: 12:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用户信息</title> </head> <body> <h1>用户信息</h1><hr> <% String username = ""; String password = ""; Cookie[] cookies = request.getCookies(); if(cookies != null && cookies.length > 0){ for(Cookie c:cookies){ if(c.getName().equals("username")){ username = c.getValue(); } if(c.getName().equals("password")){ password = c.getValue(); } } } %> Username: <%=username %><br> Password: <%=password %><br> </body> </html>