cookie操作的例子

---摘自互联网

<!--
cookie.jsp

This  JSP  makes  use  of  3  custom  tags:
One  to  set  a  Cookie  using  attributes  that  were  entered  in  a  submitted  form.  <setcookie>
One  to  display  all  the  Cookies  associated  with  this  request.  <getcookies>
One  to  display  a  single  specific  Named  Cookie  <getcookie>

Note  the  use  of  <getcookies>  to  populate  the  dropdown  list  rather  than  for  general  display.
-->

<%@  taglib  uri  =  "exampleTLD"  prefix="ex"  %>
<%
String  errorMessage  =  "";
String  nameToSet  =  request.getParameter("nameToSet");  //required  in  order  to  Set  a  cookie
String  value  =  request.getParameter("value");  //required  in  order  to  Set  a  cookie
String  domain  =  request.getParameter("domain");
String  maxage  =  request.getParameter("maxage");
String  path  =  request.getParameter("path");
String  secure  =  request.getParameter("secure");
String  comment  =  request.getParameter("comment");
String  setACookie  =  request.getParameter("setACookie");
String  getAllCookies  =  request.getParameter("getAllCookies");
String  getACookie  =  request.getParameter("getACookie");
String  nameToGet  =  request.getParameter("nameToGet");  //required  in  order  to  Get  a  cookie  by  name.
%>

<html>
<head>
<title>cookie.jsp</title>
</head>

<body>
<center>
<h1>cookie.jsp</h1>

<% 
if(  setACookie  !=  null  &&  (nameToSet  ==  null  //  nameToSet.trim().length()  ==  0  //
value  ==  null  //  value.trim().length()  ==  0)  )
errorMessage  =  ("<font  color=red><h1>!  Both  Name  and  Value  are  required  to  Set  a  cookie.</h1></font>");

else  if(getACookie  !=  null  &&  (nameToGet  ==  null  //  nameToGet.trim().length()  ==  0)  )

errorMessage  =  ("<font  color=red><h1>!  A  Name  is  required  in  order  to  Get  a  Cookie  by  it's  name.</h1></font>");
%>

<%=  errorMessage  %>

<p>
<b>
You  must  enter  a  value  for  all  of  the  fields  which  aren't  marked  as  optional.<br>
The  Comment  field  can  only  be  used  with  Version  1  (RFC2109)  cookies.
</b>

<br>

<p>
<form  action=cookie.jsp  method=POST>
<table>
<tr><td><b>Name  :</b></td><td><input  type=text  size=50  name="nameToSet"></td></tr>
<tr><td><b>Value  :</b></td><td><input  type=text  size=50  name="value"></td></tr>
<tr><td><b>Domain  (optional)  :</b></td><td><input  type=text  size=50  name="domain"></td></tr>
<tr><td><b>MaxAge  (optional):</b></td><td><input  type=text  size=50  name="maxage"></td></tr>
<tr><td><b>Path  (optional):</b></td><td><input  type=text  size=50  name="path"></td></tr>
<tr><td><b>Secure  (optional):</b></td><td><input  type=text  size=50  name="secure"></td></tr>
<tr><td><b>Comment  (optional)  :</b></td><td><input  type=text  size=50  name="comment"></td></tr>
</table>

<input  type=submit  name="setACookie"  value="Set  A  Cookie"> 
<input  type=reset>
<br><br><br>
<input  type=submit  name="getAllCookies"  value="Get  All  Cookies"> 
<br><br><br>
</form>
</p>

<p>
<form  action=cookie.jsp  method=POST>
<table  border=0>
<tr>
<td>
<b>Name  :</b>
</td>

<td>
<select  name="nameToGet">
<ex:getcookies>
<option><%=cookieName%>
</ex:getcookies>
</select>
</td>

<td>
<input  type=submit  name="getACookie"  value="Get  A  Cookie  By  Name">
</td>
</tr>
</table> 
</form> 
</p>

<%
if(setACookie  !=  null  &&  (nameToSet  !=  null  &&  nameToSet.trim().length()  >  0  &&
value  !=  null  &&  value.trim().length()  >  0)  )

%>
<ex:setcookie  name="<%=nameToSet%>"  value="<%=value%>"  domain="<%=domain%>"  maxage="<%=maxage%>"  path="<%=path%>"  secure="<%=secure%>"  comment="<%=comment%>"  /> 
<%  }  %> 


<%
if  (getAllCookies  !=  null)
{
//Display  all  the  cookies
%>

<h1>Here  are  the  cookies  that  were  in  the  Request.</h1>

<table  border=1>
<th>Name</th>
<th>Value</th>

<ex:getcookies>
<tr>
<td  align=center>
<%=cookieName%>
</td>
<td  align=center>
<%=cookieValue%>
</td>
</tr>
</ex:getcookies>
</table>
<%  }  %>

<%
if  (getACookie  !=  null  &&  nameToGet  !=  null  &&  nameToGet.trim().length()  >  0)
{
//Display  a  single  named  cookie
%>
<h1>Here  is  the  Cookie  you  requested.</h1>

<table  border=1>
<th>Name</th>
<th>Value</th>

<ex:getcookie  name="<%=nameToGet%>">
<tr>
<td  align=center>
<%=cookieName%>
</td>
<td  align=center>
<%=cookieValue%>
</td>
</tr>
</ex:getcookie>
</table>
<%  }  %> 

</center>
</body>
</html>