Here we have mentioned most frequently asked Strut Interview Questions and Answers specially for freshers and experienced.


 

1. What are the components of Struts Framework?

Ans:

Struts framework is comprised of following components:
Java Servlets
JSP (Java Server Pages)
Custom Tags
Message Resources

2. What’s the role of a handler in MVC based applications?

Ans:

It’s the job of handlers to transfer the requests to appropriate models as they are bound to the model layer of MVC architecture. Handlers use mapping information from configuration files for request transfer.

3. What’s the flow of requests in Struts based applications?

Ans:

Struts based applications use MVC design pattern. The flow of requests is as follows:
User interacts with View by clicking any link or by submitting any form.
Upon user’s interaction, the request is passed towards the controller.
Controller is responsible for passing the request to appropriate action.
Action is responsible for calling a function in Model which has all business logic implemented.
Response from the model layer is received back by the action which then passes it towards the view where user is able to see the response.

4. Which file is used by controller to get mapping information for request routing?

Ans:

Controller uses a configuration file “struts-config.xml file to get all mapping information to decide which action to use for routing of user’s request.

5. What’s the role of Action Class in Struts?

Ans:

In Struts, Action Class acts as a controller and performs following key tasks:
After receiving user request, it processes the user’s request.
Uses appropriate model and pulls data from model (if required).
Selects proper view to show the response to the user.

6. How an actionForm bean is created?

Surrogate

Ans:

actionForm bean is created by extending the class org.apache.struts.action.ActionForm
In the following example we have created an actionForm bean with the name ‘testForm’:

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class testForm extends ActionForm {
private String Id=null;
private String State=null;
public void setId(String id){
this.Id=id;
}
public String getId(){
return this.Id;
}
public void setState(String state){
this.State=state;
}
public String getState(){
return this.State;
}

7. What are the two types of validations supported by Validator FrameWork?

Ans:

Validator Framework is used for form data validation. This framework provides two types of validations:
Client Side validation on user’s browser
Server side validation

8. What are the steps of Struts Installation?

Ans:

In order to use Struts framework, we only need to add Struts.Jar file in our development environment. Once jar file is available in the CLASSPATH, we can use the framework and develop Strut based applications.

9. How client side validation is enabled on a JSP form?

Ans:

In order to enable client side validation in Struts, first we need to enable validator plug-in in struts-config.xml file. This is done by adding following configuration entries in this file:


<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>

Then Validation rules are defined in validation.xml file. If a form contains email field and we want to enable client side validation for this field, following code is added in validation.xml file:

depends=”required”>


10. How action-mapping tag is used for request forwarding in Struts configuration file?

Ans:

In Struts configuration file (struts-config.xml), forwarding options are defined under action-mapping tag.
In the following example, when a user will click on the hyperlink test.do, request will be forwarded to /pages/testing.jsp using following configurations from struts-config.xml file.



 

11. How duplicate form submission can be controlled in Struts?

Ans:

In Struts, action class provides two important methods which can be used to avoid duplicate form submissions.
saveToken() method of action class generates a unique token and saves it in the user’s session. isTokenValid() method is used then used to check uniqueness of tokens.

12. In Struts, how can we access Java beans and their properties?

Ans:

Bean Tag Library is a Struts library which can be used for accessing Java beans.

13. Which configuration file is used for storing JSP configuration information in Struts?

Ans:

For JSP configuration details, Web.xml file is used.

14. What’s the purpose of Execute method of action class?

Ans:

Execute method of action class is responsible for execution of business logic. If any processing is required on the user’s request, it’s performed in this method. This method returns actionForward object which routes the application to appropriate page.
In the following example, execute method will return an object of actionForward defined in struts-config.xml with the name “exampleAction”:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class actionExample extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
return mapping.findForward("exampleAction");
}
}

15. What’s the difference between validation.xml and validator-rules.xml files in Struts Validation framework?

Ans:

In Validation.xml, we define validation rules for any specific Java bean while in validator-rules.xml file, standard and generic validation rules are defined.

