Using the session of the JSP introductory beginner tutorial

Source: Internet
Author: User
Tags session id

The HTTP protocol is stateless, that is, information cannot be passed through the HTTP protocol itself. In order to track the user's operation status, ASP applies the Session object. JSP uses an object called HttpSession to achieve the same function. HTTPSession is a high-quality interface built on cookies and url-rewriting. The session information is saved on the server side, and the session ID is stored in the client's cookie. In fact, on many servers, they use cookies if the browser supports them, but if they are not supported or abolished, they automatically convert to Url-rewriting,session automatically provides a convenient way to store information for each process.

Session typically sets a 30-minute expiration time on the server, which automatically expires when the customer stops the activity. The information saved and retrieved in the session cannot be a basic data type such as int, double, and must be a corresponding object in Java, such as Integer, double.

HttpSession has the following APIs:

GetId This method returns a unique identity, which is generated for each session. When only a single value is associated with a session, or when the log information is related to a previous sessions, it is used as a key name.

GetCreationTime returns the time the session was created. The minimum unit is 1 per thousand seconds. To get a value that is useful for printing output, you can pass this value to the date constructor or GregorianCalendar method Settimeinmillis.

Getlastaccessedtime returns the time the session was last sent by the customer. The minimum unit is 1 per thousand seconds.

Getmaxinactiveinterval returns the total time (in seconds), and a negative value indicates that the session never times out.

GetAttribute to take a session of the information associated with. (in jsp1.0 for GetValue)

Integer item = (integer) session.getattrobute ("item")//retrieves the value of the session and converts it to an integral type

SetAttribute provides a keyword and a value. will replace any previous values. (in jsp1.0 for Putvalue)

Session.setattribute ("Itemvalue", itemname); Itemvalue must not be must simple type

The most used in applications are getattribute and setattribute. A simple example is given to illustrate the application of the session, test1.jsp (information written to session), TEST2.JSP (read from session).

test1.jsp

<HTML>
<HEAD>
<TITLE> Document </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
session.setAttribute("str",new String(“this is test”));
</BODY>
</HTML>
test2.jsp
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<%
String ls_str=null;
ls_str=(String)session.getAttribute("str");
out.println(“从session里取出的值为:”+ls_str);
%>
</BODY>
</HTML>

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.