JSP Error handling

5,600 views

Published on

jsp error and exception handling ppt with error page and web.xml

Published in: Education, Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,600
On SlideShare
0
From Embeds
0
Number of Embeds
372
Actions
Shares
0
Downloads
0
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

JSP Error handling

  1. 1. Chapter 3Error Handling http://www.java2all.com
  2. 2. Introduction http://www.java2all.com
  3. 3. We already know about error and exception. In JSP there are 2 types of exception1. Translation time errors2. Run Time Exception Translation error occurs during page compilation, this results in an Internal Server Error (500). An exception on other hand occurs when page is compiled and servlet is running. Exception can be handled in JSP in three ways: http://www.java2all.com
  4. 4. a.) Java Exception Handling mechanismb.) Dealing with exception with page directivec.) Dealing with exception in Deployment Descriptor. http://www.java2all.com
  5. 5. By Mechanism http://www.java2all.com
  6. 6. Java Exception handling mechanismInputData.html :<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>InputData.html</title> </head> <body> <form action="../JSPFILE/Calculator.jsp"> <input type="text" name="n1"> <br> <input type="text" name="n2"> <br> <input type="submit" value="ADD"> </form> </body></html> http://www.java2all.com
  7. 7. Calculator.jsp :<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><html> <head> <title> Calculator.jsp </title> </head> <body> <% try { int i1 = Integer.parseInt(request.getParameter("n1")); int i2 = Integer.parseInt(request.getParameter("n2")); int add = i1 + i2; out.print("Addition = "+add); } catch(NumberFormatException ne) { out.print("Esception : "+ne); } %> </body></html> http://www.java2all.com
  8. 8. URL :http://localhost:8080/JAVA_PROJECT/HTMLFILE/InputData.html http://www.java2all.com http://www.java2all.com
  9. 9. Input the integer value in text fields and clickADD button. The browser display the below message, Addition = 11 Now, input the float value in any of the text fieldand click ADD button so the browser display themessage, Exception : java.lang.NumberFormatException: Forinput string: "6.3" http://www.java2all.com
  10. 10. http://www.java2all.com http://www.java2all.com
  11. 11. By Page Directive http://www.java2all.com
  12. 12. Dealing exception with page directive : The two attributes of page directiveerrorPage and isErrorPage are used to deal withexception.InputData.html :<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>InputData.html</title> </head> <body> <form action="../JSPFILE/Calculator.jsp"> <input type="text" name="n1"> <br> <input type="text" name="n2"> <br> <input type="submit" value="ADD"> </form> </body></html> http://www.java2all.com
  13. 13. Calculator.jsp :<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"errorPage="Error.jsp"%><html> <head> <title> Calculator.jsp </title> </head> <body> <% int i1 = Integer.parseInt(request.getParameter("n1")); int i2 = Integer.parseInt(request.getParameter("n2")); int add = i1 + i2; out.print("Addition = "+add); %> </body></html> http://www.java2all.com
  14. 14. Error.jsp :<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"isErrorPage="true"%><html> <head> <title>Error.jsp</title> </head> <body> Your page generate an Exception. <br> <%= exception.getMessage() %> </body></html> http://www.java2all.com
  15. 15. Input the integer value in textfields and clickADD button. The browser display the below message, Addition = 11 Now, input the float value in any of thetextfield and click ADD button so the browserdisplay the message, Your page generate an Exception. For input string: "6.3" http://www.java2all.com
  16. 16. In Deployment Descriptor http://www.java2all.com
  17. 17. InputData.html :<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>InputData.html</title> </head> <body> <form action="../JSPFILE/Calculator.jsp"> <input type="text" name="n1"> <br> <input type="text" name="n2"> <br> <input type="submit" value="ADD"> </form> </body></html> http://www.java2all.com
  18. 18. Calculator.jsp :<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><html> <head> <title> Calculator.jsp </title> </head> <body> <% int i1 = Integer.parseInt(request.getParameter("n1")); int i2 = Integer.parseInt(request.getParameter("n2")); int add = i1 + i2; out.print("Addition = "+add); %> </body></html> http://www.java2all.com
  19. 19. Error.jsp :<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"isErrorPage="true"%><html> <head> <title>Error.jsp</title> </head> <body> Your page generate an Exception. <br> <%= exception.getMessage() %> </body></html> http://www.java2all.com
  20. 20. Web.xml :<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <error-page> <exception-type>java.lang.NumberFormatException</exception-type> <location>/Error.jsp</location> </error-page></web-app> http://www.java2all.com
  21. 21. NOTE : web.xml (deployment descriptor) file isavailable in WEB-INF folder at WebRoot. Input the integer value in textfields and clickADD button. The browser display the below message, Addition = 11 Now, input the float value in any of thetextfield and click ADD button so the browserdisplay the message, http://www.java2all.com
  22. 22. Your page generates an Exception. For input string: "6.3“ This deployment descriptor entry means thatwhenever a web component throws aNumberFormatException from any web page inthe whole application(web project), the web container call the Error.jsp file,which simply reports the error message in webbrowser. http://www.java2all.com

×