Interviw Questions
Interviw Questions
STRUTS
Q 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 enca
psulates 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.
Q 2. What is a framework?
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.
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. Struts provides its own Controller component and integrates with
other technologies to provide the Model and the View. For the Model, Struts can interact with
standard data access technologies, like JDBC and EJB, as well as most any third-party
packages, like Hibernate, iBATIS, or Object Relational Bridge. For the View, Struts works
well with JavaServer Pages, including JSTL and JSF, as well as Velocity Templates, XSLT,
and other presentation systems.
Q 5. What is ActionServlet?
Any java class which extends from org.apache.struts.action.Action is called Action class. The
Action is part of the controller. The purpose of Action Class is to translate the
HttpServletRequest to the business logic. To use the Action, we need to Subclass and
overwrite the execute() method. The ActionServlet (commad) passes the parameterized class
to Action Form using the execute() method. There should be no database interactions in the
action. The action should receive the request, call business objects (which then handle
database, or interface with J2EE, etc) and then determine where to go next. Even better, the
business objects could be handed to the action at runtime (IoC style) thus removing any
dependencies on the model. The return type of the execute method is ActionForward which
is used by the Struts Framework to forward the request to the file as per the value of the
returned ActionForward object..
package com.durgasoft;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class TestAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
Q 9. What is ActionForm?
Struts Framework provides the functionality to validate the form data. It can be use to
validate the data on the users browser as well as on the server side. Struts Framework emits
the java scripts and it can be used validate the form data on the client browser. Server side
validation of form can be accomplished by sub classing your From Bean with
DynaValidatorForm class.
The Validator framework was developed by David Winterfeldt as third-party add-on to
Struts. Now the Validator framework is a part of Jakarta Commons project and it can be used
with or without Struts. The Validator framework comes integrated with the Struts Framework
and can be used without doing any extra settings.
Q11. How you will display validation fail errors on jsp page?
The controller is responsible for intercepting and translating user input into actions to be
performed by the model. The controller is responsible for selecting the next view based on
user input and the outcome of model operations. The Controller receives the request from the
browser, invoke a business operation and coordinating the view to return to the client.The
controller is implemented by a java servlet, this servlet is centralized point of control for the
web application. In struts framework the controller responsibilities are implemented by
several different components like
The ActionServlet Class
The RequestProcessor Class
The Action Class
The ActionServlet extends the javax.servlet.http.httpServlet class. The ActionServlet class
is not abstract and therefore can be used as a concrete controller by your application.
The controller is implemented by the ActionServlet class. All incoming requests are mapped
to the central controller in the deployment descriptor as follows.
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>
All request URIs with the pattern *.do are mapped to this servlet in the deployment descriptor
as follows.
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
<servlet-mapping>
A request URI that matches this pattern will have the following form.
http://localhost:8080/mycontext/actionName.do
The preceding mapping is called extension mapping, however, you can also specify path
mapping where a pattern ends with /* as shown below.
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/do/*</url-pattern>
<url-pattern>*.do</url-pattern>
A request URI that matches this pattern will have the following form.
http://localhost:8080/mycontext/do/action_Name The class
org.apache.struts.action.requestProcessor process the request from the controller. You can
sublass the RequestProcessor with your own version and modify how the request is
processed.
Once the controller receives a client request, it delegates the handling of the request to a
helper class. This helper knows how to execute the business operation associated with the
requested action. In the Struts framework this helper class is descended of
org.apache.struts.action.Action class. It acts as a bridge between a client-side user action and
business operation. The Action class decouples the client request from the business model.
This decoupling allows for more than one-to-one mapping between the user request and an
action. The Action class also can perform other functions such as authorization, logging
before invoking business operation. the Struts Action class contains several methods, but
most important method is the execute() method.
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception
The execute() method is called by the controller when a request is received from a client.
The controller creates an instance of the Action class if one doesn?t already exist. The strut
framework will create only a single instance of each Action class in your application.
Action are mapped in the struts configuration file and this configuration is loaded into
memory at startup and made available to the framework at runtime. Each Action element is
represented in memory by an instance of the org.apache.struts.action. ActionMapping class.
The ActionMapping object contains a path attribute that is matched against a portion of the
URI of the incoming request.
<action>
path= "/somerequest"
type="com.somepackage.someAction"
scope="request"
name="someForm"
validate="true"
input="somejsp.jsp"
<forward name="Success" path="/action/xys" redirect="true"/>
<forward name="Failure" path="/somejsp.jsp" redirect="true"/>
</action>
Once this is done the controller should determine which view to return to the client. The
Example:
<exception
key="database.error.duplicate"
path="/UserExists.jsp"
type="mybank.account.DuplicateUserException"/>
b) Programmatic Exception Handling: Here you can use try{}catch{} block to handle
the exception.
The DispatchAction class is used to group related actions into one class. Using this class,
you can have a method for each logical action compared than a single execute method. The
DispatchAction dispatches to one of the logical actions represented by the methods. It picks a
method to invoke based on an incoming request parameter. The value of the incoming
parameter is the name of the method that the DispatchAction will invoke.
LookupDispatchAction is useful if the method name in the Action is not driven by its name in
the front end, but by the Locale independent key into the resource bundle. Since the key is
always the same, the LookupDispatchAction shields your application from the side effects of
I18N.
The difference between LookupDispatchAction and DispatchAction is that the actual method
that gets called in LookupDispatchAction is based on a lookup of a key value instead of
specifying the method name directly.
The SwitchAction class 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 class can be used as is, without extending.
Q21. What if <action> element has <forward> declaration with same name as global
forward?
In this case the global forward is not used. Instead the <action> element’s <forward> takes
precendence.
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.get Parameter( .. ).
<struts-config>
<!-- ========== Form Bean Definitions ============ -->
<form-beans>
<form-bean name="login" type=" LoginForm" />
</form-beans>
<!-- ========== Global Forward Definitions ========= -->
<global-forwards>
</global-forwards>
<!-- ========== Action Mapping Definitions ======== -->
<action-mappings>
<action
path="/login"
type="LoginAction" >
</action>
</action-mappings>
<!-- ========== Properties Definitions ============ -->
<message-resources parameter="MessageResources" />
<!-- ========== Validator framework Definitions ============ -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Q29. How you will enable front-end validation based on the xml in validation.xml?
The <html:javascript> tag to allow front-end validation based on the xml in validation.xml.
For example the code: <html:javascript formName="logonForm" dynamicJavascript="true"
staticJavascript="true" /> generates the client side java script for the form "logonForm" as
defined in the validation.xml file. The <html:javascript> when added in the jsp file generates
the client site validation script.
uses Servlets. Use the IncludeAction class to include another resource in the response to the
request being processed.
<form-bean name="loginForm"
type="org.apache.struts.action.DynaActionForm" >
<form-property name="userName" type="java.lang.String"/>
<form-property name="password" type="java.lang.String" />
</form-bean>
In your Action subclass that uses your form bean:
o import org.apache.struts.action.DynaActionForm
o downcast the ActionForm parameter in execute() to a DynaActionForm
o access the form fields with get(field) rather than getField()
The ActionServlet plays the role of controller wich is responsible for handling the request and
selecting the correct Application Module and storing ApplicationConfig and
MessageResource bundle in the request object.
If we modify the ActionServlet the Controller may or may not work what happens that
depends on your modification, You have not specify whether you want to create your own
custom ActionServlet by extending ActionServlet and overriding the methods in it or what
exactly you want to modify.