The simple answer is that in your catch block, don't throw a new exception. When
you throw the Unavailable exception, it gets passed up to the next caller in the
stack, in this case the Server. When the server catches an exception from the
servlet, it unloads the servlet.

The "correct" thing to do is not to throw a new exception. The only thing I can
think that you would want to do in init is log the error somehow. Then in your
method service, doPost, or doGet check the con object before attempting
anything. If con is null you can return directly to the user with an HTML page
that says something like, "Sorry, your request cannot be processed now. Please
try again later." You could also attempt to reload the driver or recreate the
Connection before responding to the user. (Alternately, put a try-catch around
any use of the Connection; in the catch block you would respond back to the user
as above).

K Mukhar

"Cote, Stephane" wrote:
>
> Hi all
>
>         First of all, Servlets are the best and so much fun to work with.
> But I seem to be missing something in my overall understanding of error
> handling for Servlets.  How would I write the catch routine so that the
> Servlet is not unloaded when an error occurs.  I've included a part of my
> class.
>
> Any other examples would be great also.
>
> Thanks.
>
> Stef
>
> public class Login extends HttpServlet {
>
>         Connection con = null;
>
>         public void init(ServletConfig config) throws ServletException {
>                 super.init(config);
>                 try {
>                         // Load the Sybase driver
>                         Class.forName("com.sybase.jdbc.SybDriver");
>                         // Connect to db
>                         con =
> DriverManager.getConnection("jdbc:sybase:Tds:scote:2638","userid","password"
> );
>                 }
>                 catch (ClassNotFoundException e) {
>                         throw new UnavailableException(this, "Couldn't load
> database driver");
>                 }
>                 catch (SQLException e) {
>                         throw new UnavailableException(this, "Couldn't get
> db connection");
>                 }
>         }
> .......
> }

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to