Session usage in Jsp

Source: Internet
Author: User

The Jsp session is a life cycle of bean. Generally, it is page, which means that the session is valid until the user leaves the website. If the user cannot determine when to leave, it is generally set based on the system, tomcat is set to 30 minutes.
We use the seesion function to allow multiple jsp programs to operate on the same java bean, so this java bean can be used as our "global variable pool" in the traditional sense ". (IN java, we can use static to statically convert a variable and method, and use singleton to uniquely convert the object .)
In project practice, many parameters in our Jsp program need to be read from the database. Some parameters can be read once actually. If it is designed that each user needs to read the database every time a page is generated, obviously, the database load is huge and time-consuming. Although database connection pool optimization may occur, it is our programming principle to use the database as little as possible.
For example, both test. jsp and test1.jsp need to get a parameter userdir, Which is learned from the database. 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" %>



Untitled



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


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



Untitled



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



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.

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.