Servlet init

Discussion in 'Java' started by Sameer, Jun 15, 2006.

  1. Sameer

    Sameer Guest

    The init method for a servlet is as follows:

    public void init(){
    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    connection =
    DriverManager.getConnection("jdbc:eek:dbc:library");
    } catch(ClassNotFoundException cnfe) {
    System.err.println("Error loading driver: " + cnfe);
    } catch (SQLException ex) {
    ex.printStackTrace();
    }
    }

    The Stack Trace does not print in the browser and we also do not have
    access to HttpServletResponse object.
    Then how it is possible to display an error message in the client's
    browser if database connection fails?
     
    Sameer, Jun 15, 2006
    #1
    1. Advertising

  2. Sameer

    Oliver Wong Guest

    "Sameer" <> wrote in message
    news:...
    > The init method for a servlet is as follows:
    >
    > public void init(){
    > try {
    > Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    > connection =
    > DriverManager.getConnection("jdbc:eek:dbc:library");
    > } catch(ClassNotFoundException cnfe) {
    > System.err.println("Error loading driver: " + cnfe);
    > } catch (SQLException ex) {
    > ex.printStackTrace();
    > }
    > }
    >
    > The Stack Trace does not print in the browser and we also do not have
    > access to HttpServletResponse object.
    > Then how it is possible to display an error message in the client's
    > browser if database connection fails?
    >


    Try emitting the output to standard out instead of standard error.

    - Oliver
     
    Oliver Wong, Jun 15, 2006
    #2
    1. Advertising

  3. Sameer

    Andy Flowers Guest

    Sameer wrote:
    > The init method for a servlet is as follows:
    >
    > public void init(){
    > try {
    > Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    > connection =
    > DriverManager.getConnection("jdbc:eek:dbc:library");
    > } catch(ClassNotFoundException cnfe) {
    > System.err.println("Error loading driver: " + cnfe);
    > } catch (SQLException ex) {
    > ex.printStackTrace();
    > }
    > }
    >
    > The Stack Trace does not print in the browser and we also do not have
    > access to HttpServletResponse object.
    > Then how it is possible to display an error message in the client's
    > browser if database connection fails?
    >


    Once you have found an error you can set a class level variable, or an
    application level attribute, with details of the failure.

    For example you might have a boolean flag to indicate success/failure of the
    database connection, and a string for the message. These could be used at the
    start of requests.

    Alternatively you could look into using the DataSource interface, and connection
    pools, supported by J2EE.

    See http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Resources3.html#wp80235 for
    some details.

    Here's a snippet to point you to areas to search for

    Context initContext = new InitialContext();
    Context envContext = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
    Connection conn = ds.getConnection();
    .....

    These are then used on a per call basis and make it easier to get shared
    connections from a pool, and make it easier to detect problems.
     
    Andy Flowers, Jun 15, 2006
    #3
  4. Sameer

    Ben_ Guest

    Hello,

    I think you need some more readings on Servlets and best practices with
    coding
    Servlets.

    Look for ConnectionPool vs. DriverManager to see how to make effective
    access to the database and to externalize configuration settings.

    Also you should not cache the database connection. There are plenty of
    discussions on this and on static or member variables in a Servlet.

    For what the init concerns, check again the Servlet lifecycle methods if
    your intention was to send an error message to the browser from within the
    init method.

    It makes sense however to return a meaningful message when the init failed.
    It's where you'll find the UnavailableException helpful for example.
     
    Ben_, Jun 15, 2006
    #4
  5. Sameer

    Juha Laiho Guest

    "Sameer" <> said:
    >The init method for a servlet is as follows:
    >
    >public void init(){
    > try {
    > Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    > connection =
    >DriverManager.getConnection("jdbc:eek:dbc:library");
    > } catch(ClassNotFoundException cnfe) {
    > System.err.println("Error loading driver: " + cnfe);
    > } catch (SQLException ex) {
    > ex.printStackTrace();
    > }
    > }
    >
    >The Stack Trace does not print in the browser and we also do not have
    >access to HttpServletResponse object.
    >Then how it is possible to display an error message in the client's
    >browser if database connection fails?


    init() will be called before the first time a service method of the
    servlet is called, and init() is called without any reference to
    any inbound request that there might be. The error message will
    (possibly) go to one of the log files.

    One thing you could do here is to throw an UnavailableException,
    and perhaps catch that with an error-handler page to provide a more
    user-friendly error message.

    Or, as was suggested in another message, have init() set a flag
    variable within the servlet object to signify failure in jdbc
    initialization, and show an error message based on that flag.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, Jun 15, 2006
    #5
    1. Advertising

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. Tony Cheng
    Replies:
    1
    Views:
    8,259
    Juan T. Llibre
    Feb 24, 2006
  2. Mitch
    Replies:
    2
    Views:
    564
    Mitch
    Mar 13, 2006
  3. Replies:
    1
    Views:
    671
    Jules
    Aug 18, 2005
  4. Jess
    Replies:
    4
    Views:
    457
  5. news.aon.at
    Replies:
    11
    Views:
    658
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page