Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

Top 40 Struts Interview Questions and Answers for Freshers and Experienced

Published On: February 15, 2025

Global employers are increasingly seeking personnel with expertise in Struts that can be used in newer technologies, such as cloud computing, microservices, and DevOps. Keeping your skills up-to-date helps you remain competitive in the job market. Finetune your skills with this collection of top 40 Struts interview questions and answers carefully crafted for freshers and experienced. Explore our Struts course syllabus to get started.

Struts Interview Questions and Answers for Freshers

The basic concepts are covered in these Struts interview questions and answers for freshers.

1. What is Apache Struts?

An open-source framework for creating Java EE web applications is called Apache Struts. It adheres to the Model-View-Controller (MVC) architectural pattern and extends the Java Servlet API. The benefits of Apache Struts:

  • It makes developers want to use MVC design.
  • It assists programmers in building maintainable and adaptable web apps.
  • It assists programmers in creating adaptable web applications.

2. What are the key components of Struts?

The primary components of Struts are:

  • ActionServlet: It is the controller component.
  • Action: It handles business logic.
  • ActionForm: It represents form data.
  • Struts-config.xml: The configuration file for mapping actions and forms.
  • JSP/View: It is the presentation layer.

3. Explain the MVC architecture in Struts.

A web application can be divided into three separate parts using Struts’ MVC (Model-View-Controller) architecture: 

  • Model: The “Model” manages data and business logic. 
  • View: The “View” displays the user interface. 
  • Controller: The “Controller” serves as a mediator between the Model and View. 

It is taking user input, processing it through the Model, and then selecting which View to display based on the outcomes. This effectively allows for a clear division of responsibilities within the application development process.  

4. What is the role of ActionServlet in Struts?

The ActionServlet serves as the central controller in the Struts framework. 

  • It receives all incoming web requests. 
  • It interprets them according to configuration settings. 
  • It routes the request to the relevant Action class to handle the business logic. 

It is the main element that controls how a web application flows within Struts.  

5. What is an Action class in Struts?

A Java class that manages business logic is called an Action class. It responds with the relevant View after processing user requests and interacting with the Model.

  • After executing business logic (or calling on other classes to do so), action classes react to user actions and deliver a result that instructs Struts on which view to render. 
  • The ActionSupport class, which is supplied by the Struts 2 framework, is typically extended by Struts 2 Action classes.

Recommended: Struts Online Training Program.

6. What is ActionForm in Struts?

A Java class called ActionForm is used to represent form data. Prior to sending user input to the Action class, it is utilized to record and verify it.

7. What is the purpose of struts-config.xml?

Struts applications are configured using the struts-config.xml file. Action mappings, form beans, global forwarding, and other configurations are defined.

All of the navigation rules are defined as Struts action definitions in the Struts configuration file ($WSHOME/WEB-INF/spe/config/struts-config.xml). Typically, path, type, and argument attributes are included in action declarations. 

8. What is the difference between ActionForm and DynaActionForm?

The struts custom tag is used in struts 1.0 to populate the html tags in jsp using an action form.The action class must be modified whenever the Java code is modified. Dyna action form 1.1 is introduced to prevent the changes in struts.This can be utilized for XML development.The struts-config.xml-based definition causes the dyna action form to bloat up.

  • ActionForm: Needs getters and setters for form fields to be manually created.
  • DynaActionForm: Enables form fields to be created dynamically without the need to write getters and setters.

9. What are Struts tags?

Custom JSP tags are offered by Struts for logic control, data presentation, and form handling. Examples: <html:form>, <html:text>, and <logic:iterate>.

The Struts HTML Tags drawer is where you can build Struts HTML tags. Along with other tags that are usually helpful in the building of HTML-based user interfaces, these tags are used to create Struts input forms. When in XHTML mode, the output is XHTML 1.0 or HTML 4.01 compliant.

Learn the fundamentals with our HTML training in Chennai.

10. What is the use of the validate() method in ActionForm?

The ActionForm’s user input is validated using the validate() method. An ActionErrors object with validation errors is returned.

Before the action class is executed, the validate() function provided by the action form class is called. so that you may use this method to validate your properties. A collection (ActionErrors) of error messages (ActionMessage) is the method’s return value. 

11. How do you configure multiple Struts configuration files?

