Java EE Learning-cookie use example

Source: Internet
Author: User

The Javax.servlet.http.Cookie class in Java is used to create a cookie

The main methods of the cookie class

No.

Method

Type

Describe

1

Cookie (string name, String value)

Construction method

Instantiate a cookie object, passing in the value of the Cooke name and cookie

2

Public String GetName ()

Common methods

Get the name of a cookie

3

Public String GetValue ()

Common methods

Get the value of a cookie

4

public void SetValue (String newvalue)

Common methods

Set the value of a cookie

5

public  void setmaxage (int expiry)

normal method

set the maximum save time for cookies, that is, the expiration date of the cookie, when the server sends a cookie back to the browser, If the Setmaxage method is not invoked on the server side to set the validity period of the cookie, the cookie is valid only for one session, and the user opens a browser, clicks multiple hyperlinks, accesses multiple Web resources on the server, and then closes the browser. The whole process is called a session , when the user closes the browser, the session ends, and the cookie expires, and if the cookie is set to expire on the server side using the Setmaxage method, such as setting a 30-minute Then when the server sends the cookie to the browser, the cookie will be stored on the client's hard disk for 30 minutes, and in 30 minutes, even if the browser is turned off, the cookie still exists, and within 30 minutes, the browser will bring the cookie with you when you open the browser to access the server. This allows the server side to get to the client browser passed the information in the cookie, which is the difference between the cookie settings maxage and not set MaxAge, do not set maxage, then the cookie is only valid in one session, once the user closes the browser, Then the cookie does not have, then how does the browser do this, we start a browser, it is equivalent to launch an application, and the server sends back the cookie first exists in the browser cache, when the browser is closed, the browser's cache will naturally be absent, So the cookie stored in the cache is naturally cleared, and if the cookie is set to expire, the cookie will be stored on the hard disk when the browser is closed, so that the cookie can persist.

6

public int Getmaxage ()

Common methods

Get the validity of cookies

7

public void SetPath (String uri)

Common methods

Setting a valid path to a cookie, such as setting the valid path of a cookie to "/XDP", when a browser accesses a Web resource in the "XDP" directory, it takes a cookie and, for example, sets the valid path of the cookie to "/xdp/gacl", Then the browser will only be accessed with a cookie when accessing Web resources in the directory "GaCl" in the "XDP" directory, and when accessing Web resources in the "XDP" directory, the browser is not a cookie

8

Public String GetPath ()

Common methods

Get a valid path to a cookie

9

public void SetDomain (String pattern)

Common methods

Set a valid domain for a cookie

10

Public String GetDomain ()

Common methods

Get the valid domain of a cookie

The response interface also defines a Addcookie method that is used to add a corresponding Set-cookie header field to its response header. Similarly, a getcookies method is defined in the request interface, which is used to obtain the cookie submitted by the client.

Access Cookie Code:

1 protected voiddoget (httpservletrequest request,2HttpServletResponse response)throwsservletexception, IOException {3         //TODO auto-generated Method Stub4         //set the server side to output UTF-8 encoding5Response.setcharacterencoding ("UTF-8");6         //set the browser to receive UTF-8 encoding to solve Chinese garbled problem7Response.setcontenttype ("Text/html;charset=utf-8");8PrintWriter out =Response.getwriter ();9         //gets the array of cookies passed over when the browser accesses the serverTencookie[] Cookies =request.getcookies (); One         //If the user is a first-time access, then the resulting cookie will be null A         if(Cookies! =NULL) { -Out.write ("The last time you visited is:"); -              for(inti = 0; i < cookies.length; i++) { theCookie cookie =Cookies[i]; -                 if(Cookie.getname (). Equals ("LastAccessTime")) { -Long LastAccessTime =Long.parselong (Cookie.getvalue ()); -Date Date =NewDate (lastaccesstime); + Out.write (date.tolocalestring ()); -                 } +             } A}Else { atOut.write ("This is your first time to visit this site!") "); -         } -  -         //after the user has accessed, reset the user's access time, store it in a cookie, and then send it to the client browser -Cookie cookie =NewCookie ("LastAccessTime"), System.currenttimemillis () -+ "");//Create a Cookie,cookie whose name is LastAccessTime in         //The cookie object is added to the response object so that the server outputs the cookie to the client browser when it outputs the contents of the response object. - Response.addcookie (cookie); to}

Java EE Learning-cookie use example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.