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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值