Struts Interview Questions
Give an overview of Struts ? What is an ActionServlet ? What is an Action class ? What is an ActionForm ? What is a Struts Validator Framework ?Struts is a framework with set of cooperating classes, servlets and JSP tags that make up a reusable MVC 2 design.
Client (Browser):A request from the client browser creates an HTTP request. The Web container will respond to the request with an HTTP response, which gets displayed on the browser.
Controller (ActionServlet class and RequestProcessor class):The controller receives the request from the browser, and makes the decision where to send the request based on the struts-config.xml. Design pattern: Struts controller uses the command design pattern by calling the Action classes based on the configuration file struts-config.xml and the RequestProcessor class’s process() method uses template method design pattern by calling a sequence of methods like:
processPath(request, response) read the request URI to determine path element.
processMapping(request,response) use the path information to get the action mapping
processRoles(request,respose,mapping) Struts Web application security which provides an authorization scheme. By default calls request.isUserInRole().
For example allow "/addCustomer" action if the role is executive.
processValidate(request,response,form,mapping) : calls the validate() method of the ActionForm. processActionCreate(request,response,mapping) : gets the name of the action class from the "type" attribute of the <action> element. processActionPerform(req,res,action,form,mapping) : This method calls the execute method of the Action class which is where business logic is written.
ActionServlet class is the controller part of the MVC implementation and is the core of the framework. It processes user requests, determines what the user is trying to achieve according to the request, pulls data from the model (if necessary) to be given to the appropriate view, and selects the proper view to respond to the user. As discussed above ActionServlet class delegates the grunt of the work to the RequestProcessor and Action classes.
Workflow Logic (Action class):The Servlet dispatches the request to Action classes, which act as a thin wrapper to the business logic (the actual business logic is carried out by either EJB session beans and/or plain Java classes). The action class helps control the workflow of the application. (Note: The Action class should only control the workflow and not the business logic of the application). The Action class uses the Adapter design pattern. The Action class is a wrapper around the business logic. The purpose of the Action class is to translate the HttpServletRequest to the business logic. To use the Action class, subclass and overwrite the execute() method. The actual business logic should be in a separate package or EJB to allow reuse of business logic in a protocol independent manner (i.e. the business logic should be used not only by HTTP clients but also by WAP clients, EJB clients, Applet clients etc).
ActionForm class:Java representation of HTTP input data. They can carry data over from one request to another, but actually represent the data submitted with the request. The ActionForm class maintains the state for the Web application. ActionForm is an abstract class, which is subclassed for every input form model. The struts-config.xml file controls, which HTML form request maps to which ActionForm.
View (JSP):The view is a JSP file. There is no business or flow logic and no state information. The JSP should just have tags to represent the data on the browser.
ExceptionHandlercan be defined to execute when the Action class’s execute() method throws an Exception.
When an IOException is thrown then it will be handled by the execute() method of the my.ExceptionHandler class. The struts-config.xml configuration information is translated into ActionMapping, which are put into the ActionMappings collection. Further reading is recommended for a more detailed understanding.
What is Struts Validator Framework ?Form data can be validated on the client side as well as on the server side using the Validator Framework, which was developed as a third-party add on to Struts. This framework generates the java script and it can be used to validate the form data on the client browser. Server side validation of your form can be carried out by subclassing your form class with DynaValidatorForm class. The Validator Framework uses 2 xml configuration files validator-rules.xml (defines reusable standard validation routines, which are usable in validator.xml) and validator.xml (defines validation applicable to a form bean).
How will you display failed validation errors on JSP page ?Use the
How will you turn on the client side validation based on validation.xml file ?Use the
What is a synchronizer token pattern in Struts or how will you protect your Web against multiple submissions ?Web designers often face the situation where a form submission must be protected against duplicate or multiple submissions, which breaks the normal control flow sequence. This situation typically occurs when the user clicks on submit button more than once before the response is sent back or client access a page by returning to the previously book marked page or client resubmits the page by clicking the back button/refresh button .
How do you upload a file in Struts ?In JSP page set the code as shown below:
In the FormBean set the code as shown below:
Are Struts action classes thread-safe ?No. Struts action classes are not thread-safe. Struts action classes are cached and reused for performance optimization at the cost of having to implement the action classes in a thread-safe manner.
How do you implement internationalization in Struts ?Internationalization is built into Struts framework. In the JSP page set the code as shown below:
Now we need to create an application resource file named ApplicationResource.properties
Now in Italian, create an application resource file named ApplicationResource_it.properties
Finally, add reference to the appropriate resource file in the struts-config.xml
What is an action mapping in Struts ?An action mapping is a configuration file (struts-config.xml) entry that, in general, associates an action name with an action. An action mapping can contain a reference to a form bean that the action can use, and can additionally define a list of local forwards that is visible only to this action.
How will you extend Struts ?Struts is not only a powerful framework but also very extensible. You can extend Struts in one or more of the following ways:
Plug-In:Define your own Plug-In class if you want to execute some init() and destroy() methods during the application startup and shutdown respectively. Some services like loading configuration files, initializing applications like logging, auditing, etc can be carried out in the init() method.
RequestProcessor:You can create your own RequestProcessor by extending the Struts RequestProcessor. For example you can override the processRoles(req, res, mapping) in your extended class if you want to query the LDAP server for the security authorization etc.
ActionServlet:You can extend the ActionServlet class if you want to execute your business logic at the application startup or shutdown or during individual request processing. You should take this approach only when the above mentioned approaches are not feasible.
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
J2EE Web and Application Server Interview Questions >>>