SlideShare una empresa de Scribd logo
1 de 22
Chapter 3



Error Handling



                 http://www.java2all.com
Introduction



               http://www.java2all.com
We already know about error and exception.

   In JSP there are 2 types of exception
1.      Translation time errors
2.      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
a.) Java Exception Handling mechanism

b.) Dealing with exception with page directive

c.) Dealing with exception in Deployment
    Descriptor.




                                     http://www.java2all.com
By Mechanism




               http://www.java2all.com
Java Exception handling mechanism

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
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
URL :
http://localhost:8080/JAVA_PROJECT/HTMLFILE/Input
Data.html




                                        http://www.java2all.com
                                        http://www.java2all.com
Input the integer value in text fields and click
ADD button.

     The browser display the below message,
     Addition = 11

     Now, input the float value in any of the text field
and click ADD button so the browser display the
message,

     Exception :
     java.lang.NumberFormatException: For
input string: "6.3"
                                              http://www.java2all.com
http://www.java2all.com
 http://www.java2all.com
By Page Directive




                    http://www.java2all.com
Dealing exception with page directive :

      The two attributes of page directive
errorPage and isErrorPage are used to deal with
exception.
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
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
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
Input the integer value in textfields and click
ADD button.

     The browser display the below message,
     Addition = 11

      Now, input the float value in any of the
textfield and click ADD button so the browser
display the message,

     Your page generate an Exception.
     For input string: "6.3"
                                            http://www.java2all.com
In Deployment Descriptor




                      http://www.java2all.com
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
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
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
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</exce
ption-type>
  <location>/Error.jsp</location>
 </error-page>
</web-app>
                                                          http://www.java2all.com
NOTE : web.xml (deployment descriptor) file is
available in WEB-INF folder at WebRoot.

   Input the integer value in textfields and click
ADD button.

     The browser display the below message,
     Addition = 11

      Now, input the float value in any of the
textfield and click ADD button so the browser
display the message,
                                            http://www.java2all.com
Your page generates an Exception.
     For input string: "6.3“

     This deployment descriptor entry means that
whenever a web component throws a
NumberFormatException from any web page in
the whole application(web project),

     the web container call the Error.jsp file,
which simply reports the error message in web
browser.

                                             http://www.java2all.com

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Servlets
ServletsServlets
Servlets
 
State Machine Diagram
State Machine DiagramState Machine Diagram
State Machine Diagram
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Session tracking in servlets
Session tracking in servletsSession tracking in servlets
Session tracking in servlets
 
Java exception
Java exception Java exception
Java exception
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Generics
GenericsGenerics
Generics
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Event handling
Event handlingEvent handling
Event handling
 
Types of Drivers in JDBC
Types of Drivers in JDBCTypes of Drivers in JDBC
Types of Drivers in JDBC
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Serialization in java
Serialization in javaSerialization in java
Serialization in java
 

Similar a JSP Error handling

Understanding JSP -Servlets
Understanding JSP -ServletsUnderstanding JSP -Servlets
Understanding JSP -ServletsGagandeep Singh
 
Oracle Endeca Developer's Guide
Oracle Endeca Developer's GuideOracle Endeca Developer's Guide
Oracle Endeca Developer's GuideKeyur Shah
 
การเข ยนโปรแกรมต ดต_อฐานข_อม_ล
การเข ยนโปรแกรมต ดต_อฐานข_อม_ลการเข ยนโปรแกรมต ดต_อฐานข_อม_ล
การเข ยนโปรแกรมต ดต_อฐานข_อม_ลBongza Naruk
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran TochAdil Jafri
 
Servlet and jsp development with eclipse wtp
Servlet and jsp development with eclipse wtpServlet and jsp development with eclipse wtp
Servlet and jsp development with eclipse wtpodilodif
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceNiraj Bharambe
 
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 MinutesBuild Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 MinutesRobert Li
 
jQuery basics
jQuery basicsjQuery basics
jQuery basicsKamal S
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersJames Gray
 
Java serverpages
Java serverpagesJava serverpages
Java serverpagesAmit Kumar
 

Similar a JSP Error handling (20)

Jsp element
Jsp elementJsp element
Jsp element
 
Understanding JSP -Servlets
Understanding JSP -ServletsUnderstanding JSP -Servlets
Understanding JSP -Servlets
 
Oracle Endeca Developer's Guide
Oracle Endeca Developer's GuideOracle Endeca Developer's Guide
Oracle Endeca Developer's Guide
 
