Oracle JavaServer Pages Developer's Guide and Reference Release 8.1.7 Part Number A83726-01 |
|
The javax.servlet.http
package offers a standard mechanism for managing session resources. Additionally, Oracle provides extensions for managing application, session, page, and request resources.
A JSP page must appropriately manage resources acquired during its execution, such as JDBC connection, statement, and result set objects. The standard javax.servlet.http
package provides the HttpSessionBindingListener
interface and HttpSessionBindingEvent
class to manage session-scoped resources. Through this mechanism, a session-scoped query bean could, for example, acquire a database cursor when the bean is instantiated and close it when the HTTP session is terminated. (The example in "JSP Starter Sample for Database Access" opens and closes the connection for each query, which adds overhead.)
This section describes use of the HttpSessionBindingListener
valueBound()
and valueUnbound()
methods.
An object that implements the HttpSessionBindingListener
interface can implement a valueBound()
method and a valueUnbound()
method, each of which takes an HttpSessionBindingEvent
instance as input. These methods are called by the servlet container--the valueBound()
method when the object is stored in the session; the valueUnbound()
method when the object is removed from the session or when the session times-out or becomes invalid. Usually, a developer will use valueUnbound()
to release resources held by the object (in the example below, to release the database connection).
Note: OracleJSP provides extensions for additional resource management, allowing you to program JavaBeans to manage page-scoped, request-scoped, or application-scoped resources as well as session-scoped resources. See "OracleJSP Event Handling--JspScopeListener". |
The next section, "JDBCQueryBean JavaBean Code", provides a sample JavaBean that implements HttpSessionBindingListener
and a sample JSP page that calls the bean.
Following is the sample code for JDBCQueryBean
, a JavaBean that implements the HttpSessionBindingListener
interface. (It uses the JDBC OCI driver for its database connection; use an appropriate JDBC driver and connection string if you want to run this example yourself.)
JDBCQueryBean
gets a search condition through the HTML request (as described in "The UseJDBCQueryBean JSP Page"), executes a dynamic query based on the search condition, and outputs the result.
This class also implements a valueUnbound()
method (as specified in the HttpSessionBindingListener
interface) that results in the database connection being closed at the end of the session.
package mybeans; import java.sql.*; import javax.servlet.http.*; public class JDBCQueryBean implements HttpSessionBindingListener { String searchCond = ""; String result = null; public void JDBCQueryBean() { } public synchronized String getResult() { if (result != null) return result; else return runQuery(); } public synchronized void setSearchCond(String cond) { result = null; this.searchCond = cond; } private Connection conn = null; private String runQuery() { StringBuffer sb = new StringBuffer(); Statement stmt = null; ResultSet rset = null; try { if (conn == null) { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); conn = DriverManager.getConnection("jdbc:oracle:oci8:@", "scott", "tiger"); } stmt = conn.createStatement(); rset = stmt.executeQuery ("SELECT ename, sal FROM scott.emp "+ (searchCond.equals("") ? "" : "WHERE " + searchCond )); result = formatResult(rset); return result; } catch (SQLException e) { return ("<P> SQL error: <PRE> " + e + " </PRE> </P>\n"); } finally { try { if (rset != null) rset.close(); if (stmt != null) stmt.close(); } catch (SQLException ignored) {} } } private String formatResult(ResultSet rset) throws SQLException { StringBuffer sb = new StringBuffer(); if (!rset.next()) sb.append("<P> No matching rows.<P>\n"); else { sb.append("<UL><B>"); do { sb.append("<LI>" + rset.getString(1) + " earns $ " + rset.getInt(2) + "</LI>\n"); } while (rset.next()); sb.append("</B></UL>"); } return sb.toString(); } public void valueBound(HttpSessionBindingEvent event) { // do nothing -- the session-scoped bean is already bound } public synchronized void valueUnbound(HttpSessionBindingEvent event) { try { if (conn != null) conn.close(); } catch (SQLException ignored) {} } }
The following JSP page uses the JDBCQueryBean
JavaBean defined in the preceding section ("JDBCQueryBean JavaBean Code"), invoking the bean with session
scope. It uses JDBCQueryBean
to display employee names that match a search condition entered by the user.
JDBCQueryBean
gets the search condition through the jsp:setProperty
command in this JSP page, which sets the searchCond
property of the bean according to the value of the searchCond
request parameter input by the user through the HTML form. (The HTML INPUT
tag is what specifies that the search condition entered in the form be named searchCond
.)
<jsp:useBean id="queryBean" class="mybeans.JDBCQueryBean" scope="session" /> <jsp:setProperty name="queryBean" property="searchCond" /> <HTML> <HEAD> <TITLE> The UseJDBCQueryBean JSP </TITLE> </HEAD> <BODY BGCOLOR="white"> <% String searchCondition = request.getParameter("searchCond"); if (searchCondition != null) { %> <H3> Search results for : <I> <%= searchCondition %> </I> </H3> <%= queryBean.getResult() %> <HR><BR> <% } %> <B>Enter a search condition for the EMP table:</B> <FORM METHOD="get"> <INPUT TYPE="text" NAME="searchCond" VALUE="ename LIKE 'A%' " SIZE="40"> <INPUT TYPE="submit" VALUE="Ask Oracle"> </FORM> </BODY> </HTML>
Following is sample input and output for this page:
In the preceding example, an alternative to the HttpSessionBindingListener
mechanism would be to close the connection in a finalize
method in the JavaBean. The finalize
method would be called when the bean is garbage-collected after the session is closed. The HttpSessionBindingListener
interface, however, has more predictable behavior than a finalize
method. Garbage collection frequency depends on the memory consumption pattern of the application. By contrast, the valueUnbound()
method of the HttpSessionBindingListener
interface is called reliably at session shutdown.
Oracle provides the following extensions for managing application and session resources as well as page and request resources:
JspScopeListener
--for managing application, session, page, or request resources
For information, see "OracleJSP Event Handling--JspScopeListener".
globals.jsa
application and session events--for start and end events for applications and sessions, typically in a servlet 2.0 environment such as Apache/JServ
See "The globals.jsa Event Handlers" for information.
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|