Learning Session

Study after Cookie, solve the problem of sharing data without sending the request. Cookie is a browser-side data storage technology, this lesson focuses on an important additional data storage technology, session technology.

Session Learning

problem:

Request object to solve the problems of different data sharing within the first request Servlet,

So how do deal with different requests a user needs to use the same data it ?

solve:

Use session technology.

 

principle:

The first user uses the browser sends a request to the server, the server after receiving the request, invoke corresponding Servlet processing. Will process the user creates a session object, used to store common data processing related to the user request, and session JSESSIONID this object in the Cookie stored (temporarily stored, i.e., failure of the browser is closed) in the browser. When a user initiates a second request and the subsequent request, the request message will be included with the JSESSIONID, server upon receiving the request, the corresponding Servlet invocation request process, and returns the corresponding session object according JSESSIONID.

 

Features:

1, Session Cookie art technique relies server data storage technology.

2, were created by the server

3, each user has a separate session

4, the default storage time is 30 minutes effect:

5, solve the problem of different data sharing request of a user.

 

use:

1. Create a Session object

2, store the data session object acquisition session object

3, retrieve data from the session object

4, if the acquired data is not present in the session returns null.

 

note:

They do not close the browser, and in the case of the session does not fail, any user with a request acquired at any Servlet project are the same session object.

 

Scope: a conversation

 

Servlet Session

HTTP is a "stateless" protocol, which means retrieving the page each time the client, the client opens a separate connection to the Web server, the server will automatically retain no record prior to client requests.

 

Three ways to maintain session conversation between the Web client and Web server

1、Cookies

2, hidden form fields

3, URL rewrite

 

 

HttpSession object

In addition to the above-mentioned three methods, the Servlet also provides HttpSession interface that provides the user identifying the user and storing information about a plurality of page request when a cross mode or visit.

 

Servlet container to use this interface to create a session conversation between an HTTP client and HTTP server. Session lasts a specified period of time, across multiple connected or page request.

 

By calling () to get the public HttpSession object HttpServletRequest method getSession, as follows:

HttpSession session = request.getSession();

You need to send the client any document content before calling request.getSession () .

Here is a summary of several important methods HttpSession object available in:

 

Method and Description

1、public Object getAttribute(String name)

This method returns the object with the specified name of the session in the session, if the object name is not specified, null is returned.

 

2、public Enumeration getAttributeNames()

This method returns an enumeration of String objects, String object containing the names of all bound to the session session object.

 

3、public long getCreationTime()

The method returns the time session session is created, self GMT January 1970 date of midnight, in milliseconds.

 

4、public String getId()

The method returns to the assigned comprising a unique string identifier of the session of the session.

 

5、public long getLastAccessedTime()

This method returns the client sends the last session session-related requests time from GMT January 1970 date of midnight, in milliseconds.

 

6、public int getMaxInactiveInterval()

This method returns the Servlet container holding the maximum time interval sessions open session when the client access, in seconds.

 

7、public void invalidate()

This indicates that the method is invalid session session, and releasing bound to any object above it.

 

8、public boolean isNew()

If the client does not know the session session, or if the customer chooses not to participate the session into a session, then the method returns true.

 

9、public void removeAttribute(String name)

The method from session to remove the name of the specified object session.

 

10、public void setAttribute(String name, Object value)

This method uses the specified name to bind an object to the session session.

 

11、public void setMaxInactiveInterval(int interval)

In the process before indicating that the session is invalid session Servlet container, the time between the specified client request, in seconds.

 

 

Delete Session session data

1, to remove a specific attribute: You can call the public void removeAttribute (String name) method to remove a particular value associated with a key.

2, delete the entire session session: You can call the public void invalidate () method to discard the whole session to session.

3, set the session session expiration time: You can call the public void setMaxInactiveInterval (int interval) method to set up a separate session session timeout.

 

web.xml Configuration

Using a Tomcat, in addition to the above-described methods, you can configure the session in the session timeout web.xml file, as follows:

 <session-config>

    <session-timeout>15</session-timeout>

  </session-config>

 

 

Examples of the above timeout time is minutes, covering the Tomcat default timeout for 30 minutes.

 

In one getMaxInactiveInterval Servlet in () method returns a session timeout session, in seconds. Therefore, if the session configuration web.xml session timeout time of 15 minutes, then getMaxInactiveInterval () returns 900.

 

Guess you like

Origin www.cnblogs.com/qq308015824/p/11025712.html