Pass Connection Object between JSPs

S

Sisilla

Hello All,

Is there any way to pass a Connection Object from one JSP to another?
I appreciate any effort to help me. Thank you for your time and
consideration.

Thanks,

Sisilla
 
S

Sisilla

Here's how if anyone's wondering...

Assuming conn is a valid Connection Object, in the first jsp...:
<% session.setAttribute("conn", conn); %>

.... and in the second:
<% Connection conn = (Connection) session.getAttribute("conn"); %>
 
W

Wojtek

Sisilla wrote :
Here's how if anyone's wondering...

Assuming conn is a valid Connection Object, in the first jsp...:
<% session.setAttribute("conn", conn); %>

... and in the second:
<% Connection conn = (Connection) session.getAttribute("conn"); %>

Except that the lifetime of the session is how long the user is active.
So the Connection Object will not go away as long as the session has a
reference to it. You may run out of connections to the database if your
site gets at all busy.

You really want to use the same basic syntax except use the request
object. That way the reference goes away when the JSP is finished
processing. And make sure that you close the connection somewhere along
the line.
 
S

Sisilla

Thanks, Wojtek. I appreciate the tip. I'll make the changes
accordingly. ~Sisilla
 
D

derek

Thanks, Wojtek. I appreciate the tip. I'll make the changes
accordingly. ~Sisilla
requestobject. That way the reference goes away when the JSP is finished

Storing a connection is a really REALLY bad idea.
Even if it is just the request object, its still a bad idea.
You should use connection pooling. It will take manage
your connections for you. It will try to reuse connections
so you can improve performance.

The code i use goes something like this:

public Connection getConnection(String name) throws Exception {
Context initCtx = new InitialContext();
DataSource ds;
if ( useEnv ) { // tomcat needs this, but websphere doesnt, i am no jndi expert so i am not sure why.
initCtx = (Context)initCtx.lookup("java:comp/env");
}
// this is the connection pool name you have defined, something usually like jdbc/poolName
ds = (DataSource)initCtx.lookup(name);
return ds.getConnection();
}

good luck.


..
=====================================================
THIS IS MY SIGNATURE. There are many like it, but this one is mine.
 
D

derek

Thanks, Wojtek. I appreciate the tip. I'll make the changes
accordingly. ~Sisilla
requestobject. That way the reference goes away when the JSP is finished

Storing a connection is a really REALLY bad idea.
Even if it is just the request object, its still a bad idea.
You should use connection pooling. It will take manage
your connections for you. It will try to reuse connections
so you can improve performance.

The code i use goes something like this:

public Connection getConnection(String name) throws Exception {
Context initCtx = new InitialContext();
DataSource ds;
if ( useEnv ) { // tomcat needs this, but websphere doesnt, i am no jndi expert so i am not sure why.
initCtx = (Context)initCtx.lookup("java:comp/env");
}
// this is the connection pool name you have defined, something usually like jdbc/poolName
ds = (DataSource)initCtx.lookup(name);
return ds.getConnection();
}

good luck.


..
=====================================================
THIS IS MY SIGNATURE. There are many like it, but this one is mine.
 
D

derek

Thanks, Wojtek. I appreciate the tip. I'll make the changes
accordingly. ~Sisilla
requestobject. That way the reference goes away when the JSP is finished

Storing a connection is a really REALLY bad idea.
Even if it is just the request object, its still a bad idea.
You should use connection pooling. It will take manage
your connections for you. It will try to reuse connections
so you can improve performance.

The code i use goes something like this:

public Connection getConnection(String name) throws Exception {
Context initCtx = new InitialContext();
DataSource ds;
if ( useEnv ) { // tomcat needs this, but websphere doesnt, i am no jndi expert so i am not sure why.
initCtx = (Context)initCtx.lookup("java:comp/env");
}
// this is the connection pool name you have defined, something usually like jdbc/poolName
ds = (DataSource)initCtx.lookup(name);
return ds.getConnection();
}

good luck.


..
=====================================================
THIS IS MY SIGNATURE. There are many like it, but this one is mine.
 
W

Wojtek

derek wrote :
Storing a connection is a really REALLY bad idea.
Even if it is just the request object, its still a bad idea.
You should use connection pooling. It will take manage
your connections for you. It will try to reuse connections
so you can improve performance.

I use proxool.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,330
Latest member
AlvaStingl

Latest Threads

Top