Multiple struts-config.xml files can be specified using the <init-param> tag in web.xml, where a comma “,” can be used to divide the files. This method enables you to divide the configuration file as you see fit.

12. What is the role of the ActionMapping class?

ActionMapping is used to establish the application’s flow and map incoming requests to particular Action classes.

The “ActionMapping” class functions as a configuration element within the Struts framework, connecting a particular URL pattern (or “action”) to a Java class that is in charge of managing that request.

13. What is the purpose of the execute() method in Struts 2?

In Struts 2, user requests are processed using the execute() method, which returns a result (such as SUCCESS or ERROR).

The execute() method, which encapsulates the business logic, a straightforward concatenation of two strings, is called when this action is processed. Simple objects called Struts2 Actions classes resemble POJOs (Plain Old Java Objects). 

14. What is the difference between forward and include in Struts?

The main difference between the two is that the include method leaves the output stream open whereas the forward method closes it after it has been invoked. 

  • Forward: It gives another resource (like JSP or Action) control.
  • Include: It adds another resource’s output to the response.

15. What is the use of the ActionForward class?

After processing, the request is sent to another resource (such as JSP or Action) using ActionForward. It is utilized by RequestDispatcher and is a return type of the Action#execute() function.

16. What is the role of the ActionContext in Struts 2?

Access to an action’s context, including session, request, and application data, is made possible via ActionContext. The context in which an action is performed is known as the ActionContext. 

Each context is a container for the session, parameters, locale, and other items that an action requires in order to be executed. As the ActionContext is thread local, the values it contains are specific to each thread. 

17. What is the difference between session and request scope in Struts?

A session scoped bean endures across several HTTP requests as long as the session is ongoing, but a request scoped bean is only available for a single HTTP request.

  • Request Scope: Only the current request’s data is available.
  • Session Scope: Information is retained for the same user across several requests. 

18. What is the use of the token mechanism in Struts?

The token system creates a distinct token for every form, preventing duplicate submissions. 

The main purpose of Struts’ token mechanism is to stop duplicate form submissions, which are frequently brought on by users inadvertently clicking the submit button more than once or by submitting a form using the browser’s back button. 

This effectively protects against unforeseen data processing problems, such as double charges in an online store.  

19. What is the purpose of the ActionMessages class?

ActionMessages is used to store and show the user messages (such as success or error).

The <html:messages> tag in the JSP view layer serves as a central container for storing and retrieving messages based on their corresponding property names. 

The ActionMessages class in the Apache Struts framework is used to store a collection of messages associated with specific properties or keys, enabling developers to easily manage and display validation or informational messages to the user within a web application. 

20. What is the difference between ActionErrors and ActionMessages?

ActionErrors are deprecated, therefore there is no difference. ActionMessages received all of the behavior from ActionErrors. 

To make it apparent that these classes can be used to send any type of message from the controller to the view, errors being only one type of message, this was done.

  • ActionErrors: For mistakes in validation.
  • ActionMessages: Used to convey generic messages (such as information or success). 

Learn app development from scratch with our Java training in Chennai.

Struts Interview Questions and Answers for Experienced

Here are the advanced struts interview questions and answers for experienced.

1. How does Struts handle exceptions?

Struts uses the struts-config.xml file to handle exceptions both locally and globally. It is possible to link exceptions to particular error pages or actions.

  • There could be an exception in our web application at any time. 
  • Struts 2 offers a global exception handling technique that allows us to show the user a global result to solve this issue. 
  • The user is redirected to the error handler page by Struts 2, which also automatically logs the uncaught exceptions. 

2. What is the use of the Tiles framework in Struts?

In Struts applications, the Tiles framework is used to create consistent and reusable page layouts. By defining common elements like headers, footers, and sidebars in one place, developers can design consistent page structures using Struts’ Tiles framework. 

These reusable components for web page layouts can then be dynamically inserted into various application pages, encouraging code reuse and streamlining page management.  

Review your Java understanding with our Java interview questions and answers

3. What is the difference between declarative and programmatic validation in Struts?

In Struts, “programmatic validation” entails writing custom Java code within your Action class to perform validation checks on form data, giving you more flexibility for complex validation scenarios. “Declarative validation” refers to defining validation rules for form fields within a configuration file (such as struts-config.xml) without writing explicit Java code.  

FeaturesDeclarative ValidationProgrammatic Validation
ValidationConfiguration-based: XML configuration files were used.Complex-based: Completed in ActionForm validate() method.
RulesSimple: Ideal for simple validation checks such as length constraints, necessary fields, and email format. Complex: It enables more complex validation checks, such as those using complex business rules, bespoke computations, or database lookups.
ExampleUsing XML elements in the struts-config.xml file to specify that a username field is necessary and needs to include five to twenty characters.It is creating a unique validation technique that can involve database checks to see if a user’s password complies with a predetermined pattern. 

4. What is the role of interceptors in Struts 2?

Pre-processing and post-processing duties (such as validation and logging) are carried out by interceptors in Struts 2 both before and after an action is carried out.

  • Interceptors, which are essentially hooks that can be plugged into the request lifecycle at specific points to perform various tasks before the actual action execution takes place, are a key mechanism in Struts 2 that execute code both before and after an Action is invoked. 
  • They essentially handle the majority of the framework’s processing, such as data conversion, validation, internationalization, and exception handling, allowing developers to implement cross-cutting concerns without cluttering their Action classes.  

5. What is the use of the ValueStack in Struts 2?

Application data is stored in a stack called the ValueStack, which the view layer can access. Simply said, a value stack is a stack that holds things unique to a given application, like action objects and other model objects. Action is positioned at the top of the stack during execution. We have the ability to add, query, and remove objects from the valuestack. 

6. What is the difference between Struts and Spring MVC?

Struts is an older framework that is closely related to the Servlet API. Spring MVC is a contemporary framework that works well with other Spring modules and is loosely linked. Although both Spring MVC and Struts are Java-based web frameworks that adhere to the Model-View-Controller (MVC) architecture. 

The main distinction between the two is that Spring MVC is more flexible, lighter, and has more sophisticated features like dependency injection, which makes it the better option for contemporary applications. Struts, on the other hand, is frequently regarded as more legacy and tightly coupled, making it best suited for maintaining older projects.  

7. What is the use of the OGNL in Struts 2?

The ValueStack uses OGNL (Object-Graph Navigation Language) to access and modify data.

  • The Struts 2 OGNL concludes that the Object-Graph Navigation Language (OGNL), a potent expression language, can be used to reference and modify the data on the ValueStack. 
  • OGNL also makes data transport and type conversion easier. There are several similarities between the OGNL and the JSP Expression Language.

8. What is the role of the FilterDispatcher in Struts 2?

Struts 2 requests enter through FilterDispatcher. It assigns requests to the proper actions and initializes the framework.

To decide which action to call, the Filter Dispatcher must first validate the request URI. Struts matches URIs with action classes using the struts.xml configuration file. 

Related Training: Spring training in Chennai.

9. What is the use of the @Action annotation in Struts 2?

In Struts 2, action methods are defined using the @Action annotation rather than XML configuration.

  • By designating which method should be called when a particular action link is clicked, the @Action annotation in Struts 2 instructs Struts 2 to execute the annotated method when the value of the action link matches the value specified in the @Action annotation. 
  • This allows a method within a class to be marked as an action that the framework can execute when a matching URL is requested.  

10. What is the difference between Struts and JSF?

Although both JSF and Struts are Java web application frameworks, the main distinction between them is in their architectural approaches: 

  • JSF is a “component” framework that focuses on the view layer and offers a more structured method of creating user interfaces with reusable components. Rich user interface components are provided by this component-based framework. 
  • Struts is primarily an “action” framework that focuses on the controller layer in MVC. JSP is used for views in the action-based framework Struts.

11. How do you configure a Struts application?

Load the struts-config.xml file and configure the web.xml file to define the ActionServlet. 

  • Setting up a web project, adding the required Struts libraries, and creating a “struts.xml” configuration file in which you define action mappings, form beans, results, and other application components are the main steps involved in configuring a Struts application. 
  • You then need to link your JSP pages to the actions in the configuration file in order to handle user requests and display data. 
  • It is a mapping of user interactions to corresponding Java classes that process the logic.  

12. Write a simple Struts action class.

public class HelloWorldAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,

                                 HttpServletRequest request, HttpServletResponse response) {

        request.setAttribute(“message”, “Hello, World!”);

        return mapping.findForward(“success”);

    }

}

