web 開發中使用cookie 詳解

action中生成cookie:


    if (rememberMe != null) {                                // 創建Cookie
     Cookie cokie_user = new Cookie("username", userName);
     Cookie cokie_password = new Cookie("password", passWord);
     cokie_user.setMaxAge(60 * 60); // 失效時間爲一小時
     cokie_password.setMaxAge(60 * 60);
     response.addCookie(cokie_user);
     response.addCookie(cokie_password);
    }
                
jsp中如此調用:

 <%@ page language="java" contentType="text/html; charset=gb2312"  pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html locale="true">
  <head>
   
    <title><bean:message key="loginform.title.displayname"/></title>
   
   </head>
   <body>
<%
 
   Cookie[] cook=request.getCookies();
   String userid="";
   String password="";
   if(cook!=null){
   for(int i=0;i<cook.length;i++){
    
         if(cook[i].getName().equals("username")){
            userid=cook[i].getValue();
          }
         if(cook[i].getName().equals("password")){
            password = cook[i].getValue();
         }
     }
    
   }
 
%>
   <html:messages id="errors" message="true">
      <bean:write name="errors"/>
   </html:messages>
  <html:javascript formName="LoginForm" cdata="false" />
  <br>
  <br>
     <bean:message key="loginform.title.edit"/>
 
  <bean:message key="loginform.welcome"/>

<html:form action="/login.do" focus="userName"  οnsubmit="return validateLoginForm(this);" >
<table width="100%">
    <tr>
        <td colspan="2">
        </td>
    </tr>
    <tr>
        <th><bean:message key="loginform.userName.dispalyname"/></th>
        <td>
            <html:text property="userName" size="20" maxlength="20" value="<%=userid%>"/>
        </td>
    </tr>
    <tr>
        <th><bean:message key="loginform.Password.dispalyname"/> </th>
        <td>
            <html:password property="password" size="20" maxlength="20" value="<%=password%>"/>  
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <html:checkbox property="rememberMe" value="true">
           <bean:message key="loginform.rememberMe.dispalyname"/>
            </html:checkbox>
        </td>
    </tr>
    <tr>
        <td></td>
        <td>
            <html:submit styleClass="st" property="submit" >
            <bean:message key="loginform.button.login"/>
            </html:submit> 
        </td>
    </tr>
</table>
</html:form>
<br>
<br>
<p>
    <bean:message key="loginform.message.notmember"/> <a href="profiletil.do" ><bean:message key="loginform.signup.displayname"/> </a>
    <bean:message key="loginform.message.account"/>
</p>
<br>
 <bean:message key="loginform.message.version"/> &middot; <bean:message key="loginform.message.copyright"/> &copy;
  <bean:message key="loginform.message.produce.date"/>
<a href="http://www.baidu.com"><bean:message key="loginform.message.relation"/></a>
  </body>
</html:html>

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