JSP details-session

Source: Internet
Author: User

JSP details-session

Session Object

Session allows you to save the user's status when redirecting between Web pages of an application, so that the entire user session persists until the browser is closed. However, in a session, if the client does not send a request to the server for a long time, the session will automatically disappear. This time depends on the server and can be modified through the program.

1. Create and obtain customer sessions

It is implemented through the setAttribute () and getAttribute () Methods of the session.

Syntax format of setAttribute () method:

Session. setAttribute (String name, Object obj)

Parameter description:

Name: variable name

Obj: the object stored in the session range.

Example:

Save the username "Zhang San" to the username variable in the session range.

Session. setAttribute ("usernme1", "Zhang San ");

Syntax format of getAtrribute ()

GetAttrbite (String name );

Parameter description:

Name: parameter name stored in the range of session Variables

The returned value of the getAttribute () method is of the Object type.

2. Remove the specified bound object from the session

Syntax format of removeAttribute:

RemoveAttribute (String name)

Parameter description:

Name: variable name in the session

Example:

Remove the username stored in the session

<%

Session. removeAttribute ("username ");

%>

3. Destroy the session object

The invalidate () method of the session object can destroy the session. Its Syntax format is:

Session. invalidate ();

After the session object is destroyed, the session object cannot be used; otherwise, an error occurs in Session already invalidate.

4. Manage Session Timeout

You can set the session lifecycle in the session object as follows:

GetLastAccessTime (): returns the last session-related request time of the client.

GetMaxInactiveInterval (): returns the maximum interval between two requests in a session in seconds.

SetMaxInactiveInterval (): sets the session validity period in seconds.

5. Application of the session Object

Example:

In index. the jsp page provides a text box for the user to enter the user name. on the jsp page, save the user name entered by the user in the session. You can add your favorite places on this page, in result. on the jsp page, the user name and favorite place entered by the user are displayed.

The index. jsp page is as follows:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

My JSP 'index. jsp 'starting page

Result. jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

<%

String place = request. getParameter ("place ");

Session. setAttribute ("place", place); // gets the object stored in the session

String name = request. getParameter ("name ");

%>

Result page

Session. jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

<%

String name = request. getParameter ("name"); // obtain the user name

Session. setAttribute ("name", name); // Save the user name in the session

%>

Session jsp




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.