Exception Handlers in Struts Example Continued ......
(3) Creating the Action Class
The Action class is same as we have learned just with a small change. We are throwing an error from the Action Class. so the code for the execute method is as follows:-
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception { if(true){
// Throwing exception to call the Exception Handler throw new RuntimeException("No Action"); } return mapping.getInputForward(); }
(4) JSP Page:- index.jsp
Note :- The below jsp will only call the Exception.do and then exception handler will pass the control again to this page.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="exception.do"> <input type="submit" value="Click"> </form> </body> </html>
(5) Output:-
The program will print Exception Handler for the specific error in the console window which shows that the CustomExceptionHandler is called before redirecting to the specific page. |