Use of session in JSP getting started tutorial

Source: Internet
Author: User
The TTP protocol is stateless, that is, information cannot be transmitted through the HTTP protocol itself. ASP applies the session object to track the user's operation status. JSP uses an object called httpsession to implement the same function. Httpsession is a high-quality interface built on cookies and URL-rewriting. The session information is stored on the server, and the session ID is stored in the cookie of the client. In fact, many servers use cookies if supported by browsers, but if not, they are automatically converted to URL-rewriting, session automatically provides a convenient way to store information for each process.

Generally, a 30-minute expiration time is set for a session on the server. The session will automatically expire when the customer stops the activity. The stored and retrieved information in the session cannot be basic data types such as int and double, but must be Java objects such as integer and double.

  Httpsession has the following APIs:

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

  GetcreationtimeReturns the time when the session was created. The minimum unit is 1‰ seconds. To obtain a value that is useful for print output, you can pass the value to the date constructor or gregoriancalendar method settimeinmillis.

  GetlastaccessedtimeReturns the time when the session was last sent by the client. The minimum unit is 1‰ seconds.

  GetmaxinactiveintervalThe total time (in seconds) returned. A negative value indicates that the session will never time out.

  GetattributeObtains the information associated with a session. (Getvalue in jsp1.0)

  Integer item = (integer) Session. getattrobute ("item ")// Retrieve the session value and convert it to an integer

  SetattributeProvides a keyword and a value. Will replace any previous values. (Putvalue in jsp1.0)

  Session. setattribute ("itemvalue", itemname );// Itemvalue must not be of the must simple type

Getattribute and setattribute are the most commonly used in applications. Here is a simple example to describe the session application, test1.jsp (information written to session) and test2.jsp (reading information 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 ("the value retrieved from the session is:" + 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.