การเข ยนโปรแกรมต ดต_อฐานข_อม_ล
การเข ยนโปรแกรมต ดต_อฐานข_อม_ลการเข ยนโปรแกรมต ดต_อฐานข_อม_ล
การเข ยนโปรแกรมต ดต_อฐานข_อม_ล
 
Ta Javaserverside Eran Toch
Ta Javaserverside Eran TochTa Javaserverside Eran Toch
Ta Javaserverside Eran Toch
 
Servlet and jsp development with eclipse wtp
Servlet and jsp development with eclipse wtpServlet and jsp development with eclipse wtp
Servlet and jsp development with eclipse wtp
 
Jsp intro
Jsp introJsp intro
Jsp intro
 
Advanced java practical semester 6_computer science
Advanced java practical semester 6_computer scienceAdvanced java practical semester 6_computer science
Advanced java practical semester 6_computer science
 
Rest hello world_tutorial
Rest hello world_tutorialRest hello world_tutorial
Rest hello world_tutorial
 
Jsp1
Jsp1Jsp1
Jsp1
 
jQuery PPT
jQuery PPTjQuery PPT
jQuery PPT
 
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 MinutesBuild Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
Test2
Test2Test2
Test2
 
Test2
Test2Test2
Test2
 
Lecture2
Lecture2Lecture2
Lecture2
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and Controllers
 
Ajax3
Ajax3Ajax3
Ajax3
 
Java serverpages
Java serverpagesJava serverpages
Java serverpages
 
Jsp tutorial
Jsp tutorialJsp tutorial
Jsp tutorial
 

Más de kamal kotecha

Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Examplekamal kotecha
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPTkamal kotecha
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods pptkamal kotecha
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with codekamal kotecha
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySqlkamal kotecha
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types pptkamal kotecha
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of javakamal kotecha
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operatorkamal kotecha
 

Más de kamal kotecha (20)

Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Java rmi
Java rmiJava rmi
Java rmi
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
 
Jdbc api
Jdbc apiJdbc api
Jdbc api
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Packages and inbuilt classes of java
Packages and inbuilt classes of javaPackages and inbuilt classes of java
Packages and inbuilt classes of java
 
Interface
InterfaceInterface
Interface
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Class method
Class methodClass method
Class method
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Control statements
Control statementsControl statements
Control statements
 
Jsp myeclipse
Jsp myeclipseJsp myeclipse
Jsp myeclipse
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 

Último

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Último (20)

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

JSP Error handling

  • 1. Chapter 3 Error Handling http://www.java2all.com
  • 2. Introduction http://www.java2all.com
  • 3. We already know about error and exception. In JSP there are 2 types of exception 1. Translation time errors 2. 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. a.) Java Exception Handling mechanism b.) Dealing with exception with page directive c.) Dealing with exception in Deployment Descriptor. http://www.java2all.com
  • 5. By Mechanism http://www.java2all.com
  • 6. Java Exception handling mechanism 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
  • 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. URL : http://localhost:8080/JAVA_PROJECT/HTMLFILE/Input Data.html http://www.java2all.com http://www.java2all.com
  • 9. Input the integer value in text fields and click ADD button. The browser display the below message, Addition = 11 Now, input the float value in any of the text field and click ADD button so the browser display the message, Exception : java.lang.NumberFormatException: For input string: "6.3" http://www.java2all.com
  • 11. By Page Directive http://www.java2all.com
  • 12. Dealing exception with page directive : The two attributes of page directive errorPage and isErrorPage are used to deal with exception. 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. 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. 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. Input the integer value in textfields and click ADD button. The browser display the below message, Addition = 11 Now, input the float value in any of the textfield and click ADD button so the browser display the message, Your page generate an Exception. For input string: "6.3" http://www.java2all.com
  • 16. In Deployment Descriptor http://www.java2all.com
  • 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. 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. 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. 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</exce ption-type> <location>/Error.jsp</location> </error-page> </web-app> http://www.java2all.com
  • 21. NOTE : web.xml (deployment descriptor) file is available in WEB-INF folder at WebRoot. Input the integer value in textfields and click ADD button. The browser display the below message, Addition = 11 Now, input the float value in any of the textfield and click ADD button so the browser display the message, http://www.java2all.com
  • 22. Your page generates an Exception. For input string: "6.3“ This deployment descriptor entry means that whenever a web component throws a NumberFormatException from any web page in the whole application(web project), the web container call the Error.jsp file, which simply reports the error message in web browser. http://www.java2all.com