Jsp session details (Part 1)

Source: Internet
Author: User

Address: http://www.egzcn.com/article/webbc/JSP/2006-03-22/2003.html

 

 

Abstract: Although the session mechanism has been used in web applications for a long time, many people still do not know the nature of the session mechanism, and even cannot correctly apply this technology. This article will discuss in detail the working mechanism of the session and provide answers to common problems when the session mechanism is applied in Java web applications.

Directory:
I. Term session
Ii. HTTP protocol and status maintenance
3. Understanding cookie Mechanism
4. Understanding the session mechanism
5. Understand javax. servlet. http. HttpSession
Vi. HttpSession FAQs
7. Cross-Application session sharing
VIII. Summary
References

I. Term session
In my experience, the word session is abused only after transaction. What's more interesting is that transaction and session have the same meaning in some contexts.

Session, which is often translated into sessions in Chinese. Its original meaning refers to a series of actions/messages that start and end, for example, a series of processes from picking up a phone call and dialing to hanging up a phone call can be called a session. Sometimes we can see this: "during a browser session ,... the term session here uses its original meaning, which refers to the period from opening a browser window to closing it. The most confusing is the phrase "a user (client) is in a session", which may refer to a series of actions of the user (generally a series of actions related to a specific purpose, for example, an online shopping process, from login to purchase of goods to checkout and logout, is also called a transaction. However, sometimes it may only refer to a connection, it may also be the meaning ①. The difference between them can only be inferred by context ②.

However, when a session is associated with a network protocol, it often implies two meanings: "connection-oriented" and "/" persistence, "connection orientation" refers to the establishment of a communication channel before the communication parties establish a communication channel, such as a phone call, until the other party receives a telephone communication. In contrast, it refers to writing a letter, when you send a letter, you cannot confirm whether the address of the other party is correct. The communication channel may not be established, but for the sender, the communication has started. "Keep status" means that the communication party can associate a series of messages so that messages can be mutually dependent, for example, a waiter can recognize an old customer who has visited the store again and remembers that the customer still owes a dollar to the store. Examples of this type include "One TCP session" or "one POP3 session" ③.

In the era of vigorous development of web servers, session semantics in the context of web development has been expanded, it refers to a kind of solution that maintains the status between the client and the server. Sometimes session refers to the storage structure of this solution, such as "Saving xxx in session" ⑤. Various languages used for web development provide support for this solution to a certain extent. Therefore, in a specific language context, session is also used to refer to the solution of this language, for example, the javax. servlet. http. httpSession is short for session 6.

As this confusion cannot be changed, the use of session in this article will also have different meanings according to the context.
In this article, we use the Chinese "browser session period" to express the meaning ①, the "session mechanism" to express the meaning ④, and the "session" to express the meaning ⑤, use the specific "httpsession" to express the meaning 6

Ii. http protocol and status maintenance
The HTTP protocol itself is stateless, which is consistent with the original purpose of the HTTP protocol. The client simply needs to request the server to download some files, there is no need to record the previous behavior of each other on both the client and server. Each request is independent, like a customer, a vending machine, or a common (non-membership) the relationships between supermarkets are the same.

But smart (or greedy ?) People soon discovered that providing on-Demand dynamic information will make the web more useful, just like adding the on-demand function to cable TV. On the one hand, this requirement forces HTML to gradually add client behaviors such as forms, scripts, and Dom, and on the other hand, there is a CGI specification on the server side to respond to dynamic requests from the client, the HTTP protocol, which acts as the transmission carrier, also adds the file upload and cookie features. Among them, cookies are used to solve the stateless defects of HTTP. As for the later session mechanism, it is another solution that maintains the status between the client and the server.

Let's use several examples to describe the difference and connection between the cookie and session mechanism. I often went to a coffee shop and offered a free discount for five coffee cups. However, there is little chance of consuming five coffee cups at a time, in this case, you need to record the consumption quantity of a customer in some way. Imagine the following solutions:
1. The shop clerk is very good. He can remember the consumption quantity of each customer. As long as the customer enters the coffee shop, the clerk will know how to deal with it. This method is supported by the Protocol itself.
2. Send a card to the customer, which records the consumption quantity and generally has a validity period. For each consumption, if the customer shows this card, the current consumption will be associated with the previous or later consumption. This approach is to maintain the status on the client.
3. Send a membership card to the customer. No information except the card number is recorded. If the customer shows the card at each purchase, then the clerk finds the log corresponding to this card number in the store's record to add some consumption information. This approach is to maintain the status on the server side.

Because the HTTP protocol is stateless and does not need to be stateful for various reasons, the next two solutions have become a realistic choice. Specifically, the cookie mechanism adopts the client-side persistence scheme, while the session mechanism adopts the server-side persistence scheme. At the same time, we can also see that because the server-side persistence scheme also needs to save an identifier on the client, the session mechanism may need to use the cookie Mechanism to save the identifier, but in fact it has other options.

3. Understanding cookie Mechanism
The basic principle of the cookie mechanism is as simple as the above example, but there are several problems to solve: how to distribute "membership cards", the content of "membership cards", and how customers use "membership cards ".

The orthodox cookie distribution is implemented by extending the HTTP protocol. The server prompts the browser to generate the corresponding cookie by adding a special line in the HTTP response header. However, pure client scripts such as JavaScript or VBScript can also generate cookies.

Cookies are automatically sent to the server in the background by the browser according to certain principles. The browser checks all stored cookies. If the declared range of a cookie is greater than or equal to the location where the requested resource is located, the cookie is attached to the HTTP request header of the requested resource and sent to the server. This means that the McDonald's membership card can only be presented in the McDonald's store. If a branch still has its own membership card, in addition to the McDonald's membership card, the store's membership card is also presented.