16. How can we display all validation errors to user on JSP page?

Ans:

To display all validation errors based on the validation rules defined in validation.xml file, we use tag in our JSP file.

17. What’s declarative exception handling in Struts?

Ans:

When logic for exception handling is defined in struts-config.xml or within the action tag, it’s known as declarative exception handling in Struts.

18. What’s DynaActionForm?

Ans:

DynaActionForm is a special type of actionForm class (sub-class of ActionForm Class) that’s used for dynamically creating form beans. It uses configuration files for form bean creation.

19. What configuration changes are required to use Tiles in Struts?

Ans:

To create reusable components with Tiles framework, we need to add following plugin definition code in struts-config.xml file.

20. What’s the difference between Jakarta Struts and Apache Struts? Which one is better to use?

Ans:

Both are same and there is no difference between them.




 

21. What’s the use of Struts.xml configuration file?

Ans:

Struts.xml file is one the key configuration files of Struts framework which is used to define mapping between URL and action. When a user’s request is received by the controller, controller uses mapping information from this file to select appropriate action class.

22. How tag libraries are defined in Struts?

Ans:

Tag libraries are defined in the configuration file (web.xml) inside tag.

23. What’s the significance of logic tags in Struts?

Ans:

Use of logic tags in Struts helps in writing a clean and efficient code at presentation layer without use of scriptlets.

24. What are the two scope types for formbeans?

Ans:

1. Request Scope: Formbean values are available in the current request only
2. Session Scope: Formbean values are available for all requests in the current session.

25. How can we group related actions in one group in Struts?

Ans:

To group multiple related actions in one group, we can use DispatcherAction class.

26. When should we use SwtichAction?

Ans:

The best scenario to use SwitchAction class is when we have a modular application with multiple modules working separately. Using SwitchAction class we can switch from a resource in one module to another resource in some different module of the application.

27. What are the benefits of Struts framework?

Ans:

Struts is based on MVC and hence there is a good separation of different layers in Struts which makes Struts applications development and customization easy. Use of different configuration files makes Struts applications easily configurable. Also, Struts is open source and hence, cost effective.

28. What steps are required to for an application migration from Struts1 to Struts2?

Ans:

Following Steps are required for Struts1 to Struts2 migration:
Move Struts1 actionForm to Struts2 POJO.
Convert Struts1 configuration file (struts-config.xml) to Struts2 configuration file (struts.xml)

29. How properties of a form are validated in Struts?

Ans:

For validation of populated properties, validate() method of ActionForm class is used before handling the control of formbean to Action class.

30. What’s the use of reset method of ActionForm class?

Ans:

reset method of actionForm class is used to clear the values of a form before initiation of a new request.


 

31. What are disadvantages of Struts?

Ans:

Although Struts have large number of advantages associated, it also requires bigger learning curve and also reduces transparency in the development process.
Struts also lack proper documentation and for many of its components, users are unable to get proper online resources for help.

32. What’s the use of resourcebundle.properties file in Struts Validation framework?

Ans:

resourcebundle.properties file is used to define specific error messages in key value pairs for any possible errors that may occur in the code.
This approach helps to keep the code clean as developer doesn’t need to embed all error messages inside code.

33. Can I have html form property without associated getter and setter formbean methods?

Ans:

For each html form property, getter and setter methods in the formbean must be defined otherwise application results in an error.

34. How many servlet controllers are used in a Struts Application?

Ans:

Struts framework works on the concept of centralized control approach and the whole application is controlled by a single servlet controller. Hence, we require only one servlet controller in a servlet application.

35. For a single Struts application, can we have multiple struts-config.xml files?

Ans:

We can have any number of Struts-config.xml files for a single application.

36. Which model components are supported by Struts?

Ans:

Struts support all types of models including Java beans, EJB, CORBA. However, Struts doesn’t have any in-built support for any specific model and it’s the developer’s choice to opt for any model.

37. When it’s useful to use IncludeAction?

