jsp session的用法

來源:互聯網
上載者:User

session是使用bean的一個生存期限,一般為page,session意思是在這個使用者沒有離開網站之前一直有效,如果無法判斷使用者何時離開,一般依據系統設定,tomcat中設定為30分鐘

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

擷取或設定session值

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

我們的test.jsp教程 和test1.jsp都需要得到一個參數userdir,這個userdir是從資料庫教程中得知,使用session將大大最佳化效能,程式如下:

設計一個javabean 儲存userdir.

public class UserEnv {

private String userdir = "";
private String userurl = "";

public UserEnv(){
//構建方法初始化userdir,可以從資料庫中讀取,這裡簡單給值ppp
userdir="pppp";
System.out.println("init userdir, one time");
}

public String getUserdir() throws Exception{
return userdir;

}

}

test1.jsp程式:

<%@ 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程式:
<%@ 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>

 

無論使用者先調用test1.jsp還是test2.jsp, java bean UserEnv總是先初始化一次, 由於這個bean存在周期是seesion,因此該使用者第二次以後只要在seesion有效期間內再調用,myenv.getUserdir()將直接從bean記憶體中讀取變數,不必再初始化.這樣提高速度,又減少資料庫訪問量.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.