Struts 1.2 Interview Questions Part II

What is the difference between Struts 1 and struts 2?

The difference between struts1 and struts2 are below:

  • Struts1 uses ActionServlet as Controller where as Struts2 uses Filter as a controller.

  • Struts1 uses the ActionForm for mapping the JSP forms but in struts2 there no such ActionForm.

  • Struts1 uses Validation() method to validate the data in the page forms where as struts 2 validation of data is ensure by Validator Framework.

  • In Struts 1 the bean and logic tag libraries are often replaced by JSTL, but Struts 2 has such tag libraries that we don’t need to use JSTL.

Learn more about Struts in our Video Tutorial

What is the difference between RequestAware and ServletRequestAware interface?

The difference between RequestAware and ServletRequestAware are:

  • RequestAware enables programmer with the attributes in the Servlet Request as a map whereas ServletRequestAware enables programmer with HttpServletRequest object.

  • ServletRequestAware are more flexible than RequestAware.

  • ServletRequestAware makes action class highly coupled with Servlet environment which is not possible in RequestAware.

What are inner class and anonymous class?

Inner class : classes that are defined within other classes.

The nesting is a relationship performed between two different classes.

  • An inner class can access private members and data.

  • Inner classes have clearly two benefits:

    1. Name control

    2. Access control.

Anonymous class : Anonymous class is a class defined inside a method without a name.

  • It is instantiated and declared in the same method.

  • It does not have explicit constructors.

What are the steps used to setup dispatch action?

To setup the dispatch action the following steps are used:

  • Create a subclass for DispatchAction.

  • Create method for logical action and their related actions.

  • Request Parameter is created for each action.

  • Define ActionMapping.

  • The JSP takes on the subclass defined for dispatch action method names as their values.

What are the Custom tags?

Custom Tags are User Defined Tags, which means that user can create those tags depending upon their need in the application.

  • When a JSP page consisting of user- defined or custom tag is translated into a Servlet, the custom is also get translated into operation

  • Help in fast development of the application due to custom tag reusability.

What is the difference between empty default namespace and root namespace?

The difference between the empty default namespace and root name space are:

  • When namespace attribute is not defined then it is referred to as Empty Default Namespace whereas when name space attribute is assign with forward slash(/) then it is referred to as Root Name Space.

  • The root namespace must be matched.

What is difference between Interceptors and Filters?

The difference between Interceptors and filter are below:

  • Filters are based on Servlet Specification whereas Interceptors are based on Struts2.

  • Filters are executed only when patter matches whereas Interceptors executes for all request qualifies for a front controller.

  • Filters are not Configurable method calls whereas Interceptors methods can be configured.

What is struts.devMode?

The struts.devMode is used to make sure that framework is running in development mode or production mode by setting true or false. struts.devMode is set to false in production phase to reduce impact of performance. By default it is "false". It is used because of the following reasons:

  • Resource Reloading : Resource bundle reload on every request.

  • Modification : struts.xml can be modified without restarting or redeploying the application.

  • Error Handling : The error occurs in the application will be reported, as oppose to production mode.

What are action errors?

When user or client submits the incorrect or invalid data in the application, then these errors are known as Action error.

  • Action errors are generated by the clients.

  • Action error should be determined as soon as possible.

The impacts of such Action Error are:

  • Wastage of server time and resources.

  • Negative impact on code quality.

Describe the components of Struts.
  1. Model Components :
    Many application’s focus is on the view. The processing required for each submitted form / request by keeping the model’s perspective in view. In general the focus for handling the model components is done by the creation of java beans that support the functionality requirements. The scope concept is related to the beans is used first. The concept of extending ActionForm class is assumed by the Struts framework for each input form. ActionForm is a bean that are declared in ActionMapping configurationfile. The struts controller servlet automatically performs the services required, before invoking the appropriate Action method.

  2. View Components :
    Struts supports the interacting input forms and used for building internationalized applications. Some files are uploaded by using HTML forms. Most of the browsers support the uploading of files through a input tag like < input type=”file” >. This generates a file browse button. Struts handles these kind of forms in a way that is identical to building normal forms.

  3. Controller Components :
    The primary function of mapping request URI to an Action class that is implemented by the servlet. This operation also included in Struts framework. So, the Controller responsibilities include:

    • Write an Action class for every request that may be received.

    • Configuring an ActionMapping, that is in the form of XML file, for every logical requests that implements.

    • The name of the file is struts-config.xml.

    • Updating the deployment descriptor file for the application to include the necessary components.

    • Adding the Struts components to the application.

What is Jakarta Struts Framework?

An open source framework for creating web applications. Web applications, some deliver static content and some create dynamic response. A web application can perform database actions and perform business logic to make responses customized.

A web application that uses JSP sometimes mix the operations like database code, design code, and control code. In this way a web application becomes difficult to maintain.

Model, View, Control(MVC) architecture is one of the ways to separate the concerns in developing software applications. The struts framework is designed to isolate these process (MVC architecture) while designing web applications.

