javaweb--Session Technology Cookies & Sessions

Source: Internet
Author: User

Session Technology

Because the HTTP protocol is stateless , which means that each client accesses a server-side resource, the server does not know who the client is , and therefore requires session technology to identify the state of the client. session technology is to help the server remember the client state (differentiate between different clients)

from opening a browser to access a site, the entire process of closing the browser becomes a session. session technology is the recording of the state and data of the client in this session.
Session technology is divided into cookie and session:

    • Cookies: Data is stored locally on the client, reducing server-side storage pressure, security is not good, clients can clear cookies
    • Session: Store data on the server side, the security is relatively good, increase the pressure of the server
First, Cookie technology

Cookie technology is the technology that stores the user's data to the client .

1. Set Cookies
Cookie cookie = new Cookie ("username", "x5456");     Create Cookie Object Cookie.setmaxage (10*60);     Set the cookie to be valid for 10min, the default is to 0 clear Cookiecookie.setpath ("/web16") when the browser is turned off;   Set the cookie carrying path, which by default puts the entire Web application (/WEB16) on Cookieresponse.addcookie (cookie);     Send the information stored in the cookie to the client

Note: If you do not set the persistence time, the cookie is stored in the browser's memory, the browser shuts down cookie information is destroyed (session-level cookie), and if the persistence time is set, the cookie information is persisted to the browser's disk file

A cookie can only save strings in Chinese

2. Get the specified cookie

Cookie information is sent to the server in the form of a request header

cookie[] cookies = request.getcookies ();    Get the cookie list for (cookie cookie:cookies) {     //traversal cookie array, get the cookie if the cookie is the name we want    (Cookie.getname (). Equals ("username")) {        String cookievalue = Cookie.getvalue ();        System.out.println (Cookievalue);    }}
3. Clearing cookies
Cookie C = new Cookie ("name", "" ");   Key is the Cookiec.setmaxage (0) to be cleared; C.setpath ("/web16");    The path should be the same as before Response.addcookie (c);
Second, session technology

Session technology is the technology that stores data on the server side, creating a memory space for each client to store the customer's data , but the client needs to carry an identity ID each time to go to the server to find its own memory space . So the implementation of the session is based on the cookie,session need to use cookies to store the unique identity of the customer Jsessionid

1. Access data to the session domain

The session is also a domain object, so you can also use the Get/set/remove method

/* * the Request.getsession () method internally determines if the client already exists on the server side * If the client does not exist on this server then a new session object will be created * If the client already exists in this server session to obtain an already existing session return */httpsession session = Request.getsession (); Session.setattribute ("Name", " x5456 ");   Set sessionstring Jsessionid = Session.getid ();    Gets the number of the Session object Idcookie cookie = new Cookie ("Jsessionid", Jsessionid);    Manually create a cookie that stores Jsessionid, set the persistent time Cookie.setpath ("/web16") for the cookie, cookie.setmaxage (60*10); Response.addcookie (cookies);
2. Get the value in session
HttpSession session = Request.getsession (); Gets the domain objects object name = Session.getattribute ("name"); System.out.println ((String) name);
the life cycle of the session object

Created: The first time you execute Request.getsession () is created

Destroyed:

1) when the server (not normal) shuts down

2) session expiration/expiration (default 30 minutes)

Starting point of time: Start timing from a resource that is not operating on the server side

You can configure the expiration time in the project's Web. xml

<session-config>

<session-timeout>30</session-timeout>

</session-config>

3) Manual Destruction

Session.invalidate ();

Scope of Action:
Default in one session , that is, any resource in a session is common

javaweb--Session Technology Cookies & Sessions

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.