Java Struts Interview

1.What is MVC?
Model-View-Controller (MVC) is a design pattern put together to help control change. MVC decouples interface from business logic and data.
  • Model : The model contains the core of the application's functionality. The model encapsulates the state of the application. Sometimes the only functionality it contains is state. It knows nothing about the view or controller.

  • View: The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur.

  • Controller:The controller reacts to the user input. It creates and sets the model.

2.What is a framework?
A framework is made up of the set of classes which allow us to use a library in a best possible way for a specific requirement.

3.What is Struts framework?
Struts framework is an open-source framework for developing the web applications in Java EE, based on MVC-2 architecture. It uses and extends the Java Servlet API. Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java.

4.What are the components of Struts?
Struts components can be categorize into Model, View and Controller:
  • Model: Components like business logic /business processes and data are the part of model.
  • View: HTML, JSP are the view components.
  • Controller: Action Servlet of Struts is part of Controller components which works as front controller to handle all the requests.

5.What are the core classes of the Struts Framework?
Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design.
  • JavaBeans components for managing application state and behavior.
  • Event-driven development (via listeners as in traditional GUI development).
  • Pages that represent MVC-style views; pages reference view roots via the JSF component tree.

6.What is ActionServlet?
ActionServlet is a simple servlet which is the backbone of all Struts applications. It is the main Controller component that handles client requests and determines which Action will process each received request. It serves as an Action factory – creating specific Action classes based on user’s request.

7.What is role of ActionServlet?
ActionServlet performs the role of Controller:
  • Process user requests
  • Determine what the user is trying to achieve according to the request
  • Pull data from the model (if necessary) to be given to the appropriate view,
  • Select the proper view to respond to the user
  • Delegates most of this grunt work to Action classes
  • Is responsible for initialization and clean-up of resources

8.What is the ActionForm?
ActionForm is javabean which represents the form inputs containing the request parameters from the View referencing the Action bean.

9.What are the important methods of ActionForm?
The important methods of ActionForm are : validate() & reset().

10.Describe validate() and reset() methods ?
validate() : Used to validate properties after they have been populated; Called before FormBean is handed to Action. Returns a collection of ActionError as ActionErrors. Following is the method signature for thevalidate() method.

public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

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. 

public void reset() {}

11.What is ActionMapping?
Action mapping contains all the deployment information for a particular Action bean. This class is to determine where the results of the Action will be sent once its processing is complete.

12.How is the Action Mapping specified ?
We can specify the action mapping in the configuration file called struts-config.xml. Struts framework createsActionMapping object from <ActionMapping> configuration element of struts-config.xml file

<action-mappings>
 
<action path="/submit"
           
type="submit.SubmitAction"
       
 name="submitForm"
       
 input="/submit.jsp"
       
 scope="request"
        
validate="true">
 
<forward name="success" path="/success.jsp"/>
 
<forward name="failure" path="/error.jsp"/>
 
</action>
</action-mappings>

13.What is role of Action Class?
An Action Class performs a role of an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request.

14.In which method of Action class the business logic is executed ?
In the execute() method of Action class the business logic is executed.

public ActionForward execute(
                ActionMapping mapping,
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response)
          
throws Exception ;

execute() method of Action class:
  • Perform the processing required to deal with this request
  • Update the server-side objects (Scope variables) that will be used to create the next page of the user interface
  • Return an appropriate ActionForward object

15.What design patterns are used in Struts?
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

16) 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 numbar of requests at same time.
17) 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:
1. Validator-Rules.xml file
2. Validation.xml file
18) 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.
19) 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.
20) 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.
21) 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.
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 :
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() {}
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 the difference between session scope and request scope when saving FormBean?
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.
What is the different actions available in Struts?
The different kinds of actions in Struts are:
- ForwardAction
- IncludeAction
- DispatchAction
- LookupDispatchAction
- SwitchAction
24) 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 UserAction. like add user, delete user and update user.
- DispatchAction execute the action based on the parameter value it receives from the user.
25) 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
26) What is the difference between ForwardAction and IncludeAction?
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.
What is difference between LookupDispatchAction and DispatchAction?
LookupDispatchAction is the subclass of the DispatchAction.
- Actual method that gets called in LookupDispatchAction whereas DispatchAction dispatches the action based on the parameter value.
27) 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.
28) 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.
- Use to forward a request to another resource in your application.
29) 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.
30) 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.
 31) 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.
 32) 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.
 33)Difference between Html tags and Struts specific HTML Tags
 - 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.
 34) What is the difference between session scope and request scope when saving          FormBean?
 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.


No comments:

Post a Comment