Results 1 to 3 of 3
  1. #1
    an24 is offline Member
    Join Date
    Nov 2009
    Rep Power

    Default Error handling in servlet

    When a JSP throws java.sql.SQLException, then in can be handled via

    But a servlet can throw only ServletException and IOException.

    Question 1.Am I right thinking, that <exception-type> element assumed to be used only with JSP, and for Servlet we should <exception-code> and sendError(code) method?

    Question 2.
    Can we somehow use <exception-type>java.sql.SQLException</exception-type> to handle java.sql.SQLException in a Servlet?

    Question 3. What is the best way to handle SQLException & ClassNotFoundException in JDBC methods?
    Last edited by an24; 12-21-2009 at 03:53 PM.

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    Never tried before but following might be possible

    Parse the xml and maintain a hashtable/map making the exception type as key and error page url as value

    Java Code:
    public void doPost/doGet(..){
    	catch(SQLException e){
    		String errorPageURL=(String)map.get(e.getClass().getName());
    		//forward to errorPageURL

  3. #3
    FON is offline Senior Member
    Join Date
    Dec 2009
    Belgrade, Serbia
    Rep Power


    I never ever do anything with SQL from servlet or JSP directly.

    If you want to do this right, you have to seriously rethink of your code design and which part of your code should work with DB.

    1. On JSP page user providers some info and submit input form to servlet.
    2. Servlet takes request params and call further your app logic.
    3. Some class responsible for DB works (call it DBBroker or DBManager...)
    3.1 creates connection (or gets on from connection pool)
    3.2 Queries DB with input params from servlet request

    - if everything is OK, returns query result and other info
    to some other class which will create response for end user,
    and then servlet forwards/redirects it to result result JSP page

    - if SQLException is catched -
    you have to continue your logic by logging it (with e-mail alert sent to developer to clean that mess)
    and create some sort of meaningful error message for end user
    and servlet forwards/redirects this to JSP error page to display it

    i know that this scenario might be too much for you at
    the moment, but if you want to learn thing right,
    spend some tome on MVC, you will do yourself big favour

    good luck ;)

Similar Threads

  1. [SOLVED] error handling
    By jmHoekst in forum New To Java
    Replies: 9
    Last Post: 07-07-2008, 10:46 PM
  2. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  3. SQL error handling example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-12-2008, 10:38 AM
  4. Replies: 0
    Last Post: 09-28-2007, 12:56 PM
  5. Generated servlet error
    By tommy in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts