4, Servlet Cookie to the user information storing web pages.

Servlet  Cookie processing

Cookie is stored on the client computer on the text file , and retain a variety of tracking information . Apparently Java Servlet support HTTP Cookie .

Returns the user identification comprises three steps:

  • Server scripts to the browser sends a group of Cookie. For example: name, age or identification number.
  • Browser this information is stored on the local computer for future use.
  • When the next time the browser to the Web server transmits any request, the browser will the Cookie information is sent to the server, the server will use this information to identify the user.

This chapter will explain You how to set or reset Cookie , how to access them, and how they are deleted .

// the Servlet needs to process cookies Chinese encoding and decoding, as follows: 

String STR    = java.net.URLEncoder.encode ( "Chinese", "UTF-. 8");             // encoding Encoder 
String STR = java.net.URLDecoder .decode ( "encoded string", "UTF-. 8");    // decode decoder

Cookie analysis

Cookie is usually set in the HTTP header information in (although JavaScript can also be set directly in a browser Cookie). Set Cookie Servlet sends the header information following:

<! - the Set-cookies header contains: a name value pair; a GMT date; a path; domain -> 
<! - name and URL value will be encoded. -> 
! <- the Expires field is an instruction that tells the browser after a given period of time and date "forget" the Cookie. -> 

HTTP / 1.1 200 the OK
 a Date: Fri, 04 Feb 2000 21:03:38 GMT
 Server: the Apache / 1.3.9 (UNIX) PHP / 4.0b3
 the Set-Cookie: name = xyz; the Expires = catalog on Friday, 04-Feb-07 22:03:38 GMT; 
                 path = /; Domain = runoob.com Connection: use Close
 Content-Type: text / HTML

If your browser is configured to store Cookie, it will retain this information until the expiration date.

If the user's browser to point to any match of the Cookie path and domain of the page , it will re-send Cookie to the server.

Browser header information may be as follows:

GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz

Servlet can by request method  request.getCookies ()  to access Cookie, the method returnsCookie  object array .

Servlet Cookie method

 The following is the Servlet Cookie operations useful to use in the list of methods .

序号 方法 & 描述
1 public void setDomain(String pattern)
该方法设置 cookie 适用的,例如 runoob.com
2 public String getDomain()
该方法获取 cookie 适用的,例如 runoob.com
3 public void setMaxAge(int expiry)
该方法设置 cookie 过期的时间(以为单位)。如果这样设置,cookie 只会在当前 session 会话中持续有效
4 public int getMaxAge()
该方法返回 cookie 的最大生存周期(以为单位),默认情况下,-1 表示 cookie 将持续下去,直到浏览器关闭
5 public String getName()
该方法返回 cookie 的名称。名称在创建后不能改变
6 public void setValue(String newValue)
该方法设置与 cookie 关联的
7 public String getValue()
该方法获取与 cookie 关联的
8 public void setPath(String uri)
该方法设置 cookie 适用的路径。如果您指定路径,与当前页面相同目录下的(包括目录下的)所有 URL 都会返回 cookie。
9 public String getPath()
该方法获取 cookie 适用的路径
10 public void setSecure(boolean flag)
该方法设置布尔值,表示 cookie 是否应该只在加密的(即 SSL)连接上发送。
11 public void setComment(String purpose)
设置cookie的注释。该注释在浏览器向用户呈现 cookie 时非常有用。
12 public String getComment()
获取 cookie 的注释,如果 cookie 没有注释则返回 null

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cookie settings by Servlet

(1) create a Cookie object:

// call the Cookie constructor with a cookie name and cookie value
 // cookie name and cookie value are strings. 

The cookie Cookie = new new Cookie ( "Key", "value");

(2) set the maximum life cycle:

// use the method to specify setMaxAge cookie, capable of maintaining effective period of time (in seconds).
// The following sets a maximum validity period of 24 hours cookie. 

cookie.setMaxAge ( 60 * 60 * 24);

(3) transmits a response to the HTTP Cookie header:

// add the HTTP response header using Cookie response.addCookie 

response.addCookie (Cookie);

Cookie read by Servlet

Delete Cookie by Servlet

Delete Cookie is very simple. If you want to delete a cookie, you only need to follow these three steps:

  • Read an existing cookie, and it is stored in the Cookie object.
  • Use  setMaxAge ()  method to set the cookie age of zero to delete an existing cookie.
  • This cookie added to the response headers .

 

Guess you like

Origin www.cnblogs.com/Strugglinggirl/p/10986465.html