Gerry Scheetz wrote:

> My team is in the process of building a web application.  Currently the
> Controller Servlet calls the appropriate Action Class.  This Action Class
> does what ever is necessary (update data, query, validate, etc.) and stores
> the information that needs to be displayed in JavaBeans.  When completed the
> Action class returns the location of the JSP page to be displayed.  My
> problem is all my database access goes through one singleton class, which
> uses connection pooling.  The run-time performance of this class is fine,
> but with multiple developers everyone want to access this file at the same
> time. :(
>
> My question is how do others transfer information between a database and
> JavaBeans.
>
> Some topics I have run across while surfing and my thoughts.
> ** Enterprise Java Beans - no in-house knowledge, so we would be learning
> from the ground up.  Do we need a server?  Are they tough to code?  How to
> get info from there to JavaBean for use in JSP?


EJB is overkill for most web applications, and comes at a great cost
in terms of learning and additional implementation time. Unless you're
very careful, it's also very easy to end up with an application with
worse performance than if you stick to just servlets and JSP. I would
only consider EJB if you're application has to support multiple types
of clients (browsers, GUI apps, and other servers, for instance) and
even then it's not a given.

> ** Placing function in each JavaBean to retrieve information from the
> database and save info to the database. - Some questions on using the
> connection pooling, but I think I could work through this.  Is it still a
> JavaBean if I do this (add a populate and commit methods).


This, or a variation on it, is probably the best approach for most
web applications. Don't get hung up on the JavaBeans term: a bean is
simply a class that has a no-args constructor and provides access to
its properties through setter and getter methods named according to
the JavaBeans convention. It can do whatever you want, and have
additional methods besides the setter and getter methods. Whether it's
important to use a bean or not depends on how you intend to use the
class.

> ** Others but nothing that I can think of right now.


The variation of the above that I had in mind goes something like
this. Let your Access classes use another set of classes to access
the database. These classes encapsulate all SQL code and can return
the data as beans that you pass on to the JSP pages. A simple example:

   public class EmpListAction implements Action {
     private static final String LIST_VIEW_URL = "...";
     private GetEmpList dao = new GetEmpList(...);

     public String process(...) {
       // Validation etc.
       ...
       EmpListBean empList = dao.getEmpList(...);
       // Deal with null, if needed
       ...
       request.setAttribute(empList);
       return LIST_VIEW_URL;
     }
   }

   public class GetEmpList {
     private DataSource ds;

     public GetEmpList(...) {
       ds = ...;
     }

     public EmpListBean getEmpList(...) {
       Connection conn = null;
       EmpListBean empList = new EmpListBean();
       try {
         conn = ds.getConnection();
         // Execute the SQL query and populate the bean with the result
       }
       catch (SQLException e) {
         // Deal with it
       }
       finally {
         if (conn != null) {
            // Closing the Connection returns it to the pool if the
            // DataSource is pooled
            conn.close();
         }
       }
     }
   }

I hope this helps.

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
JavaServer Pages        http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to