Results 1 to 3 of 3
Thread: Error handling in servlet
- 12-21-2009, 01:35 PM #1
Member
- Join Date
- Nov 2009
- Location
- Russia
- Posts
- 9
- Rep Power
- 0
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 02:53 PM.
- 12-21-2009, 02:33 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Kolkata
- Posts
- 280
- Rep Power
- 5
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
- 12-21-2009, 06:40 PM #3
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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
-
[SOLVED] error handling
By jmHoekst in forum New To JavaReplies: 9Last Post: 07-07-2008, 10:46 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
SQL error handling example
By Java Tip in forum Java TipReplies: 0Last Post: 02-12-2008, 09:38 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM -
Generated servlet error
By tommy in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-05-2007, 10:46 AM


LinkBack URL
About LinkBacks

Bookmarks