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> |