Tuesday, August 19, 2014

Struts Framework

Struts interview questions:

1) What is struts?
2) Why have you used struts in your application?
3) Main classes used in struts applications?
4) How exceptions are handled in struts application?
5) How validation is performed in struts application?
6) What is difference between dispatch action and lookupdispatchaction in struts framework?
7) What the validate() and reset() method does?
8) How will you make available any message resources definitions file to the struts framework environment?
9) What configuration files are used in struts?
10) Explain struts workflow?
11) Validation on client side or server side? Performance vs security
12) Types of Checks ?
13) How to forward and redirect?.
14) Multithreading in servlets?

What is struts?

- struts is nothing but open source framework mostly used for making web applications.
- whenever we use term framework means it comprises JSP, servlet, custom tag message resources all in one bundle which makes developer task very easy.

Why have you used struts in your application?
- if we go with servlet all HTML code which is related with design part mostly will come inside java code which makes code unmaintainable and complex
- if we use jsp, all java code with some business come inside design part which again make code complex
- struts was made based upon this pattern and easy to develop web application.

Reason:
- mvc design pattern
- front controller pattern
- better flow management 

Main classes used in struts applications?

- action servlet: controller class responsible for handling entire request
- action class: all business logic is developed
- action form: represent our form and associated with action mapping.
- action mapping: using this class we do the mapping between object and action
- action forward: is used to forward the result from controller to destination

How exceptions are handled in struts application?

- programmatically handling: using try{} catch block in code where 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="stock database.error.invalidCurrencyType"
Path="/availableCurrency.jsp"
Type="stock.account.illegalCurrencyTypeException">
</exception>

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

Exception handle in web.XML <error-page><error-code><error-exception>
In base action you need to handle exceptions

How validation is performed in struts application?

- validator-rules.XML: contains default struts pluggable validator definitions. You can add new validation rules by adding an entry in this file.
- validation.XML: contain details regarding validation routines that are applied to the different form beans

What is difference between dispatch action and lookupdispatchaction in struts framework?

- dispatch action: 
It's parent class of lookupDispatchAction
It is not useful for I18N

- lookupDispatchAction:
Subclass of dispatch action
LookupDispatchaction is used for I18N

What the validate() and reset() method does?

- validate():
 this method is used to validate properties after they have been populated
This method is called before formBean is passed to action
Returns a collection of action errors, following is method signature for validate() method.

Public action errors validate(ActionMapping mapping, HttpServletRequest req)
{
}

Reset():
- this method is called by Struts framework with each request that uses the defined ActionForm.
- this purpose is to reset all of the ActionForms data members prior to the new request values being set.

Public void reset(ActionMapping mapping, HttpServletRequest req){
This.password=null;
This.username=null;
}

Set null for every request

How will you make available any message resources definitions file to the struts framework environment?
- message resource definitions file are simple .properties file and these files contains the messages that can be used in the struts project.
- message resource definitions files can be added to struts-config.XML file through <message-resources/> tag.
Ex:
<message-resource key="myResource" parameter="resource.Application" null="false">

What configuration files are used in struts?

- applicationresources.properties and struts-config.XML these two files are used between controller and model.

Explain struts workflow?

Steps

1) create development directory structure

Get *.jar files in web-INF-> lib directory

- commons-beanutils-1.8.jar
- commons-collections.jar
- commons-digester-1.8.jar
- commons-file upload.jar
- commons-Lang.jar
- commons-logging-1.0.4.jar
- commons-validator-1.3.1.jar
- Jakarta-pro.jar
- mysql-connector-java-5.0.8-bin.jar
- struts-core-1.3.10.jar
- struts-tag lib-1.3.10.jar
- struts-tiles-1.3.10.jar

2) Write web.XML deployment descriptor

- servlet definition and mapping of action servlet
- there are several struts specific <init-param> elements
- struts tag libraries also need to be defined <tag lib>

3) write struts-config.XML

- identify required input forms and then define them as <form-bean> elements
- identify required actions and then define them as <action> elements within <action-mappings> element
- decide view selection logic and specify them as <forward> element within <action> element

4) write action form classes (getter and setter methods)
- extend org.apache.struts.action.ActionForm class
- decide set of properties that reflect the input form
- write getter and setter methods for each property
- write validate() method if input validation is desired

Public final class submitForm extends ActionForm{
.....
}

Public actionErrors validate(action mapping mapping, httpservletrequest req)
{
......
}

5) write action classes

- extend org.apache.struts.action.Action class
- handle the request
Decide what kind of server side model objects(EJB, JDO etc) can be invoked
- based on the outcome, select the next view

Public final class submitActin extends Action{
Public actionForward execute(ActionMapping mapping, ActionForm form, HttpServletrequest req, HttpServletResponse response){
.....
Return (mapping.findForward("success"));
}

6) Create applicationResource. Properties and configure web.XML accordingly

- create resource file for default locale
- create resource file for other locales

7) write jsp pages

- write one jsp page for each view
- use struts tags for 
Handling HTML input forms
Writing out messages

Submit.jsp

8) write ant build script

9) build, deploy and test application

Can have multiple struts-config.XML
Can change name of struts-config.XML

Web- struts- config--login action--log inform

for validation
- use validate method
- use validation.XML

Validation to be done on client or server side?

Performance vs security

Validation on client side front end: good performance.
Javascript: no need to send data across server. Security is problem.

Validation on server side/ other side: good security.
Some people do on both sides as they want to reduce network overload

presentation will be changed by client

Types of Checks ?

Data Integrity: not corrupting data. Validate userId.
Format: check length, format, alphabet.

every login form has method called validate()
Write code in back end and get all those errors.

How to forward and redirect?
Set forward to true
Set redirect to true

Multithreading in servlets?
- Servlets are by nature multi-threaded - the servlet container will automatically have multiple threads run the servlet at the same time if simultaneous requests come in.
-  The only way NOT to have multi-threading is to implement the obsolete SingleThreadModel interface (and you really should not do that).

No comments:

Post a Comment