cookies


cookies
**************************从cookies中取值判断**********************************
小脚本
<%
  String name="";
  String pwd="";
  //获得cookie所有数组
Cookie[] cookies=request.getCookies();
if(cookies!=null){

for(Cookie co:cookies){
//获得cookie中的name是否等于输入进来的name
if(co.getName().equals("name")){
//转换name的字符集编码
name=URLEncoder.encode(co.getValue(), "UTF-8");
}
//判断cookies中的密码和输入进来的密码是否相等
if(co.getName().equals("pwd")){
//相等把cookies中的值复制给pwd
pwd=co.getValue();
}
  }
}
%>
<form action="Server/se.jsp" method="post">
  <table>
  <tr>
  <td align="center">请输入用户名</td>
  <td><input type="text" name="usname" value="<%=name %>"/></td>
  </tr>
  <tr>
  <td align="center">请输入密码</td>
  <td><input type="password" name="pwd" value="<%=pwd %>"/></td>
  </tr>
  <tr>
<td align="center"><input type="checkbox" name="ck" value="1"/></td>
   <td align="center">确定保存信息</td>
  </tr>
  <tr>
  <td align="center"><input type="submit" value="登陆"/></td>
  <td align="center"><input type="reset" value="重置"/></td>
  </tr>
  </table>
</form>
**************************往cookies中放值****************************************
<%

request.setCharacterEncoding("UTF-8");
String usname=request.getParameter("usname");
String pwd=request.getParameter("pwd");
String cks=request.getParameter("ck");
//判断如果选择保存数据则执行以下代码
if(cks.equals("1")){
Cookie us=new Cookie("name",URLEncoder.encode(usname, "Utf-8"));
Cookie pd=new Cookie("password",pwd);
//设置cookies的实效时间单位一秒计算
us.setMaxAge(5);
pd.setMaxAge(10*60);
us.setPath("/");
us.setPath("/");
//cookies中塞入数值
response.addCookie(us);
response.addCookie(pd);
}
%>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值