Servlet init

S

Sameer

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?
 
O

Oliver Wong

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?

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

- Oliver
 
A

Andy Flowers

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?

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.
 
B

Ben_

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.
 
J

Juha Laiho

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top