"Steven W. Rock" wrote:

> Hi All,
>
> Thanks for your thoughtful and enlightening threads of discussion on the
> Model 2 architecture. I have been building a web site using Servlets, beans,
> java classes and JSP based on this model. The one question I have which
> seems to be missing from these discussions is where do I store the
> Connection Pool? My current thoughts are to initialize the connection pool
> in the controller servlet init() function, create the Pool as a Singleton,
> then any java class at the persistence level that needs a connection just to
> call ConnectionPool.getInstance(). Is this what most people are doing? The
> other option I suppose is to pass a connection through the Java Bean Action
> class, down through the persistence java classes.
>

One option to consider is storing the connection pool as a servlet context
attribute.  Go ahead and create it in the init() method of your controller servlet,
then execute:

    ConnectionPool pool = ...;
    getServletContext().setAttribute("pool", pool);

Now, if you've read my suggestions on the method signature of the perform method in
action classes, you will note that I pass the servlet itself as an argument:

    public interface Action {
        public void perform(HttpServlet servlet,
          HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException;
    }

One of the main reasons I do this is so that action classes have access to the
resources of the servlet context.  In particular, from within the perform() method
of an action class, I can call:

    ConnectionPool pool = (ConnectionPool)
      servlet.getServletContext().getAttribute("pool");
    Connection conn = pool.getConnection();

and pass the connection on to business logic beans that need it.


>
> In a similar vein, how do people handle logging. I wish to have one logger
> class accessible by all classes in my application. This includes the
> Servlets, beans and regular java classes.
>

I use the same technique for other application-wide shared resources as I described
for connection pools above -- store them in the servlet context attributes.  As an
added bonus, I can even refer to these resources in a JSP page:

    <jsp:useBean id="logger" class="com.mycompany.mypackage.MyLogger"
     scope="application"/>

or in an action class:

    MyLogger logger = (MyLogger)
      servlet.getServletContext().getAttribute("logger");


> I've programmed with a proprietary Application Server Web Objects, that had
> one Application class that was accusable by all other classes in the
> application. Here I stored the logging, and any other global services. How
> would I handle this with Servlets, JSP, bean Model 2 architecture.
>

That's pretty much what servlet context attributes are for.

>
> Thanks for your time in this matter.
>
> -Steven Rock
> hitmusic.com  - choose and watch music videos of today's top hits.
>

Craig McClanahan

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

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to