Ans:

IncludeAction is action class provided by Struts which is useful when an integration is required between Struts and Servlet based application.

38. Is Struts thread safe?

Ans:

Yes Struts are thread safe. In Struts, a new servlet object is not required to handle each request; rather a new thread of action class object is used for each new request.

39. What configuration changes are required to use resource files in Struts?

Ans:

Resource files (.properties files) can be used in Struts by adding configuration entry in struts-config.xml file.

 

40. How nested beans can be used in Struts applications?

Ans:

Struts provide a separate tag library (Nested Tag Library) for this purpose. Using this library, we can nest the beans in any Struts based application.



 

41. What are the Core classes of Struts Framework?

Ans:

Following are the core classes provided by Struts Framework:
Action Class
ActionForm Class
ActionMapping Class
ActionForward Class
ActionServlet Class

42. Can we handle exceptions in Struts programmatically?

Ans:

Yes we can handle exceptions in Struts programmatically by using try, catch blocks in the code.

try {
// Struts code
}
Catch (Exception e) {
// exception handling code
}

43. Is Struts Framework part of J2EE?

Ans:

Although Struts framework is based on J2EE technologies like JSP, Java Beans, Servlets etc but it’s not a part of J2EE standards.

44. How action mapping is configured in Struts?

Ans:

Action mappings are configured in the configuration file struts-config.xml under the tag as follows:


type="login.loginAction"
name="loginForm"
input="/login.jsp"
scope="request"
validate="true">



45. When should be opt for Struts Framework?

Ans:

Struts should be used when any or some of the following conditions are true:
A highly robust enterprise level application development is required.
A reusable, highly configurable application is required.
A loosely coupled, MVC based application is required with clear segregation of different layers.

46. Why ActionServlet is singleton in Struts?

Ans:

In Struts framework, actionServlet acts as a controller and all the requests made by users are controlled by this controller. ActionServlet is based on singleton design patter as only one object needs to be created for this controller class. Multiple threads are created later for each user request.

47. What are the steps required for setting up validator framework in Struts?

Ans:

Following Steps are required to setup validator framework in Struts: – Wrong Spelling
In WEB-INF directory place valdator-rules.xml and validation.xml files.

48. Which technologies can be used at View Layer in Struts?

Ans:

In Struts, we can use any of the following technologies in view layer:
JSP
HTML
XML/XSLT
WML Files
Velocity Templates
Servlets

49. What are the conditions for actionForm to work correctly?

Ans:

ActionForm must fulfill following conditions to work correctly:
It must have a no argument constructor.
It should have public getter and setter methods for all its properties.

50. Which library is provided by Struts for form elements like check boxes, text boxes etc?

Ans:

Struts provide HTML Tags library which can be used for adding form elements like text fields, text boxes, radio buttons etc.




 

51. What is the need of Struts?

Ans:

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.

52. What are the classes used in Struts?

Ans:

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.

53. How exceptions are handled in Struts application?

Ans:

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.

54. What is MVC?

Ans:

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.

55. Describe Validate() and reset() methods.

Ans:

Validate() Method:
– This method is used to validate the properties after they are explored by the application.
– Validate method is Called before FormBean is handed to Action.
– This method returns a collection of ActionError.
Syntax :

public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

Reset() Method:
– This method is called by the Struts Framework with each request that uses the defined ActionForm.
– Used to reset all the data from the ActionForm.
Syntax :

public void reset() {}

56. What design patterns are used in Struts?

Ans:

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

57. What is the difference between session scope and request scope when saving FormBean?

Ans:

The difference between session scope and request scope when saving FormBean are following:
– In Request Scope, values of FormBean are available to current request but in Session Scope, values of FormBean are available throughout the session.

58. What is the different actions available in Struts?

Ans:

The different kinds of actions in Struts are:
– ForwardAction
– IncludeAction
– DispatchAction
– LookupDispatchAction
– SwitchAction

59. What is DispatchAction?

Ans:

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

60. How to use DispatchAction?

Ans:

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


 

61. What is the difference between ForwardAction and IncludeAction?

Ans:

The difference between ForwardAction and InculdeAction are:
– IncludeAction is used when any other action is going to intake that action whereas ForwardAction is used move the request from one resource to another resource.

62. What is difference between LookupDispatchAction and DispatchAction?

Ans:

The difference between LookupDispatchAction and DispatchAction are given below:
– LookupDispatchAction is the subclass of the DispatchAction.
– Actual method that gets called in LookupDispatchAction whereas DispatchAction dispatches the action based on the parameter value.

63. What is LookupDispatchAction?

Ans:

– The LookupDispatchAction class is a subclass of DispatchAction.
– The LookupDispatchAction is used to call the actual method.
– For using LookupDispatchAction, first we should generate a subclass with a set of methods.
– It control the forwarding of the request to the best resource in its subclass.
– It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.

64. What is the use of ForwardAction?

Ans:

– 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.
– Use to forward a request to another resource in your application.

65. What is IncludeAction?

Ans:

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.

66. What are the various Struts tag libraries?

Ans:

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.

67. What is the life cycle of ActionForm?

Ans:

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.

68. What are the loop holes of Struts?

Ans:

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.

69. 1Difference between Html tags and Struts specific HTML Tags

Ans:

Difference between HTML tag and Struts specific HTLM tags are:
– HTML tags are static in nature but Struts specific HTML tags are dynamic in nature.
– HTML tags are not User Defined whereas Struts tags can be user defined.
– HTML tags provide the different templates and themes to the programmer whereas Struts specific HTML tag provides the integrating the property value with the Formbean properties.
– HTML tags are integral part of Struts whereas Struts have HTML tag libraries.

70. What is the difference between session scope and request scope when saving FormBean?

Ans:

The difference between session scope and request scope when saving FormBean are following:
– In Request Scope, values of FormBean are available to current request but in Session Scope, values of FormBean are available throughout the session.



 

71. How to display validation errors on JSP page?

Ans:

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.

 

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

Ans:

We can use the ForwarAction to restrict the Struts application to Model View Controller.

73. What is ActionMapping?

Ans:

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.

74. What is role of Action Class?

Ans:

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

75. How to combine the Struts with Velocity Template?

Ans:

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.

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

Ans:

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.

77. What is the difference between Struts 1 and struts2?

Ans:

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.

78. What are the steps used to setup dispatch action?

Ans:

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.

79. What is difference between Interceptors and Filters?

Ans:

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.

80. What are the Custom tags?

Ans:

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.




 

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

Ans:

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.

82. What is the difference between RequestAware and ServletRequestAware interface?

Ans:

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.

83. What are inner class and anonymous class?

Ans:

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.

84. What is struts.devMode?

Ans:

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.

85. What are action errors?

Ans:

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.

86. What is request processor and how does it relates to action mapping?

Ans:

The Request Processor in java is responsible for the following in its process method (). There are steps that are required to be processed by using the Request Processor that retrieves appropriate XML block for the URL from struts-config.xml.
1. XML block looked for by the request processor is called Action Mapping. A class called Action Mapping in org.apache.struts.action package is also involved which is responsible for holding mapping between URL and action.

type="City.example. Citizen Action"
name="Citizen Form"
scope="request"
validate="true"
input="Citizen Form.jsp">
path="ThankYou.jsp"
redirect=”true”/>

2. The Request Processor is responsible for looking up the configuration file for the URL pattern “/submits Form”. (i.e. URL path without the suffix do) and finds the XML block shown in the example above. The type attribute tells Struts which Action class has to be instantiated. The XML block also contains several other attributes. Together these constitute the Java Beans properties of the Action Mapping instance for the path /submit Detail Form. The above XML block tells Struts to map the URL request with the path “/submit Form” to the class “mycity.example.Citizen Action”.

87. What are the differences between HTTP direct and HTTP indirect?

Ans:

The difference between HTTP Forward and HTTP Redirect can be stated as follows:
HTTP Forward :
HTTP Forward is the process used for displaying a page when requested by the end user. The user requests for a resource by clicking on a hyperlink or submitting a form and the next page is displayed to the user as a response. Under Servlet container, HTTP Forward is done by invoking the following command:
RequestDispatcher dispatcher = httpServletRequest.getRequestDispatcher(url);
Dispatcher.forward(httpServletRequest, httpServletResponse);
HTTP Redirect
HTTP Redirect is more complex as compared to HTTP direct. In this a user requests a resource and the responses are first sent to the user, which is not the requested resource.
It is a response with HTTP code “302” and contains the URL of the requested resource. This URL can be same or different from original requested URL. The client browser sends the request for the resource again with the new URL, this time, the requested resource is sent to the user. In web tier HTTP redirect can be done by using the simple API, sendRedirect() on the HttpServletResponse instance.

88. What are action errors and error and what are the consequences they impose?

Ans:

Users of web application may submit incorrect data or no data at all. These errors have to be caught as close to the user interface as possible, instead of waiting for the middle tier or the database to tell column cannot be inserted in the database due to expecting a non-null value.
Following are the consequences of such programming practice:
1. Wastage of server time and resources. As the request, this consumes the time and resources fails.
2. Negative impact on code quality as the probability of entering null data has to be checked while using or coding for business logics as they are the toughest codes of the system and contain enough blocks for if-else.

89. What are the sections into which a strut configuration file can be divided?

Ans:

Strut configuration file directly relates to struts-config. The five sections into which strut configuration file is divided are:
1. Form bean definition section: This is the section that is required to provide the definition of the form bean that is used to create the form and allow the user to communicate with the server using it.
2. Global forward definition section: it allows the components to be forwarded as global and provide the server with the information about the global variables.
3. Action mapping definition section: this allows the mapping of various actions that can be used to provide the tool for the user to their help.
4. Controller configuration section: this allows the configuration settings to be controlled and filled by the user. The modifications can be done to change the settings that are to be used for controlling the resources.
5. Application Resources definition section: this allows the resources to be defined that are used in the application.

90. List the important attribute and elements of action mapping under struts.

Ans:

The important attribute and elements of action mapping under struts are as follows :
1. Path : It is the URL path for which the action mapping is used. The path must be unique and can be for either path mapping or suffix mapping.
2. Type : It is the fully qualified class name of the Action.
3. Name : It is the logical name of the Form bean. It helps in deciding which action mappings should use which ActionForms for strut application.
4. Scope : The Scope of the Form bean is either session or request.
5. Validate : Is either true or false. For true, the Form bean is validated on submission, for false, the validation is skipped.
6. Input : When validation error exists the physical page to which the control should be passed is called the input.
7. Forward : When action forward with respect to the name is selected in execute method of action class the control is passed to this page.


 

91. Give an example of validates method used to avoid errors.

Ans:

Struts uses validate() method in the ActionForm to handle user input validations for cases of incorrect or no input.
An example for validate method is given below.
Listing 2.2 validate() method in the CityForm

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
// Perform validator framework validations
ActionErrors errors = super.validate(mapping, request);
// Only need crossfield validations here
if (parent == null)
{
errors.add(GLOBAL_ERROR,
new ActionError(“error.custform”));
}
if (firstName == null)
{
errors.add(“firstName”,
new ActionError(“error.firstName.null”));
}
return errors;
}

92. What is a custom tag?

Ans:

Custom Tags can be described as Java classes written by developers which can be used in the JSP using XML markup. Custom tags can be considered as class which acts as view helper beans that can be put into use without scriptlets which are Java code snippets mixed with JSP markup. JSP pages are developed and tweaked by page authors and cannot interpret the scriptlets. This blurs the separation of different duties to be performed in a project. Custom Tags thus help in solving this problem as they are XML based and like any markup language and can be easily understood by page authors.

93. What is the procedure of operation of a form tag?

Ans:

The FormTag might comprise other tags in its body. Like a submit tag whose example is given below the form tag operates as follows:

The servlet container passes the JSP to display the HTML.

On encountering a form tag a container activates the doStartTag() method which performs the same function as of the Request Processor in the execute() method.
1. The Form Tag in its path attributes checks for an Action Mapping with /submitForm.
2. On finding the action mapping the search for city form is generated under request for session scope.
3. Failing to find the one the form tag creates and puts a new value for the one in the related context. If the value is found it is used which makes form name available.
4. The action tag is accessed by form field tags from page context to retrieve the values from action form attributes in accordance to names.

94. How exceptions are handled in Struts application?

Ans:

This is little tough Struts interview question though looks quite basic not every candidate knows about it. Below is my answer of this interview questions on Struts:
There are two ways of handling exception in Struts:
Programmatically handling: using try {} catch block in code where an exception can come and flow of code is also decided by programmer .its a normal java language concept.
Declarative handling: There are two ways again either we define tag inside struts-config.XML file

<exception
key="stockdataBase.error.invalidCurrencyType"
path="/AvailbleCurrency.jsp"
type="Stock.account.illegalCurrencyTypeException">

The programmatic and Declarative way is sometimes also asked as follow-up questions are given candidate’s response on knowledge on Struts.

Key: The key represents the key present in MessageResource.properties file to describe the exception.
Type: The class of the exception occurred.
Path: The page where the controls are to be followed is case exception occurred.

95. What are the steps involved in creating a strut application?

Ans:

Following is a step by step procedure to create a strut application:
1. Relevant entries are added into web xml.
2. Then the procedure is begun with a blank template to which the following are adder: Declaration for request process and message resource bundle , properties file and its declaration as message resource bundle, the declaring of form bean, action mapping for the same and forwards.
3. After the above step the class which represents the form bean is created.
4. Next the action class is created.
5. Follows the creation of JSP using strut tags.
6. Each < bean: message> tag consisted in JSP is given a value of key pairs to the message resource bundle which was created in step 2.
7. Form bean is given a validation.
8. Error messages are defined within the message resource bundle.
9. Finally creation of the remaining JSP’s takes place.

96. What are the steps used to setup dispatch action?

Ans:

To setup the dispatch action the following steps are used:
1. A subclass for DispatchAction is created.
2. A method is created for for every logical action after identifying the related actions.
3. For all actions a request parameter is identified.
4 An action mapping is defined for the subclass created for the dispatch action to which the identified request parameter is assigned as parameter attribute value.
5. The JSP which identified request parameter in step 3 takes on the subclass defined for dispatch action method names as there values.

97. What are the ways in which duplicate form submission can occur?

Ans:

The submission form can be duplicated by the following ways:
1. Pressing the refresh button.
2. The browser back button can be used for traversing back to submit form page and resubmit form.
3. The history of browser can be accessed to submit the form again.
4. Heavy impact on server for personal gain can be inflicted by malicious submission of form.
5. By clicking multiple times on a transaction that is delayed than usual.

98. What is Struts Validator Framework?

Ans:

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:
• Validator-Rules.xml file
• Validation.xml file

99. What is the need of Struts?

Ans:

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

100. What is IncludeAction?

Ans:

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.




 

101. What is Action Class?

Ans:

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.

102. How exceptions are handled in Struts application?

Ans:

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.

103. What are the classes used in Struts?

Ans:

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.

104. What is MVC?

Ans:

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.

105. What design patterns are used in Struts?

Ans:

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

106. What is the difference between session scope and request scope when saving FormBean?

Ans:

The difference between session scope and request scope when saving FormBean are following:
• In Request Scope, values of FormBean are available to current request but in Session Scope, values of FormBean are available throughout the session.
Describe Validate() and reset() methods.
Validate() Method: this method is used to validate the properties after they are explored by the application.
• Validate method is Called before FormBean is handed to Action.
• This method returns a collection of ActionError.
• Syntax of Validate() Method:
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
Reset() Method: this method is called by the Struts Framework with each request that uses the defined ActionForm.
• Used to reset all the data from the ActionForm
• Syntax of Reset() Method:

