SlideShare uma empresa 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

Mais conteúdo relacionado

Mais procurados (20)

Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Servlet.ppt
Servlet.pptServlet.ppt
Servlet.ppt
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Java script
Java scriptJava script
Java script
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
Basics of JavaScript
Basics of JavaScriptBasics of JavaScript
Basics of JavaScript
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Xml web services
Xml web servicesXml web services
Xml web services
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Javascript
JavascriptJavascript
Javascript
 
Dom
DomDom
Dom
 

Semelhante 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
 

Semelhante 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
 

Mais 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
 

Mais 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

INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 

Último (20)

INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 

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