Struts Interview Questions and Answers
by Siyamala B, on May 21, 2018 11:45:37 AM
Q1. What is Struts? Why you have used struts in your application or project.?
Ans: 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.
if we go with servlet all HTML code which is related to design part mostly will come inside java code which makes code unmaintainable and complex similarly if use JSP, all java code related to business come inside design part which again make code complex, that’s why MVC pattern come into existence and which separate the business, design and controller and struts were made based on this pattern and easy to develop web application.
Q2. What are the components of Struts.
Ans:
- Action Servlet
- Action Classes
- Action Form
- PlugIn Classes.
1 Validator Framework
2 Tiles
- Message Resources
- Action Mapping Implemention
- Struts Configuration XML Files
- Exception Handler
Q3. What are the main classes which are used in struts application?
Ans: 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.
Q4. What is Action.
Ans: Action is part of the controller. The use of this class is to translate the
HTTP Servlet Request to the business logic.
A Struts action is an instance of a subclass of an Action class, which implements a portion of a Web application and whose perform or execute method returns a forward.
Q5. What is Action Form.
Ans: ActionForm will maintains the session state for web application and the its object is automatically populated on the server side with data entered from a form on the client side
Q6. What is Front Controller.
Ans: The Front Controller Pattern is a software design pattern listed in several pattern catalogs. The pattern relates to the design of web applications. It “provides a centralized entry point for handling requests.
Q7. What is the Difference between DispatchAction and LookupDispatchAction in Struts Framework?
Ans: This Struts interview question is pretty straight forward and I have put the differences in tabular format to make it easy to read.
Dispatch Action | LookupDispatchAction |
It’s a parent class of LookupDispatchAction | Subclass of Dispatch Action |
DispatchAction provides a mechanism for grouping a set of related functions into a single action, thus eliminating the need to create separate actions for each function. | An abstract Action that dispatches to the subclass mapped executes method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. |
If not using Internalization functionality then dispatch action is more useful. | Lookup Dispatch Action is useful when we are using Internalization functionality |
DispatchAction selects the method to execute depending on the request parameter value which is configured in the XML file. | LookupDispatchAction looks into the resource bundle file and finds out the corresponding key name. We can map this key name to a method name by overriding the getKeyMethodMap() method. |
DispatchAction is not useful for I18N | LookupDispatchAction is used for I18N |
Q8. What are Struts plugins.
Ans: Struts Plugins are modular extensions to the Struts COntroller. They are defined by the org.apache.struts.action.Plugin interface.Struts Plugins are useful are useful when you are allocating resources or preparing connections to the databases or even JNDI resources. This interface defines two lifecycle mathods: init() and desstroy().
Q9. How to call EJB from struts.
Ans: Subclass your Action class and Override execute() method.
Then in body of execute() method do this:
- Get Initial Context
- Get home object using JNDI look up.
- Create the bean and call teh business methods.
We can call EJB from struts by using the service locator design pattern or by Using initial context with create home object and getting return remote referenc object.
Q10. What the Validate () and reset () method does?
Ans: validate(): validate method is Used to validate properties after they have been populated, and this , method is Called before FormBean is passed to Action. Returns a collection of ActionError as ActionErrors. Following is the method signature for the validate() method.
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ( StringUtils.isNullOrEmpty(username) && StringUtils.isNullOrEmpty(password)){
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.usernamepassword.required"));
}
return errors;
}
reset(): reset() method is called by Struts Framework with each request that uses the defined ActionForm. The purpose of this method is to reset all of the ActionForm's data members prior to the new request values being set.
Example :
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.password = null;
this.username = null;
}
Set null for every request.
Q11. What is switch action
Ans: The SwitchAction cl provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application. The SwitchAction cl can be used as is, without extending.
Q12. Difference between Actionform and dform.
Ans:
- An ActionForm represents an HTML form that the user interacts with over one or more pages. You will provide properties to hold the state of the form with getters and setters to access them. Whereas, using DynaActionForm there is no need of providing properties to hold the state. Instead these properties and their type are declared in the struts-config.xml
- The DynaActionForm bloats up the Struts config file with the xml based definition. This gets annoying as the Struts Config file grow larger.
- The DynaActionForm is not strongly typed as the ActionForm. This means there is no compile time checking for the form fields. Detecting them at runtime is painful and makes you go through redeployment.
- ActionForm can be cleanly organized in packages as against the flat organization in the Struts Config file.
- ActionForm were designed to act as a Firewall between HTTP and the Action classes, i.e. isolate and encapsulate the HTTP request parameters from direct use in Actions. With DynaActionForm, the property access is no different than using request.getParameter( .. ).
- DynaActionForm construction at runtime requires a lot of Java Reflection (Introspection) machinery that can be avoided.
Q13. What is Mapping Dispatch.
Ans: An abstract Action that 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 for developers who prefer to combine many similar actions into a single Action class, in order to simplify their application design.
Struts MappingDispatch Action (org.apache.struts.actions.MappingDispatchAction) is one of the Built-in Actions provided along with the struts framework.
The org.apache.struts.actions.MappingDispatchAction class is a subclass oforg.apache.struts.actions.DispatchAction class. This class enables a user to collect related functions into a single action class. It needs to create multiple independent actions for each function.
Q14. What configuration files are used in Struts?
Ans: ApplicationResources.properties and struts-config.xml these two files are used to between the Controller and the Model.
Q15. 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 <global-Exception> tag inside struts-config.XML file
<exception
key="stockdataBase.error.invalidCurrencyType"
path="/AvailbleCurrency.jsp"
type="Stock.account.illegalCurrencyTypeException">
</exception>
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
Q16. Explain about token features in struts
Ans: The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back. This may result in inconsistent transactions and must be avoided. In Struts this problem can be handled by using the saveToken() and isTokenValid() methods of Action class. saveToken() method creates a token (a unique string) and saves that in the user’s current session, while isTokenValid() checks if the token stored in the user’s current session is the same as that was passed as the request parameter.
Use the Action Token methods to prevent duplicate submits
There are methods built into the Struts action to generate one-use tokens. A token is placed in the session when a form is populated and also into the HTML form as a hidden property. When the form is returned, the token is validated. If validation fails, then the form has already been submitted, and the user can be apprised.
saveToken(request)
on the return trip,
isTokenValid(request)
resetToken(request)
Q17. What are the design patterns used in struts.
Ans: Struts is based on model 2 MVC (Model-View-Controller) architecture. Struts controller uses the command design pattern and the action classes use the adapter design pattern. The process() method of the RequestProcessor uses the template method design pattern. Struts also implement the following J2EE design patterns.
- Service to Worker
- Dispatcher View
- Composite View (Struts Tiles)
- Front Controller
- View Helper
- Synchronizer Token
Q18. Is Struts efficient?
Ans: The Struts is not only thread-safe but thread-dependent(instantiates each Action once and allows other requests to be threaded through the original object.
- ActionForm beans minimize subclass code and shorten subclass hierarchies
- The Struts tag libraries provide general-purpose functionality
- The Struts components are reusable by the application
- The Struts localization strategies reduce the need for redundant JSPs
- The Struts is designed with an open architecture--subclass available
- The Struts is lightweight (5 core packages, 5 tag libraries)
- The Struts is open source and well documented (code to be examined easily)
- The Struts is model neutral
Q19. What are the core classes of Struts?
Ans: Action, ActionForm, ActionServlet, ActionMapping, ActionForward are basic classes of Structs.
Q20. What helpers in the form of JSP pages are provided in Struts framework?
Ans:
--struts-html.tld
--struts-bean.tld
--struts-logic.tld