<%@ page contentType="text/html;charset=GB2312" %>
<HTML>
<HEAD>
<TITLE>Cookie Test</TITLE>
</HEAD>
<%
final String COOKIE_NAME = "my.cokie.bgcolor" ;
String defaultValue = "#FFFFFF" ; //Default
boolean haveSet = false ;
/*存cookie*/
String radioValue = request.getParameter("radiobutton");
if(radioValue!=null){
Cookie cookie = new Cookie(COOKIE_NAME, radioValue);
cookie.setMaxAge(60*60*24) ; //one day
response.addCookie(cookie) ;
defaultValue = radioValue ;
haveSet = true ;
}
/*取cookie*/
Cookie[] cookies = request.getCookies();
if(cookies!=null && !haveSet){
for (int i = 0; i < cookies.length; i++) {
if(cookies[i].getName().equals(COOKIE_NAME)){
defaultValue = cookies[i].getValue();
haveSet = true ;
break ;
}
}
}
%>
<body bgcolor="<%=defaultValue%>">
【用Cookie存放用戶界面背景色】<BR>
<%if(!haveSet){%>
<form name="f" method ="POST" action="mycookie.jsp">
<table width="35%" border="1">
<tr>
<td width="5%"><input type="radio" name="radiobutton" value="#FFFFFF" checked></td>
<td width="95%" bgcolor="#FFFFFF"> </td>
</tr>
<tr>
<td width="5%"><input type="radio" name="radiobutton" value="#CCCCCC"></td>
<td width="95%" bgcolor="#CCCCCC"> </td>
</tr>
<tr>
<td width="5%"><input type="radio" name="radiobutton" value="#FFFF99"></td>
<td width="95%" bgcolor="#FFFF99"> </td>
</tr>
<tr>
<td width="5%"><input type="radio" name="radiobutton" value="#00FF99"></td>
<td width="95%" bgcolor="#00FF99"> </td>
</tr>
<tr>
<td width="5%"><input type="radio" name="radiobutton" value="#FF0033"></td>
<td width="95%" bgcolor="#FF0033"> </td>
</tr>
<tr>
<td width="5%"><input type="radio" name="radiobutton" value="#0000FF"></td>
<td width="95%" bgcolor="#0000FF"> </td>
</tr>
<tr>
<td width="5%"><input type="radio" name="radiobutton" value="#C51AFD"></td>
<td width="95%" bgcolor="#C51AFD"> </td>
</tr>
<tr>
<td colspan='2' align="right"><input type="submit" name="Submit" value="Submit"> </td>
</tr>
</table>
</form>
<%}%>
</body>
</HTML>