2016.9.13 Session Learning

Source: Internet
Author: User

---restore content starts---

Session

Open a browser to visit a website until the site is closed, the process is called a session

Cookies

A client-side technology that stores the client's resources in response to only the string

Cookies are sent to the browser in the response header

Browser supports 20 cookies per server, a total of 300 each cookie 4k size

To implement the last time the client access is logged

1 Get client-owned cookies

Reponse.setcontettype ("Text/html;charset=utf-8");

PrintWriter pw= Reponse.getwriter ();

Cookie [] cookies=resques.getcookies ();

2 Find the cookie that stores the last login time from the cookie

if (cookies!=null)

{

for (int i=0;i<cookies.length;i++)//cookie is a key-value pair, which is a string-type

{String name = Cookies[i].getname (); Get the name of a cookie

if ("LastAccessTime". Equals (name))

String value =cookies[i].getvalue (); Value at this time is the number of milliseconds

Pw.wirte (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (new Date (Long.parselong)));//Time formatting

}

}

The client that sent the time of the login as a cookie

Cookie cookie= New Cookie ("LastAccessTime", System.currenttimemillis () + ""); Must be of type string

Send cookies to clients

Reponse.addcookie (cookie);

Cookies are stored in the browser's memory by default, and once the browser is closed, the stored cache is erased

Use Setmaxage to set cookie survival time, default unit is seconds

Setmaxage (-1) default is-1 delete at end of session

0 when cookies are not saved

Positive number is life cycle time

What conditions are met the client sends a cookie to the server side.

1 The resource path being accessed, the previous segment to match.

Cookie.setpath (Request.getcontextpath ());

Session

One client corresponds to a session, and only one sessions object in the same conversation

SetAttribute () GetAttribute () implements session storage

On the server side of a technology, session can save objects

Session.setmaxinactiveinterval (10);//Set inactive time 10s automatic destruction;

Request.getsession (true); the existence of a session using the existing, does not exist will create new;

Request.getsession (false); The existence of a session using the presence of a nonexistent will not be created.

2016.9.13 Session Learning

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.