總共分爲三個界面,第一個界面:登錄界面
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.net.*"
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>
<%
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");
}
}
}
%>
<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"
/> 十天內免密碼登錄</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登錄"/></td>
</tr>
</table>
</form>
</body>
</html>
第二個界面:用戶請求處理界面
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.net.*"
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[] isuseCookie=request.getParameterValues("isuseCookie");
if(isuseCookie!=null&&isuseCookie.length>0){
//保存用戶信息到Cookie
//特別說明:防止中文亂碼,URLEncodeer.encode(String "字符集");
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);
//設置Cookie保存的時間,單位秒
usernameCookie.setMaxAge(864000);
passwordCookie.setMaxAge(864000);
//添加到Cookie
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
}
else{
//需要清空Cookie
Cookie[] cookie =request.getCookies();
if(cookie!=null&&cookie.length>0){
for(Cookie c:cookie){
if(c.getName().equals("username")||c.getName().equals("password")){
c.setMaxAge(0);//設置Cookie失效
response.addCookie(c);//重新保存Cookie
}
}
}
}
%>
<br>
<br>
<a href="users.jsp">用戶信息</a>
</body>
</html>
第三個界面:用戶界面
<%@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[] 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 %>
密碼:<%= password %>
</body>
</html>
可以記住用戶十天的登錄信息,十天內面密碼登錄