servlet jdbc

Discussion in 'Java' started by warth33@hotmail.com, Aug 6, 2007.

  1. Guest

    Hello

    I have a web app that is composed by a lot of pages, page1, page2,
    page3. Everypage is a servlet. The servlet will eventually redirect to
    a jsp.

    I am using jndi datasource, thats the first time. I managed
    configuring server.xml, web.xml.

    Now, in the servlet page1, in its init method, I have code like this:

    Context env = (Context) new InitialContext().lookup("java:comp/env");
    pool = (DataSource) env.lookup("jdbc/TestDB");

    Later, in the doget, I do database operations. So it works so long.

    But I am a little bit concerned about the other servlets. How can
    servlet named page2, servlet named page3, and so on access the
    database? Do they also need to have its own init method, and they also
    need the code I showed above? maybe it has to be this way, but it
    seems like you do the same job several times.

    Isnt there a way to make all the webapp share the Connection pooling
    that was created by a single servlet start?
     
    , Aug 6, 2007
    #1
    1. Advertisements

  2. On Aug 6, 11:22 am, wrote:
    > Hello
    >
    > I have a web app that is composed by a lot of pages, page1, page2,
    > page3. Everypage is a servlet. The servlet will eventually redirect to
    > a jsp.
    >
    > I am using jndi datasource, thats the first time. I managed
    > configuring server.xml, web.xml.
    >
    > Now, in the servlet page1, in its init method, I have code like this:
    >
    > Context env = (Context) new InitialContext().lookup("java:comp/env");
    > pool = (DataSource) env.lookup("jdbc/TestDB");
    >
    > Later, in the doget, I do database operations. So it works so long.
    >
    > But I am a little bit concerned about the other servlets. How can
    > servlet named page2, servlet named page3, and so on access the
    > database? Do they also need to have its own init method, and they also
    > need the code I showed above? maybe it has to be this way, but it
    > seems like you do the same job several times.
    >
    > Isnt there a way to make all the webapp share the Connection pooling
    > that was created by a single servlet start?


    One solution could be to have something like a ConfigurationLoader
    which can create the datasource/pool and keep it as a static variable
    accessible to all servlets. The configuration loader's initialization
    can be called via the contextlistener's contextInitialized() method.
    You can put any global initialization stuff in this class as well.

    Something like:

    public class ApplicationConfiguration{

    public static DataSource dataSource = null;

    public static initialize(){
    dataSource = ...
    }

    public static void getDataSource(){
    if(dataSource==null) initialize();
    return dataSource;
    }

    }

    ServletContextListener:

    public void contextInitialized(ServletContextEvent event){
    ApplicationConfiguration.initialize();
    }

    -cheers,
    Manish
     
    Manish Pandit, Aug 6, 2007
    #2
    1. Advertisements

  3. oops!

    > public static DataSource dataSource = null;


    private static DataSource dataSource = null;

    > public static initialize()


    public static void initialize()

    -cheers,
    Manish
     
    Manish Pandit, Aug 6, 2007
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ike
    Replies:
    2
    Views:
    507
    Andrew Thompson
    Nov 18, 2003
  2. circuit_breaker
    Replies:
    2
    Views:
    2,385
    Jack Jia
    Apr 4, 2004
  3. Scott D. Barrish

    JDBC - Servlet

    Scott D. Barrish, Apr 4, 2004, in forum: Java
    Replies:
    6
    Views:
    735
    Scott D. Barrish
    Apr 6, 2004
  4. Replies:
    7
    Views:
    931
    Chris Smith
    Jan 23, 2005
  5. Betty
    Replies:
    1
    Views:
    10,232
    Juha Laiho
    May 21, 2005
  6. Email2us

    servlet/jsp/jdbc best approach

    Email2us, Jun 23, 2005, in forum: Java
    Replies:
    0
    Views:
    574
    Email2us
    Jun 23, 2005
  7. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,494
    robert
    Feb 11, 2006
  8. Bruce
    Replies:
    4
    Views:
    952
    Bjorn Abelli
    Mar 25, 2006
Loading...