<%@ page language="java" contentType="text/html; charset=GBK"%>
<%
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
String strName=request.getParameter("Name");
String strpassword=request.getParameter("password");
Cookie cookies[]=request.getCookies();
if(strName==null&&strpassword==null){
strName="";strpassword="";
}
else if(("sa").equals(strName)&&("123").equals(strpassword)){
if(request.getParameter("c")!=null) {
Cookie a=new Cookie("Name",strName);
a.setMaxAge(60*60);
response.addCookie(a);
Cookie b=new Cookie("password",strpassword);
b.setMaxAge(60*60);
response.addCookie(b);
}
else {
Cookie a=new Cookie("Name",strName);
a.setMaxAge(0);
response.addCookie(a);
Cookie b=new Cookie("password",strpassword);
b.setMaxAge(0);
response.addCookie(b);
}
%> <jsp:forward page="success.jsp"/> <%
}
else {
Cookie a=new Cookie("Name",strName);
a.setMaxAge(0);
response.addCookie(a);
Cookie b=new Cookie("password",strpassword);
b.setMaxAge(0);
response.addCookie(b);
%>
<jsp:forward page="fail.jsp"/> <% }
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("Name"))
strName=cookies[i].getValue();
if(cookies[i].getName().equals("password"))
strpassword=cookies[i].getValue();
}
}%>
<center>
<html>
<BODY>
<h1>利用Cookie对象把数据记录到浏览器</h1><hr>
<form name="loginForm" method="post" action="cookie.jsp" >
用户名:<input type="text" SIZE="10" name="Name" value="<% if(strName!=null) out.println(strName);%>"><br><br>
密 码: <input type="password" SIZE="10" name="password" value="<% if(strpassword!=null) out.println(strpassword);%>"><br><br>
Cookie保存时间:
<input type="checkbox" name="c" value="保存Cookie1">保存Cookie1
<input type="submit" value="登录">
</form>
</body>
</html>
</center>