13. How do you validate a form in Struts?

If validation is unsuccessful, return ActionErrors and override the ActionForm class validate() method. A second XML file containing your validation rules can be used to verify a user’s form field entries. ActionClassName-validation.xml must be the name of the XML file containing the validation rules. The XML validation file in the example application is called EditAction-validation. 

14. How do you handle file uploads in Struts?

Use either the FileUploadInterceptor in Struts 2 or the FormFile class in Struts 1. 

  • Use the Apache Struts form and file custom tags to create a JSP. 
  • Create a Struts action class with setters that correspond to the file name as indicated by the form’s s:file tag. 
  • Use standard Java IO packages to save the uploaded file in the Struts action class’s execute function.

Hone your skills with Java framework with our Hibernate course in Chennai.

15. How do you implement internationalization in Struts?

We can make use of the <bean:message> element in JSP and resource bundles (ApplicationResources.properties).

Making a resource package for any language you desire is all that is required. Titles, messages, and other material in your user’s language will be included in the resource bundles. Key/value pairs for your application’s default language are contained in resource bundles.

16. How do you handle database operations in Struts?

To communicate with the database, use DAO (Data Access Object) classes, which are called from the Action class.

The characteristics of the action class match those of the database table’s columns. We have three string attributes: name, password, and user. The user and password arguments are used in the action method to determine whether the user is present.

17. How do you handle session management in Struts?

To handle session management in Struts, and to store and retrieve session data, utilize the Action class’s HttpSession object.

18. How do you implement pagination in Struts?

To retrieve and show data in pages, combine the Action class, DAO, and JSP. You must set the pagesize attribute in to leverage the paging functionality; in our example, three items per page should be shown. The URI that is utilized by the links to switch pages is specified by the attribute requestURI. 

19. How do you debug a Struts application?

Check server logs, enable debug mode in struts-config.xml, and use logging frameworks such as Log4j. 

  • Using the Struts framework’s “Debugging Interceptor” feature is the main way to debug a Struts application. 
  • By turning on “devMode” and adding a “debug” parameter to your URL, you can inspect data and the application flow and analyze values in the ValueStack at runtime using various viewing modes like XML, console, and browser. 
  • You can also use your IDE’s debugging features to step through the code and set breakpoints in your Action classes.  

20. What is the difference between Struts 1 and Struts 2?

While Struts 1 requires action classes to extend a specific base class, which makes it less flexible and more tightly coupled with the servlet API, Struts 2 offers more flexibility and a more modern architecture by allowing developers to use plain Java objects (POJOs) as action classes. Struts 2 offers better support for AJAX and other web technologies than Struts 1.

FeaturesStruts 1Struts 2
Action Class DesignAction classes’ versatility is restricted because they have to extend a particular abstract base class. To give designers more flexibility, action classes can be straightforward POJOs with an execute function. 
Thread SafetyBecause action objects are singletons, thread safety must be manually managed by developers.Thread safety issues are resolved by creating a new action object for every request.
Dependency on Servlet APIAction classes strongly couple HttpServletRequest and HttpServletResponse objects to the servlet API by directly accessing them.Action classes have a more adaptable way to obtain request data, which lessens their reliance on the servlet API.
Front ControllerUses the ActionServlet as the front controller. Uses a Filter Dispatcher as the front controller.
Integration with Other TechnologiesIntegration with other frameworks, such as Spring, is less smooth.Its POJO-based design makes it easier to integrate with frameworks like Spring and Hibernate.
ApproachTightly coupled with Servlet API. It uses ActionForm and Action classes.Loosely coupled, uses POJOs (Plain Old Java Objects) for actions.

Struts 2 is the recommended option for the majority of new Java web application development since it is generally regarded as a more contemporary and adaptable framework than Struts 1.

Explore all our software training courses to kickstart your IT career.

Conclusion

A thorough understanding of the architecture, constituent parts, and real-world use of Apache Struts is necessary to prepare for an interview. From fundamental ideas like MVC design, Action classes, and ActionForm to more complex subjects like Struts 2 interceptors, ValueStack, and internationalization, the top 40 Struts interview questions and answers offered cover a wide range of subjects. Secure your career with our Struts training in Chennai.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.