代码如下
login.jsp
- <%@page import=“java.net.URLDecoder”%>
 - <%@ page language=“java” contentType=“text/html; charset=UTF-8”
 - pageEncoding=“UTF-8”%>
 - <!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>
 - <h1>用户登录</h1>
 - <%
 - String username = “”;
 - String password = “”;
 - Cookie[] cookies = request.getCookies();
 - if(cookies!=null &&cookies.length>0) {
 - for(Cookie c:cookies) {
 - if(c.getName().equals(“username”)) {
 - username = URLDecoder.decode(c.getValue(),”utf-8″);//解码
 - }
 - if(c.getName().equals(“password”)) {
 - password = URLDecoder.decode(c.getValue(),”utf-8″);
 - }
 - }
 - }
 - %>
 - <form action=“doLogin.jsp” method=“post”>
 - <table>
 - <tr>
 - <td>用户名</td>
 - <td><input type=“text” name=“username” value=“<%=username %>”/></td>
 - </tr>
 - <tr>
 - <td>密码</td>
 - <td><input type=“password” name=“password” value=“<%=password %>”/></td>
 - </tr>
 - <tr>
 - <td colspan=“2”>
 - <input type=“checkbox” name=“isUseCookie” checked=“checked”/>10 天记住登录
 - </td>
 - </tr>
 - <tr>
 - <td clospan=“2”>
 - <input type=“submit” value=“登录”/>
 - </td>
 - </tr>
 - </table>
 - </form>
 - </body>
 - </html>
 
doLogin.jsp
- <%@page import=“java.net.URLEncoder”%>
 - <%@ page language=“java” contentType=“text/html; charset=UTF-8”
 - pageEncoding=“UTF-8”%>
 - <!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>
 - <h1>登录成功</h1>
 - <br />
 - <%
 - request.setCharacterEncoding(“utf-8”);
 - //首先判断用户是否选择了记住登录选项
 - String[] isUseCookies = request.getParameterValues(“isUseCookie”);
 - if(isUseCookies!=null &&isUseCookies.length>0) {
 - //把用户名和密码保存在 Cookie 里面
 - //使用 URLEncoder 解决无法在 Cookie 中保存中文字符串问题
 - String username = URLEncoder.encode(request.getParameter(“username”),”utf-8″);
 - String password = URLEncoder.encode(request.getParameter(“password”),”utf-8″);
 - Cookie usernameCookie = new Cookie(“username”,username);
 - Cookie passwordCookie = new Cookie(“password”,password);
 - response.addCookie(usernameCookie);
 - response.addCookie(passwordCookie);
 - usernameCookie.setMaxAge(864000);//设置最大生存期限为 10 天
 - passwordCookie.setMaxAge(864000);
 - } 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>
 - </body>
 - </html>
 
users.jsp
- <%@page import=“java.net.URLDecoder”%>
 - <%@page import=“org.apache.tomcat.util.http.Cookies”%>
 - <%@ page language=“java” contentType=“text/html; charset=UTF-8”
 - pageEncoding=“UTF-8”%>
 - <!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>
 - <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 = URLDecoder.decode(c.getValue(),”utf-8″);//解码
 - }
 - if(c.getName().equals(“password”)) {
 - password = URLDecoder.decode(c.getValue(),”utf-8″);
 - }
 - }
 - }
 - %>
 - 用户名:<%=username %> <br />
 - 密码: <%=password %> <br />
 - </body>
 - </html>
 
