使用Cookie定製用戶界面

儘管使用Cookie的限制有許多,但用戶界面的定製用Cookie可以輕鬆搞定。下面這個是用 Cookie設置用戶界面背景色的,在IE6.0.2800、FireFox1.0均測試通過。

<%@ 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">&nbsp;</td>
  </tr>
     <tr>
       <td width="5%"><input type="radio" name="radiobutton" value="#CCCCCC"></td>
       <td width="95%" bgcolor="#CCCCCC">&nbsp;</td>
  </tr>
     <tr>
       <td width="5%"><input type="radio" name="radiobutton" value="#FFFF99"></td>
       <td width="95%" bgcolor="#FFFF99">&nbsp;</td>
  </tr>
     <tr>
       <td width="5%"><input type="radio" name="radiobutton" value="#00FF99"></td>
       <td width="95%" bgcolor="#00FF99">&nbsp;</td>
  </tr>
     <tr>
       <td width="5%"><input type="radio" name="radiobutton" value="#FF0033"></td>
       <td width="95%" bgcolor="#FF0033">&nbsp;</td>
  </tr>
     <tr>
       <td width="5%"><input type="radio" name="radiobutton" value="#0000FF"></td>
       <td width="95%" bgcolor="#0000FF">&nbsp;</td>
  </tr>
     <tr>
       <td width="5%"><input type="radio" name="radiobutton" value="#C51AFD"></td>
       <td width="95%" bgcolor="#C51AFD">&nbsp;</td>
  </tr> 
  
     <tr>
       <td colspan='2' align="right"><input type="submit" name="Submit" value="Submit">&nbsp;</td>
  </tr>  
 
 </table>
 </form>
<%}%>
</body>
</HTML>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章