04JAVAWEB-03 conversational Technology in Java combat

Source: Internet
Author: User

First, session technology introduction 1. What is a session and why is session technology required?

Session: From opening a browser to visiting a website, the process of closing this browser is called a session. The HTTP protocol is state.

2. Classification of Session Technology

Client Storage technology: Cookies

Server-side storage technology: Session

What is the difference between a cookie and a session?

1) The user's information stored in the cookie exists on the client, the session stores the data on the server side, but the encoding ID of the session needs to be stored in the client

2) Security The relative security of the cookie session is relatively safe

3) Performance: According to the actual situation

Second, the session technology cookie

Cookie technology is the storage of data to the client

1. How to write a cookie to the client

1) Create Cookie Object

Cookie cookie = new Cookie (name,value);

2) write a cookie to the client

Response.addcookie (cookie);

Client: The client resolves the HTTP response with a cookie in the response header, and the client automatically stores the cookie information in the client's cache

2. How to get a cookie from the client

1) obtain all cookies that are carried by the client

Cookies[] Request.getcookies ();

2) to obtain a specific cookie

Traversal of all Cookies

Get the name of a cookie by GetName ()

Get the value of a cookie by GetValue ()

3. Details when setting a cookie

1) session-level cookies and persistent-level cookies

Session-Level Cookie: Session End Cookie is emptied

Persistent-level cookies: Store cookies on disk

Time saved on disk

Cookie.setmaxage (seconds);

Note: If you want to delete the cookie that the disk has stored

Set the persistence time of the cookie with the same name to 0

The path of the cookie to be deleted is set to the same path as the cookie stored on disk (that is, the setpath of two cookies)

2) Set the path of the cookie to carry

Cookies are carried by default in the directory where the cookie resources are generated.

Cookie.setpath (path to carry cookies);

Set the carry path of the cookie to/from,/on behalf of the Web server

For example:

Cookie.setpath ("/"), the cookie is carried on all resources under the Access Web server

Cookie.setpath ("Home"), which carries cookies

3) Set up a three-party cookie (learn)

Cookie.setdomain (domain name);

Three-party cookies have offensive

4. The above code implementation
1Response.setcontenttype ("Text/html;charset=utf-8");2         //1. Cookie for creation time3SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");4String accesstime = Format.format (NewDate ());5Cookie cookie =NewCookie ("Accesstime"), accesstime);6         //1.1 Set the duration of the cookie to persist----time stored on disk7         //cookie.setmaxage (60*10);8         //1.2 Set the carrying path of the cookie/on behalf of the Web server9Cookie.setpath ("/day11_208/abc/accesstime");Ten         //2. Write a cookie to the client One Response.addcookie (cookie); A         //3. The cookie that gets the time that the client carries -String accesstime_client =NULL; -cookie[] Cookies =request.getcookies (); the         if(cookies!=NULL){ -              for(Cookie coo:cookies) { -                 //Take out the name of a cookie object -String CookieName =coo.getname (); +                 if("Accesstime". Equals (CookieName)) { -                     //Remove the value of the cookie +Accesstime_client =Cookie.getvalue (); A                 } at             } -         } -          -         //4. Display the last access time for the user -         if(accesstime_client!=NULL){ -Response.getwriter (). Write ("Your last access time is:" +accesstime_client); in}Else{ -             //first time Visit toResponse.getwriter (). Write ("You are the first access"); +}
Third, the Session1 of conversational technology. How to create a session/to get a session

HttpSession session = Request.getsession ();

The internal principle of the above method:

When request calls the GetSession method, the interior checks to see if the user has an area of memory within the Web application, and if there is an address that returns the memory area directly, create a new session area if not.

What does the server side determine if the user already has a session?

Based on the session's ID--->jsessionid

2. The life cycle of the session

Created: First call to Request.getsession ()

Destroyed:

1) Server shutdown session Destruction

2) Session timeout default 30 minutes

Calculate when the point starts: 30 minutes after the last operation of the site

3) Manually destroy session

Session.invalidate ();

3. Session is a domain object

The session is a cookie, and the same session is marked with the same jsessionid.

If the browser is turned off, the session is re-created when the access resource is opened

How to persist session-----> Store Jsessionid cookie Persistence

Grab bag tool to get:

SET-COOKIE:JSESSIONID=6232D4782FC69B1D780261E93DFA5FBB; path=/day11_208/;

Manually create a cookie on top of the cookie based on one more max-age

1         // Create a cookie store manually jsessionid 2         // SET-COOKIE:JSESSIONID=6232D4782FC69B1D780261E93DFA5FBB; path=/home/; 3         New Cookies ("Jsessionid", Session.getid ()); 4         Cookie.setpath ("/home/"); 5         Cookie.setmaxage (60*10); 6         7         Response.addcookie (cookie);

If the client disables cookies, the client cannot be lost in storage Cookie,jsessionid, resolved?

Solution: URL rewrite, using semicolons after each URL address; stitching Jsessionid

1         HttpSession session = request.getsession (); 2         System.out.println (Session.getid ()); 3         String url = "/home/index.jsp"; 4         url = response.encoderedirecturl (URL); 5         System.out.println (URL); 6         Response.sendredirect (URL);

Http://localhost/home/index.jsp;jsessionid=377B2F0501FF9FE643D7D88F4E883FFD

04JAVAWEB-03 conversational Technology in Java combat

Related Article

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.