The struts framework provides the key components for a web applicatons such as:

  • A “request” is being handled by the request handler which is developed by application developer which is mapped to a standard URI.

  • A “response” handler – transferring the control from one resource to another that finishes the process of response.

  • A tag library – which is used to create interactive form-based applications by the developers with server pages.

Struts also works well with the conventional applications with SOAP and AJAX.

Explain the core classes of the Struts Framework.

The core classes of Struts framework are:

  • ActionServlet :
    ActionServlet is the back bone of the whole web application.

  • ActionForm :
    ActionForm is a Java bean that associates one or more ActionMappings. The bean is initialized from the corresponding parameters of requests well before the Action.execute method is invoked.

    The bean’s validate() method will be called, before the execution of execute() method. This process is done before the bean properties have been populated. The verification of properties submitted by the user input form is validated by the validate() method. If the method finds problems, an error messages object those are encapsulated the problems will be returned and the controller servlet will return the control back to the input form. If not , the validate() method returns null, which is the indication of accepting everything, and the Action.execute method should be invoked.

  • Action :
    This class is extended by the org.apache.struts.action.Action class. The business logic is wrapped by the Action class and this class provides an interface to the Model of the application. Action class can be viewed as a glue between the View and the Model layer of MVC architecture. The data transfer from the view layers to the specific business process layer(View to Mode) is done by this class. The processed data from the business layer to view layer is returned ultimately.

    The struts controller i.e., ActionServlet , chooses an appropriate Action and the instance is created if necessary and invokes execute() method.

  • ActionMapping :
    The ActionMapping is used to provide mappings for Objects to Actions. An action map is used in association with InputMap to locate an action at time of pressing a key. An action mapping can contain a reference to a form bean that the action can use. A reference to a form bean is contained by an action mapping. The reference to a form is used by an action.

  • ActionForward :
    It is represented as a destination for the controller, RequestProcessor, that might be directed a RequestDispatcher.forward or HttpServletResponse.sendRedirect to. This is the result of processing activities of an Action class. The instances of this classes is created dynamically as the need arises.

What are the drawbacks of Struts?

Drawbacks of Struts:

  • No backward flow

  • Single controller servlet – Only one ActionServlet is available which may be an issue of scalability.

  • This framework does not any exception, where there are errors in the config files.

  • Less transparent

  • Rigid approach.

  • With struts 1, embedding application into JSP can’t be prevented.

  • Non-XML compliance of JSP syntax

Explain the design patterns used in Struts.

The Struts design pattern has N-tier architecture. This includes MVC architecture, Web applications characters, struts framework which includes – architecture, design patterns and coding idioms.

N-Tier architecture :

The architecture includes the following layers and functionalities:

  • Client

  • User Interface

  • Presentation

  • Page Layout

  • Control

  • Command

  • Business Logic

  • Business Delegate

  • Data Access

  • Domain Model

  • Resources

  • Database, enterprise services


MVC Architecture :

The MVC architecture will isolate Model, View and the Controller functionality , which eases the development of the web applications without intervention of a database programmer with a web page designer to that of the controlling the application.


Web Application Characteristics :

  • Browsers used to display the text

  • Objects makes the domain model

  • The data persistence is usually in relational databases

  • The flow of pages changes


Struts Framework :

  • A request from a HTML page will be sent to Struts Servlet that creates a form

  • This form is sent to the ActionForm subclass. Any HTTP parameters may be sent to this subclass.

  • Form validation process is sent to ActionForm subclass.

  • Execution of ActionForm subclass methods and returns a forward page

  • The forward page is sent to the JSP.

  • The response in the form of HTML will be sent to the Client

Explain Struts navigation flow.
  • After deploying the application in the server, the container reads the information from web.xml

  • The ActionServlet object will be created. The init() method for ActionServlet will be invoked.

  • The getters() and reset() methods of FormBean method will be invoked, after the client sent a request using .jsp extension.

  • After pressing the submit button by the client, the setters() and validate() methods will be invoked.

  • In case the page is not a valid page, the form redirects to another page that is indicated from the struts-config.xml file.

  • The Action class object will be created when and if the data is valid

  • The business logic in Model can be specified and provides that object in execute() method.

  • Once the business logic process is done, it forwards to a page( a success page ) that is specified in struts-config.xml file.

Different kinds of actions in Struts.

The different kinds of actions are done by the following :

  • DispatchAction :
    One of the built-in actions provided by the struts framework. The collection of related functions into a single Action is possible using this action. It allows eliminating the creation of multiple independent actions for each function.

  • ActionDispatcher :
    It is a helper class which dispatches to a method that is in Action. This class provides an alternative mechanism for using DispatchAction. With the help of various types of mechanisms, the ‘dispatching’ behavior can be easily implemented into an Action without inheritance of a particular super Action class.

  • LookupDispatchAction :
    The subclass mapped execute() method is dispatched by this abstract Action class. This process is useful where a HTML form has many clicks on submit buttons with the same name. The corresponding ActionMapping’s parameter property specifies the button name.

  • Dispatch Action :
    It dispatches to a public method that is named by the request parameter whose name is specified by the parameter property of the corresponding ActionMapping. This Action is useful when many similar actions need to be combined into a single Action class design simplification.

  • Forward Action and IncludeAction :
    These forward and include the context-relative URI specified by the parameter property of our associated ActionMapping.

Learn about Framework from our Experts

What is Action Class?

This class is extended by the org.apache.struts.action.Action class. The business logic is wrapped by the Action class and this class provides an interface to the Model of the application. Action class can be viewed as glue between the View and the Model layer of MVC architecture. The data transfer from the view layers to the specific business process layer(View to Mode) is done by this class. The processed data from the business layer to view layer is returned ultimately.

The struts controller i.e., ActionServlet , chooses an appropriate Action and the instance is created if necessary and invokes execute() method.

What is ActionForm?

ActionForm is a Java bean that associates one or more ActionMappings. The bean is initialized from the corresponding parameters of requests well before the Action.execute method is invoked.

The bean’s validate() method will be called, before the execution of execute() method. This process is done before the bean properties have been populated. The verification of properties submitted by the user input form is validated by the validate() method. If the method finds problems, an error messages object those are encapsulated the problems will be returned and the controller servlet will return the control back to the input form. If not , the validate() method returns null, which is the indication of accepting everything, and the Action.execute method should be invoked.

Difference between Action and DispatchAction classes

Grouping related actions into one class is possible using DispatchAction class. But in Action class a group related action into one class is not possible.

  • Several changes need to be done, when one plans to group the related action using Action class.

  • Action class is used to perform single functionality. To perform multiple functionalities, we need to write an additional Action class.

  • The complexity rises if Action class is used for multiple functionalities, whereas by using DispatchAction, the code complexity will noticeably be reduced.

  • An Action class acts as a controller and is an adapter class between the web tier and the business tier.

  • Dispatch class is used to combine the related operations, thus share the common resources, helper classes for instance.

  • An Action is invoked by the ActionServlet to perform an operation that is depending on the URL request.

  • A DispatchAction selects a method depending on the value of the request parameter that is configured in the xml file.

What is switch action & forwardAction in struts?

A SwitchAction is like an Action which switches between modules and forwards the control to the URI that is specified in the new module.

There are two parameters namely page and prefix. The page is the parameter to which the control is forwarded after switching. The prefix is the parameter that specifies the module to which the control is switched.


A ForwardAction is used to forward a request to the specified URI. ForwardAction is represented as a destination to the controller is sent soon after the action is completed. JSP page specification in the code can be replaced by declaratively associate as an action forward through the application. The action forward is specified in the configuration file between and tags.

What is LookupDispatch Action?

LookupDispatch is similar to ActionDispatch. It distinguishes the ActionDispatch that uses the request parameter’s values which performs a reverse lookup from resource bundle which uses the value of parameters and match it to a method in the class.

Describe the main difference between LookupDispachAction and DispatchAction classes.

The method to execute on a DispatchAction is determined by Struts based on the value of a specified request parameter.

The method to execute on a LookupDispachAction is determined by an Action itself based on the value of a specified request parameter.

Explain the difference between ForwardAction and IncludeAction

Forwarding to a specified URL is provided by the ForwardAction. The mechanism to include the specified URL contents is proveided by the IncludeAction.

ForwardAction is used to forward to a control from one page to another page / action. IncludeAction is used to include an action in another action.

ForwardAction is similar to <jsp:forward> and include action is similar to <jsp:include> tags of a JSP page.

Difference between Html tags and Struts specific HTML Tags.

HTML tags are static and Struts tags are dynamic.

  • Struts tags can be created by the page authors.

  • Binding of the property value with the Formbean property is done by the Struts tags.

  • HTML tags support the templates and themes.

  • Struts have HTML tag libraries and thus HTML tags are integral part of Struts.

Most of the tags in the Struts framework use the following attributes:

  • id : the name of a bean for temporary use by the tag

  • name : the name of a pre-existing bean for use with the tag

  • property : the property of the bean named in the name attribute for use with the tag

  • scope : the scope to search for the bean named in the name attribute

What are the contents of web.xml and struts-config.xml?

The web.xml file contains the information that is used by servlets and JSPs. The file includes the description of the application, information about servlets, mapping information for servlets and URL, JSP configuration information, error page information that is displayed without handling exceptions, mapping information about servlets.

The struts-config.xml file contains information about struts framework at the time of deploying the web application. This file includes information about form bean, action mappings, controller information that includes buffer size, forward pattern, content type, locale, class name, content forward etc.

Web.xml example:

Differences between web.xml and struts-config.xml

web.xml is utilized to make connections between web application and the web container, where as the struts-config.xml is utilized to establish the connections between the controller and the view of MVC architecture.

At the time of starting the container, the web.xml will be read by the container, whereas the struts-config.xml will be read by the ActionServlet’s init() method.

    web.xml

  • It is used for the deployment descriptor for web applications

  • web.xml is used for making connection between web container & web application

  • It is read by container when we start the container.

    struts-config.xml

  • It is used for deployment descripror for struts application.

  • It is used for making connection between view & controller

  • It is read by init() method of ActionServlet.