Struts Interview Questions

Struts Interview Questions
  1. How do traditional web applications work?
  2. What is MVC Pattern?
  3. How does Struts (Struts 1.x) handle requests? Can you explain with an example?
  4. How does Struts 2 handle requests? Can you explain with example?
  5. How is Struts 2 different from Struts 1?
  6. Can you explain more about Interceptors in Struts 2?
  7. Can you provide an example configuration of an interceptor in Struts 2?
  8. How do you create a common set of Interceptors which apply to every request in Struts 2?
  9. Can you give couple of examples of common interceptors in Spring 2?
  10. How do you configure global exception handling in Struts 2?
  11. What is ValueStack?
  12. What is OGNL?
  13. What is struts-default package?
  14. How do you prevent duplicate form submissions with Struts 2?
  15. How do you design pages having long execution time with Struts 2?
  16. How do you implement Authentication with Struts 2?
  17. How do you use view technologies like Velocity or Freemarker with Struts 2?

How do traditional web applications work?

Struts Interview Questions - Web Request Flow

Traditional web applications are based on HTTP Request and HTTP Response cycle. Following are the steps:

  • When user initiates an action in the browser, A HTTP Request is created by the browser.
  • Web Server creates a HTTPServletRequest based on the content of HTTP Request.
  • The web application (based on the framework used) handles the HTTPServletRequest. (Controllers, Business Layer, database calls and external interfaces)
  • A HTTPServletResponse is returned. This is converted to HTTP Response.
  • The HTTP Response is rendered by the browser.

What is MVC Pattern?

MVC stands for Model, View and Controller. It is a software architectural pattern for implementing user interfaces.

  • Controller : Controls the flow. Sends commands to the model to update the model's state. Sends commands to view to change the view's presentation of the model.
  • Model : Represents the state of the application. Sometimes - notifies associated views and controllers when there is a change in its state.
  • View : Visual representation of the model for the user.

How does Struts (Struts 1.x) handle requests? Can you explain with an example?

Struts Request Flow - Interview Questions
  • User clicks on a link in an HTML page.
  • Servlet controller (ActionServlet) receives the request (Front Controller pattern) , forwards it to RequestProcessor.
  • RequestProcessor looks up mapping information in struts-config.xml, and routes to an action.
  • Action makes calls to populate the Model.
  • Action forwards to a View resource (JSP page).
  • RequestProcessor looks up the mapping for the requested resource and forwards to the appropriate JSP page.
  • JSP file is invoked and sent to the browser as HTML.
  • User is presented with a new HTML page in a web browser.

How does Struts 2 handle requests? Can you explain with example?

  • User sends a request for the action
  • Initial request goes to the Servlet container (such as Tomcat or Websphere or Jetty)
  • Container looks up web.xml file and finds the Front Controller.
  • Container invokes the Front controller. In the beginning of Struts 2, FilterDispatcher was used as Front Controller. Since struts 2.1, it is recommended to use StrutsPrepareAndExecuteFilter.
  • Controller interacts with ActionMapper and invokes the ActionProxy ( which uses Configuration Manager to read content from struts.xml)
  • ActionProxy forwards the request to the ActionInvocation
  • ActionInvocation invokes all the interceptors in the chain
  • Result is sent back to the ActionInvocation and a HttpServletResponse is generated
  • Response is sent to the user

How is Struts 2 different from Struts 1?

  • Request flow in Struts 2 is completely different from Struts 1.x. Refer above questions.
  • Interceptors: Fundamental change in Struts 2 is the focus on Interceptors. Interceptor intercepts the requests, and provides some additional processing before and after the execution of action and result. Common functionalities required by action classes are implemented as Interceptors. This makes the action class lightweight. The interceptor approach helps in modularizing common code into reusable classes.
  • Testable : Struts 2 Actions are framework independent. They can be tested easily without using a lot of mock objects.
  • Convention over Configurations: Most of the configuration elements in the Struts 2 configuration file will have default values, so there is no need to set values unless a different value is required.
  • POJO Actions & Forms: Action classes are plain old Java objects (POJO). Any Java class with an execute() method can be used as an Action class.
  • Ajax support: Struts 2 provides inbuild support for Ajax.
  • Annotations: Struts 2 provides annotations as an alternative to XML configuration.
  • Struts 2 integration with Spring is seemless.
  • POJO Actions & Forms: Action classes are plain old Java objects (POJO). Any Java class with an execute() method can be used as an Action class.

Can you explain more about Interceptors in Struts 2?

  • Interceptor intercepts the requests, and provides some additional processing before and after the execution of action and result.
  • Common functionalities required by action classes are implemented as Interceptors. This makes the action class lightweight. The interceptor approach helps in modularizing common code into reusable classes.
  • The framework provides a default set of Interceptors; we can also write our own custom Interceptor classes.
  • The Interceptor or a stack of Interceptors is configured for an action, which is executed before and after the action is executed, to provide all pre-processing and post-processing of request.
  • Every Interceptor is pluggable and the required interceptor or Interceptor Stack can be configured on a per-action basis.

Can you provide an example configuration of an interceptor in Struts 2?

In the below example, interceptor1 and interceptor2 are configured for the action login.

<interceptors>
     <interceptor name=“interceptor1” class=“Interceptor1_class_name”/>
     <interceptor name=“interceptor2” class=“Interceptor2_class_name”/>
</interceptors> 
<action name="login” class=“LoginAction”>
     <interceptor-ref name=“interceptor1”/>
     <interceptor-ref name=“interceptor2”/>
     <result name=“input”>login.jsp</result>
     <result name=“success”>success.jsp</result>
</action>

How do you create a common set of Interceptors which apply to every request in Struts 2?

Refer to example below. defaultStack is now a common set of interceptors. It can be used on all the actions as needed.

<interceptors>
     <interceptor-stack name=”defaultStack”>
          <interceptor-ref name="exception"/>
          <interceptor-ref name="servlet-config"/>
          <interceptor-ref name="prepare"/>
          <interceptor-ref name="checkbox"/>
          <interceptor-ref name="params"/>
          <interceptor-ref name="conversionError"/>
     </interceptor-stack>
</interceptors>

We can configure the defaultStack as the default for all actions (as shown below)

<default-interceptor-ref name=" defaultStack "/>
Other option is to configure it for every action (NOT preferred)
<action name=”login” class=”LoginAction”>
     <interceptor-ref name=”defaultStack”/>
</action>

Can you give couple of examples of common interceptors in Spring 2?

  • Create Session Interceptor - Used to create an Hypertext Transfer Protocol (HTTP) Session
  • Exception Interceptor - Struts framework provides the functionality of exception handling through Exception Interceptor.
  • Other important ones are debugging, fileUpload, i18n, logger, token (prevent duplicate submits) and validation.

How do you configure global exception handling in Struts 2?

Instead of writing try catch in every method to handle exceptions, Struts 2 provides a global exception handling mechanism. If the exception is not caught by any of the implemented methods, the global exception handling catches the exception and shows the error page. This is also called Declarative error handling. Example is shown below.

     <global-exception-mapping>
          <exception-mapping exception=”java.lang.exception” result=”exception”/>
     </global-exception-mapping>
     <global-results>

          <result name="exception">/error.jsp</result> 
     </global-results>

What are the different view technologies supported by Struts 2?

JavaServer Pages (including JSTL and JSF), Velocity templates, PDF, XSLT and FreeMarker.

What is ValueStack?

The ValueStack is a storage area holding all data associated with processing of a request. The ValueStack is per-request. Any value placed on the stack during request processing is accessible later in the processing of same request. Data is placed on the Value Stack by various interceptors and this is available while rendering the view. OGNL is the preferred way of accessing values on the ValueStack.

What is OGNL?

The Object-Graph Navigation Language (OGNL) is a powerful expression language that is used to reference and manipulate data on the ValueStack. OGNL also helps in data transfer and type conversion.

Examples

  • list[0].name instead of ((User) list.get(0) ).getName()
  • map['foo'] or map.foo instead of map.get("foo")
  • map.isEmpty instead of map.isEmpty()

What is struts-default package?

Struts (like Spring) adopted the philosophy of Convention over Configuration with Struts 2. Most of the defaults are defined in a built-in package called struts-default. (these are defined in struts–default.xml).

How do you prevent duplicate form submissions with Struts 2?

When interacting with a slow application, there is every chance that a user clicks submit button twice or tries to repeat the same action by refreshing the page. This results in a duplicate form submission. With Struts 2, this can be prevented using the token and token-session interceptors.

A token is associated with every request. When a duplicate form submission happens, the same token is repeated twice. We can either show an error page or handle it gracefully showing the result page (without processing the request).

How do you design pages having long execution time with Struts 2?

All applications have specific requests taking a long execution time. Nothing irritates a user more than having no feedback for a long time. Struts 2 provides an inbuilt interceptor execAndWait to handle this situation. This interceptor helps in providing user an intermediate feedback while the request completes execution.

How do you implement Authentication with Struts 2?

Authentication can be implemented by creating an Authentication Interceptor. We can assign this interceptor to be executed with every request.

<interceptors>
     <interceptor name="authenticationInterceptor" class="com.java.interview.AuthenticationInterceptor"/> 
     <interceptor-stack name="authenticatedStack"> 
          <interceptor-ref name="authenticationInterceptor"/> 
          <interceptor-ref name="defaultStack"/> 
     </interceptor-stack>
</interceptors>

<default-interceptor-ref name=" authenticatedStack "/> 

How do you use view technologies like Velocity or Freemarker with Struts 2?

By configuring appropriate result-type as shown in the example below. Its an option to configure default attribute to true. Then, this result type would be come the default when no type is specified on a result.

<result-types>

     <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult" default="true"/> 
</result-types> 

Example below shows how freemarker can be configured as default result type.

  <result-types>
  <result-type name="freemarker"
   class="org.apache.struts2.views.freemarker.FreemarkerResult"
   default="true"/>
  </result-types>
 

If you loved these Questions, you will love our PDF Interview Guide with 400+ Questions.
Download it now!.

400+ Interview Questions in 4 Categories:
  1. Java : Core Java, Advanced Java, Generics, Exception Handling, Serialization, Threads, Synchronization, Java New Features
  2. Frameworks : Spring, Spring MVC, Struts, Hibernate
  3. Design : Design, Design Patterns, Code Review
  4. Architecture : Architecture, Performance & Load Testing, Web Services, REST Web Services,Security, Continuous Integration