Sharing a connection among a bunch of JSP tags?

Hi, I am new to java, so please excuse me if this is a silly question, but here goes:

I am created a tag library with a bunch of tags in it. Basically all of these tags will perform various database queries through JDBC, so every class (which implements a tag) will have one or more methods which uses a Connection object to the database.

It would be very silly to have every method in every tag class open its own connection, do its queries, and then close the connection, because performance would be aweful.

What I would like is that the connection is established just once before any tags are displayed, and taken down just once after all the tags are finished. Also, within the tag class methods, I would not like to have a Connection argument for each one of them; they are all using the same thing, so it should be treated as a global variable basically.

Is there a way to do this?

Thanks

[946 byte] By [JavaGrasshopper] at [2007-9-26 6:36:27]
# 1

Let's say I have an object with a connection defined in it:

Connection db;

Then I can use:

pageContext.setAttribute("db", db);

to set it, and I can retrieve it later by using:

Connection db = (Connection)pageContext.getAttribute("db");

and everything works!

Learning the basics of java is quite hard, but once you get them, it gets much easier, it seems.

JavaGrasshopper at 2007-7-1 15:51:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Pretty simple. you just have to make sure that your Connection object will exist in the context where you look for it under the exact name.

When you write a tag handler, you extend either TagSupport or BodyTagSupport (you are of course only required to implement either the Tag or BodyTag interfaces - but believe me its easier just to extend the TagSupport classes). You have then access to the implicit variable pageContext. This variable's type is javax.servlet.jsp.PageContext. You can consult the APIDOC at:

http://java.sun.com/j2ee/j2sdkee/techdocs/api/index.html

Important for you are the two versions of setAttribute:

public Object getAttribute(String name, int scope)

public Object getAttribute(String name)

The first one looks for an object in a specified scope

(one of:

PageContext.REQUEST_SCOPE,

PageContext.PAGE_SCOPE,

PageContext.SESSION_SCOPE,

PageContext.APPLICATION_SCOPE;

),

and the second one looks in the page scope.

So if you define a variable of type Connection somewhere in your jsp:

...

<%

try {

Conection conn = myDataSource.getConnection();

%>

...

<myTL:myTag1/>

...

<myTL:myTag2>

...

<myTL:myTag1/>

...

</myTL:myTag2>

...

<%

}

catch (SQLException sex) {

//handle the exception

}

finally {

try {conn.close();} catch(Exception ex) {;}

}

%>

you can access the conn variable from the pageContext variable in the myTag1 and myTag2 tag handlers:

public class MyTag1 extends BodyTagSupport {

...

private Conection conn = pageContext.getAttribute("conn");

...

public int doStartTag() throws JspException {

...

// do with connection what you want...

...

}

...

}

You can of course develop a tag for the getConnection() and conn.close() sections, so everything on your page would be tags. But you have to read further for custom tags, because when you want to define page scope variables in a custom tag you have to write a TEI class.

Hope this'll help

Anton

maleev at 2007-7-1 15:51:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...