程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp session的用法

jsp session的用法

編輯:關於JSP

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內存中讀取變量,不必再初始化.這樣提高速度,又減少數據庫訪問量.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved