Example Analysis of session Usage in jsp Programming

  • 2021-08-21 21:07:20
  • OfStack

This paper illustrates the usage of session in jsp programming. Share it for your reference, as follows:

TTP protocol is stateless, that is, information cannot be transmitted through HTTP protocol itself. In order to track the user's operation status, ASP applies SESSION objects. JSP uses an object called HttpSession to do the same. HTTPSession is a high quality interface built on cookies and URL-rewriting. The information for the Session is stored on the server side, and the id for the Session is stored in the cookie for the client. In fact, on many servers, cookies is used if the browser supports it, but if it is not supported or abolished, it is automatically converted to URL-rewriting, and session automatically provides a convenient way to store information for each process.

Session1 sets a 30-minute expiration time on the server, which automatically expires when the customer stops active. The information stored and retrieved in Session cannot be the basic data type such as int, double, etc., but must be the corresponding object of java, such as Integer, Double.

Httpsession has the following API:

getId This method returns only 1 identifiers, which are generated for each session. It is used as a key name when only one single 1 value is associated with one session, or when the log information is related to the previous sessions.

GetCreationTime returns the time when session was created. The minimum unit is one thousandth of a second. To get a value that is useful for printout, pass this value to Date constructor or GregorianCalendar's method setTimeInMillis.

GetLastAccessedTime returns the time when session was last sent by the customer. The minimum unit is one thousandth of a second.

GetMaxInactiveInterval returns the total time in seconds, and a negative value indicates that session will never time out.

getAttribute takes the information associated with one session. (getValue in jsp 1.0)

Integer item = (Integer) session. getAttrobute ("item")//Retrieves the value of session and converts it to an integer

setAttribute provides 1 keyword and 1 value. Any previous values are replaced. (putValue in jsp 1.0)

session. setAttribute ("ItemValue", itemName); //ItemValue must not be of must simple type

getAttribute and setAttribute are widely used in applications. A simple example is given to illustrate the application of session, test1.jsp (information is written into session), test2.jsp (information is 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( "From session The value taken out in is: " +ls_str);
%>
</BODY>
</HTML>

Judge whether the user performs refresh operation, avoid related operations when refreshing, and distinguish whether to enter this page for the first time, session. isNew ().

Examples of online headcount statistics


<%@ page contentType="text/html; charset=GB2312" %>
<HTML>
<HEAD>
<TITLE>application Counter </TITLE>
</HEAD>
<BODY>
<BR>
<%
  Integer number=(Integer)application.getAttribute("Count");
  // Check Count Property is available 
 if(number==null)
  {
    number=new Integer(1);
    application.setAttribute("Count",number); 
  }
  if(session.isNew()==true) // Judge whether the user executes the refresh operation 
  {
 // Increments the obtained value 1
 number=new Integer(number.intValue()+1);
    application.setAttribute("Count",number); 
  }
%>
<P><P> You are the first 
  <%int a=((Integer)pageContext.getAttribute("Count",PageContext.APPLICATION_SCOPE)).intValue();
  %>
  <%=a%>
 Customers who visit this site. 
</BODY>
</HTML>

I hope this article is helpful to everyone's jsp programming.


Related articles: