Results 1 to 3 of 3
  1. #1
    an24 is offline Member
    Join Date
    Nov 2009
    Location
    Russia
    Posts
    9
    Rep Power
    0

    Default Error handling in servlet

    When a JSP throws java.sql.SQLException, then in can be handled via
    <error-page>
    <exception-type>java.sql.SQLException</exception-type>
    <location>/ErrorPage.jsp</location>
    </error-page>

    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
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    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(..){
    	try{
    	}
    	catch(SQLException e){
    		String errorPageURL=(String)map.get(e.getClass().getName());
    		//forward to errorPageURL
    	}
    }
    Swastik

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

    Default

    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.

    Scenario:
    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, 11:46 PM
  2. Replies: 0
    Last Post: 04-01-2008, 11: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, 01:56 PM
  5. Generated servlet error
    By tommy in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 11: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
  •