Cookie content mainly includes: name, value, expiration time, path and domain.
The domain can specify a domain such as .google.com, which is equivalent to a main store sign. For example, Procter & Gamble can also specify a specific machine in a domain such as www.google.com or froogle.google.com, you can use rejoice for comparison.
The path is the URL path following the domain name, for example, // or/foo. You can use a certain rejoice counter to compare it.
The combination of paths and domains constitutes the scope of cookie.
If no expiration time is set, it indicates that the life cycle of the cookie is the browser session period. When the browser window is closed, the cookie disappears. This cookie is called a session cookie. Session cookies are generally stored in the memory instead of on the hard disk. Of course, this behavior is not standardized. If the expiration time is set, the browser will save the cookie to the hard disk, and then open the browser again. These cookies are still valid until the preset expiration time is exceeded.

Cookies stored on hard disks can be shared among different browser processes, such as two IE Windows. For Cookies stored in the memory, different browsers have different processing methods. For IE, pressing Ctrl-N (or from the File menu) in an open window can share the window with the original window, other new IE processes cannot share the memory cookies of opened windows. for Mozilla Firefox0.8, all processes and tabs can share the same cookies. Generally, a window opened with window. open in javascript will share the memory cookie with the original window. The browser often causes a lot of trouble for web application developers who use the session mechanism to process session cookies.

The following is an example of how goolge sets the cookie response header.
HTTP/1.1 302 Found
Location: http://www.google.com/intl/zh-CN/
Set-Cookie: PREF = ID = 0565f77e132de138: NW = 1: TM = 1098082649: LM = 1098082649:
S = KaeaCFPo49RiA_d8; expires = Sun, 17-Jan-2038 19:14:07 GMT; path =/; domain = .google.com
Content-Type: text/html

This is part of the HTTP Communication record captured by the HTTP Sniffer software HTTPLook.
The browser automatically sends a cookie when accessing goolge resources again.
Using Firefox, you can easily observe the existing cookie values.
Using HTTPLook with Firefox can easily understand how cookies work.
IE can also be set to ask before accepting cookies
This is a dialog box asking to accept cookies.

4. Understanding the session mechanism
The session mechanism is a server-side mechanism. The server uses a structure similar to a hash (or a hash) to save information.

When the program needs to create a session for a client request, the server first checks whether the client request contains a session id called the session id, if a session id is included, it indicates that a session has been created for this client before, and the server uses the session id to retrieve the session. (if the session id is not found, a new one may be created ), if the client request does not contain the session id, the client creates a session and generates a session id associated with the session. The session id value should be unique, the session id is returned to the client for saving in this response.

The cookie can be used to save the session id. During the interaction process, the browser can automatically display the id to the server according to the rules. Generally, the cookie name is similar to SEEESIONID. For example, for the web application cookie generated by weblogic, JSESSIONID = ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng! -145788764, whose name is JSESSIONID.

Because the cookie can be artificially disabled, other mechanisms must be available so that the session ID can still be passed back to the server when the cookie is disabled. A frequently used technology called URL rewriting is to directly append the session ID to the end of the URL path. There are two additional methods, one is the URL path appended information, in the form of http ://..... /xxx; JSESSIONID = byok3vjfd75apnrf7c2hmdnv6qzcebzwowibyenlerjq99zwpbng! -145788764
The other is appended to the URL as a query string, in the form of http: //.../XXX? JSESSIONID = byok3vjfd75apnrf7c2hmdnv6qzcebzwowibyenlerjq99zwpbng! -145788764
There is no difference between the two methods for users, but they are handled differently by servers during parsing, the first method also helps to distinguish the session ID information from the normal program parameters.
To maintain the status throughout the interaction process, the session ID must be included after the path that each client may request.

Another technique is form hidden fields. The server automatically modifies the form and adds a hidden field so that the session ID can be passed back to the server when the form is submitted. For example, the following form
<Form name = "testform" Action = "/XXX">
<Input type = "text">
</Form>
It will be rewritten
<Form name = "testform" Action = "/XXX">
<Input type = "hidden" name = "JSESSIONID"
Value = "byok3vjfd75apnrf7c2hmdnv6qzcebzwowibyenlerjq99zwpbng! -145788764 ">
<Input type = "text">
</Form>
This technology is rarely used now. I have used iplanet6, the predecessor of the SunONE application server.
In fact, this technology can be simply replaced by rewriting the URL of the action application.

When talking about the session mechanism, we often hear the misunderstanding that "the session disappears as long as the browser is closed ". In fact, you can imagine the example of a membership card. Unless the customer initiates a sales card to the store, the store will never easily Delete the customer's information. The same applies to sessions. Unless the program notifies the server to delete a session, the server will keep it. Generally, the program sends a command to delete the session when the user logs off. However, the browser will never notify the server that it is about to close before it closes, so the server will not have the opportunity to know that the browser has been closed, most session mechanisms use session cookies to save session IDs. When the browser is closed, the session ID disappears, and the original session cannot be found when the server is connected again. If the cookie set by the server is saved to the hard disk, or the HTTP request header sent by the browser is rewritten by some means, the original session ID is sent to the server, then you can still find the original session when you open the browser again.

It is precisely because closing the browser will not cause the session to be deleted, forcing the server to set an expiration time for the seesion. When the last time the session was used by the client exceeds this expiration time, the server considers that the client has stopped the activity before deleting the session to save storage space.

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.