<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>cookie</title>
</head>
<body>
<%
String welcome="第一次访问";
String[] info=new String[]{"","",""};
Cookie[] cook=request.getCookies();
if(cook!=null)
{
for(int i=0;i<cook.length;i++)
{
if(cook[i].getName().equals("mrCookInfo"))
{
info=cook[i].getValue().split("#");
welcome=",欢迎回来!";
}
}
}
%>
<%=info[0]+welcome%>
<form action="show1.jsp" method="post">
<ul style="line-heigth:23">
<li>姓 名:<input name="name" type="text" value="<%=info[0]%>"/></li>
<li>出生日期:<input name="brithday" type="text" value="<%=info[1]%>"/></li>
<li>邮箱地址:<input name="mail" type="text" value="<%=info[2]%>"/></li>
<li><input type="submit" value="提交"/></li>
</ul>
</form>
</body>
</html>
通过response的addcookie方法将其发送到客户端
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>show the newfile1</title>
</head>
<body>
<%
String name=request.getParameter("name");
String brithday=request.getParameter("brithday");
String mail=request.getParameter("mail");
Cookie myCook=new Cookie("mrCookInfo",name+"#"+brithday+"#"+mail);
myCook.setMaxAge(60*60*24*365); //设置cookie有效期
response.addCookie(myCook);
%>
表单提交成功
<ul style="line-height:24px">
<li>姓名:<%=name %>
<li>生日:<%=brithday %>
<li>电子邮箱:<%=mail %>
<li><a href="index.jsp">返回</a>
</ul>
</body>
</html>