| Exception Handling in JSP Pages |
| |
| Running the Demo Pages |
| |
| Place Form.php, FormHandler.jsp and ExceptionHandler.jsp pages in a place where our application server ( e.g. Tomcat, Orion etc ) can find them. Never put our JSP pages in the /WEB-INF/ folder. |
| |
| We placed these three pages in the /web/jsp/ folder so the path to these pages on my system will be http://localhost:8080/web/jsp/Form.php and so on. Notice that on our system application server is running on 8080 port. We should change the port number to the one where our application server is running. |
| |
| Enter your age e.g. 24, in the input field and press the submit button. We should see FormHandler.jsp page giving us a response like following : |
| Your age is : 24 years. |
| |
| Now try again. This time don't enter any value or simply enter a non-integer value e.g. your name, and press the submit button. Now instead of FormHandler.jsp page showing us your age, the control will be internally passed to ExceptionHandler.jsp page which will show an exception message in red color : |
| java.lang.NumberFormatException: |
| |
| To see the complete stack trace view the source of the page. On Internet Explorer, click View -> Source. On the source page you will see complete exception stack trace written. |
| |
| Although our demo application worked and we learned about specifying error pages e.g. ExceptionHandler.jsp, in our normal JSP pages e.g. FormHandler.jsp, there is still a catch. The exception message that was given to the user i.e. java.lang.NumberFormatException, is not very useful, is it? To give a more useful message keep reading. |
| |
| |
|
| |
| |