Jsp session usage

Source: Internet
Author: User

Session is a life cycle of bean. Generally, it is page. session indicates that the user has been valid until the user leaves the website. If the user cannot determine when to leave, it is generally set based on the system, set tomcat to 30 minutes

<% @ Page import = "java. util. *" session = "true" %>
<HTML>
<HEAD>
<TITLE> Using Sessions to Track Users </TITLE>
</HEAD>

<BODY>
<%
Integer counter = (Integer) session. getAttribute ("counter ");
If (counter = null ){
Counter = new Integer (1 );
} Else {
Counter = new Integer (counter. intValue () + 1 );
}

Session. setAttribute ("counter", counter );
%>
<H1> Using Sessions to Track Users </H1>
Session ID: <% = session. getId () %>
<BR>
Session creation time: <% = new Date (session. getCreationTime () %>
<BR>
Last accessed time: <% = new Date (session. getLastAccessedTime () %>
<BR>
Number of times you 've been here: <% = counter %>
</BODY>
</HTML>

Obtains or sets the session value.

<HTML>
<HEAD>
<TITLE> Using the Application Object </TITLE>
</HEAD>

<BODY>
<H1> Using the Application Object </H1>
<%
Integer counter = (Integer) session. getAttribute ("counter ");
String heading = null;
If (counter = null ){
Counter = new Integer (1 );
} Else {
Counter = new Integer (counter. intValue () + 1 );
}

Session. setAttribute ("counter", counter );

Integer I = (Integer) application. getAttribute ("I ");
If (I = null ){
I = new Integer (1 );
} Else {
I = new Integer (I. intValue () + 1 );
}

Application. setAttribute ("I", I );
%>

You have visited this page <% = counter %> times.
<BR>
This page has been visited by all users <% = I %> times.
</BODY>
</HTML>

We need to obtain a parameter userdir for both the test. jsp tutorial and test1.jsp. This userdir is learned from the database tutorial. Using session will greatly optimize the performance. The program is as follows:

Design a javabean storage userdir.

Public class UserEnv {

Private String userdir = "";
Private String userurl = "";

Public UserEnv (){
// The construction method initializes userdir, which can be read from the database. Here, the value is ppp.
Userdir = "pppp ";
System. out. println ("init userdir, one time ");
}

Public String getUserdir () throws Exception {
Return userdir;

}

}

Test1.jsp program:

<% @ Page contentType = "text/html; charset = ISO8859_1" %>

<Jsp: useBean id = "myenv" scope = "session" class = "mysite. UserEnv"/>
<Html>
<Head>
<Title> Untitled </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>

This is test1.jsp: <% = myenv. getUserdir () %>
</Body>
</Html>


Test2.jsp program:
<% @ Page contentType = "text/html; charset = ISO8859_1" %>

<Jsp: useBean id = "myenv" scope = "session" class = "mysite. UserEnv"/>
<Html>
<Head>
<Title> Untitled </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>

This is test2.jsp: <% = myenv. getUserdir () %>
</Body>
</Html>

 

No matter whether you call test1.jsp or test2.jsp first, java bean UserEnv is always initialized first. Because the bean has a seesion cycle, this user only needs to call it within the seesion validity period after the second time, myenv. getUserdir () will read the variable directly from the bean memory without initialization. this increases the speed and reduces the database access volume.

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.