登陆界面–login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<!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>登录界面</title>
</head>
<body>
<h1>登录界面</h1>
<%
request.setCharacterEncoding("utf-8");
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" name="loginForm">
<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" value="cookieChecked"/>十天内保存登陆信息</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登陆" /></td>
</tr>
</table>
</form>
</body>
</html>
处理界面–dologin.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<!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>登陆处理</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String[] isusecookie=request.getParameterValues("isUseCookie");
if(isusecookie!=null&&isusecookie.length>0)
{
//如果点击了10天内保存信息,则保存cookie信息
//为了防止中文乱码,需要进行转码
String username=URLEncoder.encode(request.getParameter("username"), "utf-8");
String password=URLEncoder.encode(request.getParameter("password"), "utf-8");
//创建Cookie
Cookie usernameCookie=new Cookie("username",username);
Cookie passwordCookie=new Cookie("password",password);
usernameCookie.setMaxAge(864000);
passwordCookie.setMaxAge(864000);//设置最大生存期限为10天
//添加Cookie
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
}
else
{
//没用选中保存,则要删除Cookie信息
Cookie cookie[]=request.getCookies();
for(Cookie c:cookie)
{
//如果保存了Cookie信息就删除
if(c.getName().equals("username")||c.getName().equals("password"))
{
c.setMaxAge(0);//保存时间为0,就是删除
response.addCookie(c);
}
}
}
%>
<a href="user.jsp">查看用户信息</a>
</body>
</html>
获取cookie界面–user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.net.*" %>
<!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>查看用户信息</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username="";
String password="";
Cookie cookie[]=request.getCookies();
if(cookie!=null&&cookie.length>0)
{
for(Cookie c:cookie)
{
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>