Java Web中使用:
java.servlet.http public class Cookie extends Object implements Cloneable, Serializable
要创建一个Cookie,传递一个名称和值给Cookie的构造函数:
Cookie cookie = new Cookie(name, value);
创建一个Cookie之后,可以设置它的domain, path 和 maxAge等属性。
HttpServletResponse.addCookie(Cookie cookie);
当浏览器再次发出对同一个资源或者对同一台服务器中的不同资源的请求时,它会同时把从Web浏览器处收到的cookie再传回去。
从客户端读取Cookie时,包括maxAge在内的其他属性都输不可读的,也不会被提交。浏览器提交Cookie时只会提交name和value属性。
Cookie[] HttpServletRequest.getCookies();
方法。该方法将返回一个Cookie数组。如果请求中没有cookie,将返回null。
Cookie cookie = new Cookie(name, value); cookie.setMaxAge(0); response.addCookie(cookie);
logincheck.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String action = request.getParameter("action"); if (action != null){ if (action.equals("login")){ String account = request.getParameter("account"); String password = request.getParameter("password"); System.out.println(account + password + request.getParameter("timeout")); int timeout = Integer.parseInt(request.getParameter("timeout")); String ssid = account; Cookie accountCookie = new Cookie("account", account); accountCookie.setMaxAge(timeout); Cookie ssidCookie = new Cookie("ssid", ssid); ssidCookie.setMaxAge(timeout); response.addCookie(accountCookie); response.addCookie(ssidCookie); response.sendRedirect(request.getRequestURL() + "?" + System.currentTimeMillis()); return; }else if (action.equals("logout")){ Cookie accountCookie = new Cookie("account", ""); accountCookie.setMaxAge(0); Cookie ssidCookie = new Cookie("ssid", ""); ssidCookie.setMaxAge(0); response.addCookie(accountCookie); response.addCookie(ssidCookie); response.sendRedirect(request.getRequestURL() + "?" + System.currentTimeMillis()); return; } } boolean login = false; String account = null; String ssid = null; if (request.getCookies() != null){ for (Cookie cookie : request.getCookies()){ if (cookie.getName().equals("account")){ account = cookie.getValue(); } if (cookie.getName().equals("ssid")){ ssid = cookie.getValue(); } } } if (account != null && ssid != null){ login = true; } %> <!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> <% if (login){ %> Welcome: ${cookie.account.value}. <a href="${oageContext.request.requestURL}?action=logout"> Logout</a> <% }else{ %> <form action="${pageContext.request.requestURL}?action=login" method="post"> <table> <tr> <td>Account: </td> <td><input type="text" name="account" style="width:200px;"></td> </tr> <tr> <td>Password: </td> <td><input type="text" name="password" style="width:200px;"></td> </tr> <tr> <td>Invalid Term:</td> <td> <input type="radio" name="timeout" value="-1" checked>关闭浏览器即失效<br/> <input type="radio" name="timeout" value="<%= 30 * 24 * 60 * 60 %>">30天内有效<br/> <input type="radio" name="timeout" value="<%= Integer.MAX_VALUE %>" checked>永久有效<br/> </td> </tr> <tr> <td></td> <td> <input type="submit" value="Login"> </td> </tr> </table> </form> <% } %> </body> </html>
在浏览器中访问如下URL:
http://localhost:8080/base-webapp/jsp/cookie/logincheck.jsp