JavaEE session mechanism

Source: Internet
Author: User

JavaEE session mechanism

JavaEE session mechanism

Http protocol:

Before talking about the session, you must talk about the Http protocol. HTTP is a standard for client and server requests and responses (TCP ). An HTTP client initiates a request to establish a TCP connection to the specified port on the server (port 80 by default. The HTTP server listens to the requests sent from the client on that port. Once a request is received, the server (to the client) sends a status line, such as "HTTP/1.1200 OK", and (response) message, the message body may be the requested file, error message, or other information.

That is to say, the Http protocol is stateless, so our session mechanism is to save the identity recognition function of the corresponding browser.

Session technology:
1. What is session)
A State management technology that maintains the user status on the server. Its implementation process is as follows: when a browser sends a message to your server, the server creates an object (called a session object ),
This object has a unique ID (sessionId). By default, the server uses cookie technology to send the sessionId to the browser,
The next time the browser accesses the server, it will carry the sessionId to the server. The server finds the corresponding session object based on the sessionId (the user's status can be written to the session object ).
2. How to Create a session object?
1), HttpSessionsession = request. getSession (); // The server creates a session object that complies with the standard definition of the httsponsion interface.
2), HttpSessionsession = request. getSession (boolean flag );
When flag is true:
When a browser sends a request to the server, the server will check whether the request has a sessionId. If not, a session object is created;
If yes, check whether the session object to be searched still exists (for some reason, such as timeout limit, the server will delete the session object). If yes, return; if not, create a new object.
When flag is false:
When a browser sends a request to the server, the server will check whether there is a sessionId in the request. If not, a null value is returned;
If yes, check whether the session object to be searched still exists (for some reason, such as timeout limit, the server will delete the session object). If yes, return; if not, a null value is returned.
3), HttpSessionsession = request. getSession (); EquivalentHttpSessionsession = request. getSession (true );
3. Common Methods in sessions
Session. setAttribute (Stringname, Object obj );
Objectsession. getAttribute (String name); // this parameter is generally post-transformation.
Session. removeAttribute (Stringname); // remove an attribute
Stringsession. getId ();
4. Set the maximum inactivity time of the sesion.
Session. setMaxInactiveInerval (intseconds );
Session. invalidate (); // invalidate it immediately
5. Use the configuration file to set the session expiration time,
Method 1: Set the configuration expiration time for the entire server. This method affects all applications on the server. Tomcat is 30 minutes by default.
TOMCAT_HOME/conf/web. xml

30

Method 2: configure the expiration time for a specific application
You only need to modify web. xml

30

6. Example:
A. Verify the session of the page to be protected
Session Verification:
1. After successful login, write data and add a session attribute.
<%
Session. setAttribute ("user", user );
%>
2. Add the session verification code to the page to be protected.
<%
Objectobj = session. getAttribute ("user ");
If (null = obj ){
Response. sendRedirect ("login. jsp ");
}
%>
3. For the sake of security (sometimes the browser is not closed and the session still exists), make a secure exit
HttpSessionsession = request. getSession ();
Session. invalidate (); // invalidate
Response. sendRedirect ("login. jsp ");
B. Shopping Cart
Product: Product
Idlong,
ModelString,
PicNameString,
ProDescString,
Pricedouble;
CartItem: item
ProductProduct,
Quantityint;
Cart: Shopping Cart
ItemsList ,
Booleanadd (CartItem item ),
List List (),
Delete (longid ),
Clear (),
Modify (longid, int quantity ),
Doublecost ();
7. If the cookie is disabled, how does one implement the session mechanism? (How to Implement sessionId tracking)
Use the URL rewriting mechanism:
If you want to access the web component (jsp or servelt), you need to support the session mechanism (the session object is used ),
However, the cookie technology is disabled on the browser. Therefore, you cannot enter the customization of the web Components you want to access in the address bar. Instead, you must use the address generated by the server,
Instead, use an address generated by the server (this address will carry the sessionId)
How to generate an address with sessionId?
A. For link and form submission, use response. encodeURL ("url address ").
For example: ">
B. For redirection, use response. encodeRedirectURL ("url address ").
For example: response. sendRedirect (response. encodeRedirectURL ("url address "));
C. For forwarding, you do not need to consider it because the redirection inside the server does not need to interact with the browser.

Session mechanism schematic:

EESession mechanism "title =" JavaEESession mechanism ">

From: http://blog.sina.com.cn/s/blog_6c21f6480100vllc.html

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.