Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Exception Interceptor
2.1.
Internal Working of an Exception Interceptor
2.2.
Parameters of Exception Interceptor
2.3.
Example
2.4.
Displaying Exception
3.
Frequently Asked Questions
3.1.
How do you handle exceptions in struts2?
3.2.
What are the different Interceptors are there in Struts2?
3.3.
What is the filter dispatcher in Struts 2?
3.4.
Why do we use StrutsPrepareAndExecuteFilter filter?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Exception Interceptor

Introduction

Interceptor is an object that is called at the time of preprocessing and postprocessing a request.

We used Interceptor to perform different operations like validation, exception handling, etc.

Interceptors Image

You can refer to this blog to know more about Struts2 and Interceptors.

We have already discussed the fileUpload Interceptor and params Interceptor. You can also check out that.

In this blog, we will discuss the exception interceptor. 

Exception Interceptor

Exception Interceptor

Exception handling allows us to map an exception to a result code so that the result code will not throw an unexpected exception.

Since exceptions in our application may occur at any point, therefore struts2 provides a mechanism of global exception handling to overcome the problem where we can display a global result to the user. It will redirect to an error page when an exception has occurred.

Internal Working of an Exception Interceptor

Internal Working Image

If the exception occurs, it is wrapped in ExceptionHolder and pushed in the valuestack so that we can easily access the exception object from the result.

Parameters of Exception Interceptor

Following are the parameters of Exception Interceptor

  • logEnabled - It specifies whether the log is specified or not. It takes boolean values. Therefore, it can be either true or false.
  • logLevel - It specifies the log level (trace, debug, info, warn, error, fatal), and the default log level is debug.
  • logCategory - It specifies the log category. (eg. com.mycompany.app)

Example

In this example, we define the global-result and global-exception-mappings in the struts.xml file, which specifies the global-result and exception mapping for all the actions of this package, respectively.

We are using the exception, the parent of many exception classes such as IOException, ArithmeticException, etc., which means that if any exception occurs, a specified result will be invoked.

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
 Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
   <package name="aa" extends="struts-default">  
      <global-results>
         <result name="myresult">globalresult.jsp</result>  
      </global-results>  
      <global-exception-mappings>  
         <exception-mapping result="myresult" exception="java.lang.Exception"></exception-mapping>  
      </global-exception-mappings>  
      <action name="login" class="com.Login">  
         <result>welcome.jsp</result>  
         <result name="error">error.jsp</result>  
      </action>  
   </package>  
</struts>


Let's see how we can display the exception.

Displaying Exception

We can display the exception on the browser either by printing the exception or by exceptionStack. The exception object only publishes the name of the exception, whereas exceptionStack prints the details of the exception.


Now, let's talk about some frequently asked questions related to them.

Let's talk Image

Frequently Asked Questions

How do you handle exceptions in struts2?

Struts provide an easier way to handle the uncaught exception and redirect users to a dedicated error page. We can easily configure Struts to have different error pages for various exceptions. Struts make exception handling easy by using the "exception" interceptor.

What are the different Interceptors are there in Struts2?

The different types of Interceptor are- fileUpload Interceptor, params Interceptor, Exception Interceptor, Custom Interceptor, prepare Interceptor, modelDriven Interceptor, execAndWait Interceptor.

What is the filter dispatcher in Struts 2?

FilterDispatcher is a filter that Struts2 provides for handling all requests which need to be controlled by the struts2 framework.

Why do we use StrutsPrepareAndExecuteFilter filter?

Because it handles both the preparation and execution phases of the Struts dispatching process.

Conclusion

In this article, we discussed the exception Interceptor with its internal working parameters and its example. We have also talked about how we can display the exception.

After reading about the exception Interceptor, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See JavaJSP-XMLJSPIntroduction to JSP, and  Spring Boot to learn.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image
 

Live masterclass