cookies

来源:互联网 发布:数据库基础教程判断题 编辑:程序博客网 时间:2024/06/03 21:22

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
原创粉丝点击