Cookie

cookie小甜饼,是由W3C住址提出的,现在几乎所有的浏览器都支持cookie。

由于http是没有状态的协议,所以服务器不知道用户的身份,也就是没有办法记住用户的信息。现在我们平时如果不是在浏览一次网页,或者登陆一个网页的时,只要我们不清理浏览器中的cookie或者在cookie的登陆周期内,浏览器会记住我的信息,我们只需要登陆就好,不用在输入登陆名,密码等信息。这也就是依赖cookie实现的技术。但是如果浏览器不支持cookie或者被禁用那么cookie就会失效。

在Java中cookie是被封装到Cookie类中的,每个cookie都是Cookie的对象。cookie的提交或者获取都是可以通过request对象可以实现,cookie我感觉就是一个字典对象。

下面是我下的cookie下例子用来记录用户登录的次数:但是有点小问题:

login.jsp页面:

<%@ pagelanguage="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"errorPage="true"%>

 <%

  if("POST".equals(request.getMethod())){

  Cookie cookienamecookie=new Cookie("username",request.getParameter("username"));

  Cookie vostercookie=new Cookie("visternumber","0");

  response.addCookie(cookienamecookie);

  response.addCookie(vostercookie);

  //如果以Post方式登陆就从新建立cookie

  response.sendRedirect(request.getContextPath()+"/cookie.jsp");//重定向为cookiejsp页面

 

  }

 %>

<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title></title>

</head>

<body>

<divalign="center">

<fieldset>

<legend>登陆</legend>

<formaction="login.jsp"method="post">

<table>

<tr>

<td>账号:</td><td><inputname="username"type="text"style="width:150px"/></td>

</tr>

<tr>

<td>密码:</td><td><inputtype="password"style="width:150px"/></td>

</tr>

<tr>

<td></td><td><inputtype="submit"value="登陆"/></td>

</tr>

</table>

</form>

</fieldset>


</div>

</body>

</html>

cookie.jsp页面:

<%@pageimport="org.apache.tomcat.util.buf.UEncoder"%>

<%@ pagelanguage="java"contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"errorPage="login.jsp"%>

<%

String username="";

int visternumber=0;

Cookie[] cookies=request.getCookies();

for(int i=0;cookies!=null&&i<cookies.length;i++){

Cookie cookie=cookies[i];

if("username".equals(cookie.getName())){

username=cookie.getValue();

}else if("visternumber".equals(cookie.getName())){

visternumber=Integer.parseInt(cookie.getValue());

}

}

//if((username==null)||username.trim().equals("")){

//throw new Exception("您还没有登陆");

//}

//修改用户登陆的次数

Cookie vistercookie=new Cookie("visternumber",Integer.toString(visternumber++));

response.addCookie(vistercookie);//通过response.addCookie()覆盖已经存在的vistercookie,或有没有添加新的

%>   

<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>CookieNumber</title>

</head>

<body>

<divalign="center"style="margin:10px">

<fieldset>

<legend>登陆信息</legend>

<formaction="login.jsp"method="post">

<table>

<tr>

<td>登陆的账号:</td><td><%= username%></td>

</tr>

<tr>

<td>登陆的次数:</td><td><%= visternumber %></td>

</tr>

<tr>

<td></td><td><inputtype="button"value="刷新"onclick="location='<%=request.getRequestURI()%>?ts='+new Date().getTime();"class="button"/></td>

</tr>

</table>

</form>

</fieldset>

</div>

</body>

</html>

问题就是每次只能登陆一次,就是登陆次数为1;

cookie的不可跨域名性:cookie是由浏览器管理的,会根据域名不同操作不同的cookie,百度的事不能操作阿里的是一样,他们的域名不同。

cookie的常用属性:

int maxAge:cookie的失效时间,如果为正就是在maxAge秒之后失效,如果为负就是临时cookie就是关闭浏览器后就失效,如果为0就是删除cookie;

boolean secue:是否使用安全协议(HTTPS);

String path:该cookie的使用路径,例如“/server/”就是在/server下的程序可以访问cookie,“/”表示在本域名下,就是表示在/后的都可以访问;

int vision:cookie的版本号,0表示Netscape的cookie规范,1表示W3C规范;

String domain:可以访问该cookie的域名,如果设置为“.google.com”表示以google.com结尾都可以访问。

以上是我在学习过程中对cookie的理解;


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值