<%@ 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>Cookie使用的简单例子/title>
</head>
<body >
<%
Cookie[] cook = request.getCookies();//获取本地所有的Cookie对象
String[] userMessage = new String[] { "", "" }; //用户存储从cookie中获取的用户名和密码
if (cook != null) {
for (int i = 0; i < cook.length; i++) {
if (cook[i].getName().equals("userInfo")) {
userMessage = cook[i].getValue().split("#");//通过分割split获取用户名和密码.
} }
}
%> <p>本页面仅用于测试,只有输入用户名为张运涛,密码为123456时,提示正常登录,否则提示用户名密码错误;正常登入后会把用户名与密码保存在cookie中便于用户登入</p> <form action="check.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input name="userName" type="text"
value="<%=userMessage[0]%>"></td>
</tr> <tr>
<td>密码</td>
<td><input type="password" name="password"
value="<%=userMessage[1]%>"></td>
</tr> <tr>
<td> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"> <input
type="reset" value="重置"></td>
</tr> </table> </form> </body>
</html>
check.jsp
<%@ 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> <%
request.setCharacterEncoding("utf-8"); String userName = request.getParameter("userName");
String password = request.getParameter("password"); if (userName.equals("张运涛") && password.equals("123456")) {
Cookie mycook=new Cookie("userInfo",userName+"#"+password); //设置cookie对象
mycook.setMaxAge(60*60*20*365); //设置cookie有效期
response.addCookie(mycook);//把cookie对象发送到客户端进行存储
%> <script>
alert("登录成功!");
window.location.href = "login.jsp";
</script> <%
} else {
%>
<script>
alert("登录失败,用户名或者密码错误");
window.location.href = "login.jsp";
</script> <%
}
%> </body>
</html>
效果图