• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connection instance from a JSP to a JavaBean

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Connection instance (that I got from a JDBCPool) in my JSP. Can I pass this instance to a JavaBean in the constructor of the bean (from my JSP) and have the bean use this connection (instead of creating its own connection instance)? May the below code cause any errors down the road where there are several connections to the database?
Thx.
Example:
--JSP Code--
DBAccess dba = new DBAccess();
Connection con = null;
try {
con = dba.getConnection();
} catch (SQLException sqle) {
}
OrderedThreads ot = new OrderedThreads(con);
--JavaBean Constructor--
public Connection con = null;
public OrderedThreads(Connection cn) {
con = cn;
try {
stmt = con.createStatement();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you load a bean to be used in a JSP page, you use jsp:useBean to do it. The bean class should be a class that has a zero-argument(empty) constructor and you use such a constructor to load the bean. So you can't use this constructor to load the bean: OrderedThreads(Connection cn) since it has parameter.
But you can do it this way (in your jsp file):
<jsp:useBean id="OrderedThreads" class="testPKG.OrderedThreads" scope="page"/>
<%
OrderedThreads.setConnection(con); //setConnection(Connection con) is a method of OrderedThreads to set connection variable in OrderedThreads as the parameter;
%>
 
Trust God, but always tether your camel... to this tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic