Struts Interceptor

Struts Interceptor are like filters which get processed just before and after action called.

debugging, logging, i18n, params, validation are the default interceptors provided by Struts.

Note: Use link to create practice struts project Link.

Follow below steps once project setup is done and started working in local Eclipse.

Creation an interceptor require implementation of Interceptor interface from com.opensymphony.xwork2.interceptor.Interceptor package.

1. Create an interceptor name as UserInterceptor.java class as shown below.

init() → This method will get all only once at the time of server starts.

destroy() → This method will get call once this interceptor or application is undeployed

intercept(ActionInvocation invocation) → This will get call every time interceptor gets call.

2. Update struts.xml file to define interceptor using <interceptor> tag.

<interceptors> to define all individual interceptors.

<interceptor> to define actual interceptor with class and name attribute.

<interceptor-ref> This is to call above created interceptor with the help of name attribute.

3. Run application with Apache tomcat server and hit below url will give below output:

http://localhost:8081/struts-demo/user.action


OUTPUT:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan