A to Z View - All Upwork Test Answers

Struts - Interview questions and answers

In this post, you will find all the possible and most recent Job Interview questions and answers of Struts for JAVA category which you might be asked by your employer. These interview questions and answers will help you to get the job.

These Interview Questions and answers are not only for Upwork job, you will have some idea about the related job interview for your permanent or real life job. This is just some sample answers, don't just copy and paste them, read them carefully and make your own answer.

Apart from the fact that both Interceptors and filters are based on intercepting
filter,there are few differences when it comes to Struts2.

Filters: (1)Based on Servlet Specification (2)Executes on the pattern matches on
the request.(3) Not configurable method calls

Interceptors: (1)Based on Struts2. (2)Executes for all the request qualifies for
a front controller( A Servlet filter ).And can be configured to execute
additional interceptor for a particular action execution.(3)Methods in the
Interceptors can be configured whether to execute or not by means of
excludemethods or includeMethods.

2. Describe Validate() and reset() methods?

alidate() Method: this method is used to validate the properties after they
are explored by the application.

4. Does the order in which interceptors execute matters ? If yes then why
?

Well, the answer is yes and no.Some Interceptors are designed to be
independent so the order doesn't matter,but some interceptors are totally
dependent on the previous interceptors execution.For example the validation and
workflow interceptors,the validation interceptors checks if there is any error
in the form being submitted to the action, then comes the workflow interceptor
who checks if validation ( occured in the last) has any error,in presence of
error it will not let the rest of the interceptors ( if any ) in the stack to
execute.So this is very important that the validation interceptors execute first
before the workflow. On the other hand lets say you wrote an interceptors who is
doing the authentication and you have the user credential provided ( by the time
this executes) it doesn't matter where this interceptors is placed( It is a
different fact that you would like to keep it in the top ).

6. How does Interceptors help achieve Struts2 a better framework than
Struts1 ?

> Most of the trivial work are made easier to achieve for example automatic
form population.

> Intelligent configuration and defaults for example you can have struts.xml or
annotation based configuration and out of box interceptors can provide
facilities that a common web application needs

>Now Struts2 can be used anywhere in desktop applications also, with minimal or
no change of existing web application,since actions are now POJO.POJO actions
are even easier to unit test.Thanks to interceptors

>Easier UI and validation in form of themes and well known DOJO framework.

>Highly plugable,Integrate other technologies like Spring,Hibernate etc at ease.

> Ready for next generation RESTFUL services

7. How exceptions are handled in Struts application?

Exceptions are handled in struts by using any one of the following two ways:

• Programmatically handling: In this exception are handled by using try and
catch block in program. Using this programmer can define how to handle the
situation when exception arises.

• Declarative handling: In this exception handling is done by using the XML
file. Programmer defines the exception handling logic in the XML file. There are
two ways of defining the exception handling logic in the XML file:

-Global Action Specific Exception Handler Definition.

-Local Action Specific Exception Handler Definition.

8. How to combine the Struts with Velocity Template?

We can combine Struts and Velocity template by performing following steps:

1. Set classpath to Velocity JARs

2. Make web.xml file to identify the Velocity servlet.

3. Select Velocity toolbox.xml in WEB-INF directory.

4. Modify struts-config to point its views to Velocity templates instead of JSPs.

5. Create a Velocity template for each page you want to render.

9. How to display validation errors on JSP page?

Validation error: Validation error are those error which arises when user or
client enters the invalid format data into the form. For this validation of data
struts enables the programmer with the Validator() method which validates both
the data from client side and the server side.

We can display all error in the JSP page by using the following syntax in the
code.

SYNTAX: <html:error/>

10. How to use DispatchAction?

We can use the Dispatch Action we executing following steps:

• Create a class that extends DispatchAction.

• In a new class, add a method: method has the same signature as the execute()
method of an Action class.

• Do not override execute() method.

• Add an entry to struts-config.xml

11. How to use forward action to restrict a strut application to MVC?

We can use the ForwarAction to restrict the Struts application to Model View
Controller by following coding:

<global-forwards>

<statements>

path="/gotoCitizenDetails.do" />

</global-forwards>

<action-mappings>

<statements>

<action path=”/gotoCitizenDetails”

parameter=”/CitizenDetails.jsp”

type=”org.apache.struts.actions.ForwardAction” />

</action-mappings>

12. In how many ways duplicate form submission can occurs?

The submission form can be duplicated by the any of the following ways:

• Using refresh button.

• By clicking submit button more than once before the server sent back the
response.

• By clicking back navigation button present in browser.

• The browser is restores to submit the form again.

• By clicking multiple times on a transaction that is delayed than usual.

13. In Struts1, the front-controller was a Servlet but in Struts2, it is a
filter. What is the possible reason to change it to a filter ?

There are two possibilities why filter is designated as front controller in
Strtus2

(1)Servlet made as front controller needs developer to provide a right value in
<load-on-startup> which lets the framework to initialize many important aspects(
viz. struts configuration file)as the container starts.In absense of which the
framework gets initialized only as the first request hits.Struts2 makes our life
easy by providing front-controller as a filter,and by nature the filters in
web.xml gets initialized automatically as the container starts.There is no need
of such load-on-startup tag.

(2).The second but important one is , the introduction of Interceptors in
Struts2 framework.It not just reduce our coding effort,but helps us write any
code which we would have used filters for coding and necessary change in the
web.xml as opposed to Struts1.So now any code that fits better in Filter can now
moved to interceptors( which is more controllable than filters), all
configuration can be controlled in struts.xml file, no need to touch the web.xml
file.

(3).The front controller being a filter also helps towards the new feature of
Struts ie UI Themes. All the static resources in the Themes now served through
the filter

14. What are action errors?

Action error: 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.

15. 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:

o Name control

o 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.

16. What are Pull-MVC and push-MVC based architecture ? Whicharchitecture
does Struts2 follow ?

Pull-MVC and Push-MVC are better understood with how the view layer is
getting data i.e. Model to render. In case of Push-MVC the data( Model) is
constructed and given to the view layer by the Controllers by putting it in the
scoped variables like request or session. Typical example is Spring MVC and
Struts1. Pull-MVC on the other hand puts the model data typically constructed in
Controllers are kept in a common place i.e. in actions, which then gets rendered
by view layer. Struts2 is a Pull-MVC based architecture, in which all data is
stored in Value Stack and retrieved by view layer for rendering.

17. What are the classes used in Struts?

Struts Framework consists of following classes:

• Action Servlets: used to control the response for each incoming request.

• Action Class: used to handle the request.

• Action Form: it is java bean, used to referred to forms and associated with
action mapping

• Action Mapping: used for mapping between object and action.

• Action Forward: used to forward the result from controller to destination.

18. 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.

19. What are the loop holes of Struts?

The drawbacks of Struts are following:

• Absence of backward flow mechanism.

• Only one single controller Servlets is used.

• Bigger learning curve

• Worst documentation

• No exception present in this framework

• Less transparent

• Rigid approach.

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

• Non-XML compliance of JSP syntax

20. 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

21. What are the various Struts tag libraries?

The various Struts tag libraries are:

• HTML Tags: used to create the struts input forms and GUI of web page.

• Bean Tags: used to access bean and their properties.

• Logic Tags: used to perform the logical operation like comparison

• Template Tags: used to changes the layout and view.

• Nested Tags: used to perform the nested functionality

• Tiles Tags: used to manages the tiles of the application

22. What design patterns are used in Struts?

There are following types of design patterns are used in Struts:

• Service to Worker

• Dispatcher View

• Composite View (Struts Tiles)

• Front Controller

• View Helper

• Synchronizer Token

23. What is Action Class?

An Action class in the struts application is used to handle the request.

• It acts as interface or communication medium between the HTTP request coming
to it and business logic used to develop the application.

• Action class consists of RequestProcessor which act as controller. This
controller will choose the best action for each incoming request, generate the
instance of that action and execute that action.

• This should be in thread-safe manner, because RequestProcessor uses the same
instance for no. of requests at same time.

24. What is ActionMapping?

In action mapping is the mapping of the action performed by the user or
client on the application.

-We specify the action class for a specific user’s action. Like we provide the
path or URL and different view based on user event.

-We can also define where control of the page deviate in case of validation
error in the form.

-We can include ActionMapping in code like this:

<action-mappings>

<action path="/a" type=myclasse.A name="myForm">

<forward name="Login" path="/login.jsp"/>

<forward name="error" path="/error.jsp"/>

</action-mappings>

25. 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.

26. What is DispatchAction?

The DispatchAction enable the programmer to combine together related
function or class.

• Using Dispatch Action programmer can combine the user related action into a
single UserAction. like add user, delete user and update user

• DispatchAction execute the action based on the parameter value it receives
from the user.

27. What is execute and wait interceptor ?

The ExecuteAndWaitInterceptor is great interceptor provided out of box in
Struts2 for running long-lived actions in the background while showing the user
a nice progress meter or a progress bar. For example while uploading a large
file to the server we can use this interceptor to display a nice running
progress bar instead of leaving the user in confusion that the application is
not responding.This also prevents the HTTP request from timing out when the
action takes more than 5 or 10 minutes

28. What is IncludeAction?

The IncludeAction is used to integrate the one action file in another action
file.

• It is same as ForwardAction but the only difference is that the resource is
present in HTTP response.

• Is used to combine the Struts functionality and control into an application
that uses Servlets.

• Use the IncludeAction class to include another resource in the response to the
request being processed.

29. What is MVC?

Model View Controller (MVC) is a design pattern used to perform changes in
the application.

• Model: Model is referring to business or database. It stores the state of the
application. Model has no knowledge of the View and Controller components.

• View: View is referring to the Page Design Code. It is responsible for the
showing the result of the user’s query. View modifies itself when any changes in
the model happen.

• Controller: Controller is referring to navigational code. Controller will
chose the best action for each incoming request, generate the instance of that
action and execute that action.

30. What is role of Action Class?

An Action class in the struts application is used to handle the request.

• It acts as interface or communication medium between the HTTP request coming
to it and business logic used to develop the application.

• Action class consists of RequestProcessor which act as controller. This
controller will choose the best action for each incoming request, generate the
instance of that action and execute that action.

• This should be in thread-safe manner, because RequestProcessor uses the same
instance for no. of requests at same time.

31. What is Struts Validator Framework?

Struts Validator Framework enables us to validate the data of both client
side and server side.

• When some data validation is not present in the Validator framework, then
programmer can generate own validation logic, this User Defined Validation logic
can be bind with Validation Framework.

• Validation Framework consist of two XML configuration Files:

o Validator-Rules.xml file

o Validation.xml file

32. What is struts.devMode and why it is used ?

struts.devMode is a key used in struts.properties file (Can also be
configured in struts.xml file as <constant name="struts.devMode" value="true"
/>) , to represent whether the framework is running in development mode or
production mode by setting true or false. If set to development mode, it gives
the following benefits : -

> Resource bundle reload on every request; i.e. all localization properties file
can be modified and the change will be reflected without restarting the server.

> struts.xml or any configuration files can be modified without restarting or
redeploying the application

> The error occurs in the application will be reported, as oppose to production
mode.

Also remember that struts.devMode should be marked as false in production
environment to reduce impact of performance. By default it is "false".

33. 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.

• Struts takes the help of Model View Controller (MVC) architecture. Where Model
is referring to business or database, View is referring to the Page Design Code,
and Controller is referring to navigational code.

35. What is the difference between Action and ActionSupport ?

Action is interface defines some string like SUCCESS,ERROR etc and an
execute() method. For convenience Developer implement this interface to have
access to String field in action methods. ActionSupport on other hand implements
Action and some other interfaces and provides some feature like data validation
and localized error messaging when extended in the action classes by developers.

36. 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.

37. 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.

38. What is the life cycle of ActionForm?

The lifecycle of ActionForm is as follows:

• Retrieve or Create Form Bean associated with Action

• "Store" FormBean in appropriate scope (request or session)

• Reset the properties of the FormBean

• Populate the properties of the FormBean

• Validate the properties of the FormBean

• Pass FormBean to Action

39. What is the need of Struts?

We need Struts in Java because of following reasons:

• Helps in creation and maintenance of the application.

• Make use of Model View Controller (MVC) architecture. Where Model is referring
to business or database, View is referring to the Page Design Code, and
Controller is referring to navigational code.

• Enables developer to make use of Jakarta Packages, Java Servlets, JavaBeans,
ResourceBundles, and XML

40. What is the role of Action/ Model ?

Actions in Struts are POJO , is also considered as a Model. The role of
Action are to execute business logic or delegate call to business logic by the
means of action methods which is mapped to request and contains business data to
be used by the view layer by means of setters and getters inside the Action
class and finally helps the framework decide which result to render

41. What is the use of ForwardAction?

The ForwardAction is used when we want to combine Struts with existing
application.

• Used when we want to transfer the control form JSP to local server.

• Used to integrate with struts in order to take benefit of struts
functionality, without writing the Servlets again.

In Struts2 FilterServlet is the responsible class for loading struts.xml
file as we deploy the application on the container.Unlike Servlet (as with
Struts1) needs the load-on-startup tag to load the front controller,a filter
doesn't need to have load on startup tag to be loaded as the application is
deployed. As with servlet specification a filter is loaded/executed as the
application starts up.