This article is about handling exceptions that occurr in JSP including the error pages.
Exception handlingExceptions are objects present in the program that are thrown at runtime. Exception handling is the process to handle those exceptions that may turn into runtime errors. They may occur at any time in your web application and might irritate the user. So it's better to be safe by removing those errors and provide your web application with user-friendlier error response pages.Performing exception handlingThe following are two ways to perform exception handling:
JSP Error page configurationTo create an JSP error page, we need to set the page directive attribute isErrorPage value to true, then we can access the exception implicit object and can use it to send the error alert to the user. We also need to set the page directive attribute errorPage to define JSP that will handle all the types of exceptions thrown by the JSP service method.JSP error page deployment descriptor configurationIf you notice that most frequently we come across a common error page that we want to use for all the JSPs, so instead of doing that for all the pages individually, we can define an error page in web.xml with an <error-page> element. By configuring the error pages the exceptions like 404 codes can be handled.Now let's check this by taking the example.Example of exception handling in JSP by the elements of a page directiveWe are creating here five JSP files for explaining the concept.Let's start with an index.jsp that will be our main page.
Output
Error.jsp for errors related to login:
Output for login:Now User1.jsp for computation:
Error1.jsp related to computation:
Pro WPF: Windows Presentation Foundation in .NET 3.0