how to pass object to JSP
Once I've created a Connection Pool how can I pass that pool object to another JSP?
I am able to create a ConnectionPool successfully like so
//Suppose this is: "welcome.jsp"
<%
DBConnectionPool myPool = new DBConnectionPool("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:OracleConnect;UID=scott;PWD=tiger",5,10,true);
%>
My concern is each time this jsp is called it creates another connection pool rather than making use of an existing one.
So instead of creating another "myPool" object I would like to either pass it to a JSP or test for the existence of it and then use it. for example:
//Suppose this is: "dbLookup.jsp"
<%
Connection myConn = myPool.getConnection();
preparedStatement stmt = myConn.prepareStatement("Select * from tab");
ResultSet rs = stmt.executeQuery();
%>
But unless I instantiate it, myPool will not compile. So how can I get a handle on it if it were created in welcome.jsp?
[1023 byte] By [
chiranjp] at [2007-9-26 2:30:56]

You might try:
<%
static DBConnectionPool myPool;
static {
if (myPool == null) {
myPool = new DBConnectionPool("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:OracleConnect;UID=scott;PWD=tiger",5,10,true);
}
}
%>
That should insure that only one instance of the connection pool is created.
This is what I find interesting in a test I just conducted:
1)Running JRUN3.1 with PWS 4.0 (crappy test environment but it works for now).
2)This is the ode from my start page. It contains a link to poolTest.jsp
<%@ page
import="db.util.pool.*,db.util.query.*,java.sql.*,java.io.*"
autoFlush="true"
language="java"
isThreadSafe="true"
%>
<%
DBConnectionPool myPool = new DBConnectionPool("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:OracleConnect;UID=scott;PWD=tiger",5,10,true);
session.setAttribute("myPool", myPool);
%>
<html>
<head><title>JSP Page</title></head>
<body>
<a href="/train/pooltest.jsp">Start Pool Here</a>
<p>
</body>
</html>
3)In pooltest.jsp. I test if the connection is open or not.
<%@ page
import="db.util.pool.*,db.util.query.*,java.sql.*,java.io.*"autoFlush="true"
language="java"
isThreadSafe="true"
%>
<%
//try to get a connection pool object from the current session
DBConnectionPool myPool = null;
myPool = (DBConnectionPool) session.getAttribute("myPool");
boolean bPool;
//now check to see the myPool object actually exists
if(myPool==null){
myPool = new DBConnectionPool("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:OracleConnect;UID=scott;PWD=tiger",5,10,true);
session.setAttribute("myPool", myPool);
bPool = false;
}
else{
bPool = true;
}
%>
Test:
User A: opens startPool.jsp to create a connection pool
User B: opens poolTest.jsp to see if a connetion pool is available.
Result:
User B always gets false when testing for existence of a connection pool.
Ramification:
each user has to create a connection pool which defeats the purpose.
Question:
Am I doing this wrong by trying to pass the ConnectionPool object in the session? It seems that the session is unique to each user. So whatever i set in session.setAttribute() will only work for current user not all users.
So how do I make a connection object available for ALL users?
I'm 90% there! just need that little bit of help.
use the scope=application.
<jsp:useBean id="objConnPool" scope="application" class="DBConnectionPool"/>
this will ensure that ONLY one instance of the bean is spawned. it will exist for all users throughout the webserver's up time. if the server is restarted, the bean will be unloaded, and reloaded when the webserver is restarted.
i hope this helps some. i went through this SAME process.
it doesn't have to be an EJB to be used with the <usebean> tag. the work i'm doing is relatively simple, but i've been able have several different classes loaded into either session or application variables, and accessable throught my entire website.
once you declare an object as an application or session object, you can reference that same id (with all of it's methods), throughout your entire site. and you only have to do the <useBean> tag on one page. i'm setting up most of these variables on a mandatory login page, and then using session and application objects throughout the rest of my site.
of course, maybe i'm totally missing what you are saying. =) let me know if there's anything else i can toss in.