public void reset() {}

107. What is the different actions available in Struts?

Ans:

The different kinds of actions in Struts are:
• ForwardAction
• IncludeAction
• DispatchAction
• LookupDispatchAction
• SwitchAction

108. What is DispatchAction?

Ans:

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.

109. How to use DispatchAction?

Ans:

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

110. What is difference between LookupDispatchAction and DispatchAction?

Ans:

The difference between LookupDispatchAction and DispatchAction are given below:
• LookupDispatchAction is the subclass of the DispatchAction
• Actual method that gets called in LookupDispatchAction whereas DispatchAction dispatches the action based on the parameter value.


 

111. What are the various Struts tag libraries?

Ans:

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

112. What is the life cycle of ActionForm?

Ans:

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

113. What is the difference between ForwardAction and IncludeAction?

Ans:

The difference between ForwardAction and InculdeAction are:
• IncludeAction is used when any other action is going to intake that action whereas ForwardAction is used move the request from one resource to another resource.

114. What is the use of ForwardAction?

Ans:

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.
• Use to forward a request to another resource in your application

115. What is LookupDispatchAction?

The LookupDispatchAction class is a subclass of DispatchAction
• The LookupDispatchAction is used to call the actual method.
• For using LookupDispatchAction, first we should generate a subclass with a set of methods.
• It control the forwarding of the request to the best resource in its subclass
• It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.

116. Difference between Html tags and Struts specific HTML Tags

Ans:

Difference between HTML tag and Struts specific HTLM tags are:
• HTML tags are static in nature but Struts specific HTML tags are dynamic in nature.
• HTML tags are not User Defined whereas Struts tags can be user defined.
• HTML tags provide the different templates and themes to the programmer whereas Struts specific HTML tag provides the integrating the property value with the Formbean properties.
• HTML tags are integral part of Struts whereas Struts have HTML tag libraries.

117. What is the difference between session scope and request scope when saving FormBean?

Ans:

The difference between session scope and request scope when saving FormBean are following:
• In Request Scope, values of FormBean are available to current request but in Session Scope, values of FormBean are available throughout the session.

118. What is ActionMapping?

Ans:

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"/ >

119. How to display validation errors on JSP page?

Ans:

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 />

120. What are the loop holes of Struts?

Ans:

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



 

121. What is role of Action Class?

Ans:

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.

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

Ans:

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

< global-forwards >
< statements >
< forward name="CitizenDetailsPage"
path="/gotoCitizenDetails.do" />
< action-mappings >
< statements >
< action path=”/gotoCitizenDetails”
parameter=”/CitizenDetails.jsp”
type=”org.apache.struts.actions.ForwardAction” />

123. How to combine the Struts with Velocity Template?

Ans:

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.

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

Ans:

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.

125. What is Struts? Why you have used struts in your application or project.

Ans:

This is the first interview questions anyone asks in Struts to get the interview rolling. Most commonly asked during the less senior level. Struts are nothing but open source framework mostly used for making web application whenever we use the term framework means it comprises JSP, servlet, custom tags message resources all in one bundle which makes developer task very easy. Its is based on MVC pattern which is model view Controller pattern.

126. What are the main classes which are used in struts application?

Ans:

This is another beginner’s level Struts interview question which is used to check how familiar candidate is with Struts framework and API. Main classes in Struts Framework are:

Action servlet: it’s a backbone of web application it’s a controller class responsible for handling the entire request.
Action class: using Action classes all the business logic is developed us call model of the application also.
Action Form: it’s a java bean which represents our forms and associated with action mapping. And it also maintains the session state its object is automatically populated on the server side with data entered from a form on the client side.
Action Mapping: using this class we do the mapping between object and Action.
ActionForward: this class in Struts is used to forward the result from controller to destination.