Monday, June 23, 2008

Struts / Java / J2EE Frequently-Asked Interview Questions

Are Struts's action classes Thread-safe?
Yes. Action classes are based on "one-instance and many-threads". This is to conserve resources and to provide the best possible throughput.

What are the various Struts Tag libraries?
There are various struts tags. But the most-repeated tags are:
struts-bean.tld
struts-html.tld
struts-logic.tld
struts-nested.tld

What is ActionMapping in struts?
Action mapping defines the flow of one request. The possible sequence is
User -> request -> Form -> Validation -> Business Code -> Forward -> JSP -> response -> User.
The components involved are Action classes, Forms and JSP.

What are the advantages of having multiple struts-config in the same application?
The implementation with many struts-config is to organize the development work, so that many people may be involved and it is some organized way of doing things. But this would result in some compromise in performance(speed). Technically there is no any difference between single and multiple struts-config files.

What are the ways in which resource file can be used in struts?
Defining message resources in struts-config file.
Programmatically using resource files in Java classes or in JSP files.

Explain the term 'architecture of the application'?
Architecture is the set of rules (or framework) to bring in some common way of assembling or using J2EE components in the application. This helps in bringing consistency between codes developed by various developers in the team.

Which is the architecture followed by struts?
Struts follows MVC architecture.

What are components corresponding o M, V and C in struts?

Model : The model represents the data of an application. Anything that an application will persist becomes a part of model. The model also defines the way of accessing this data ( the business logic of application) for manipulation. It knows nothing about the way the data will be displayed by the application. It just provides service to access the data and modify it. Here 'Form Bean' represents Model layer.

View : The view represents the presentation of the application. The view queries the model for its content and renders it. The way the model will be rendered is defined by the view. The view is not dependent on data or application logic changes and remains same even if the business logic undergoes modification. JSP represents View Layer.

Controller : All the user requests to the application go through the controller. The controller intercepts the requests from view and passes it to the model for appropriate action. Based on the result of the action on data, the controller directs the user to the subsequent view. Action classes (action servlets) represent Controller layer.

Differentiate between the terms 'Design Patterns', 'Framework' and 'Architecture'.
Design Pattern: The various solutions arrived at for the known problem. This helps to avoid re-inventing the wheel. The risk-free solution can be easily used by others. For example, singleton is the design pattern that you can use instantly to enfore one object per class. You do not need to think of this on your own.
Framework: A framework is a structure or set of rules, used to solve or address complex issues. It is basically a reusable designf for the J2EE applications. For example, Struts, JSF,etc., are the frameworks. You can use these frameworks based on the requirements of your application and each has its own set of advantages.

Architecture: It is a design that describes how the various components in the application fit together. For example, MVC is an architecture which is helpful to define how the components interact within the application.


What is the difference between ActionForm and DynaActionForm?
In action form, you need to define the form class that extends ActionForm explicitly, whereas you can define the form dynamically inside the struts-config file when using DynaActionForm.

How can you use Validator framework in struts?
Validator Frameworks are helpful when the application needs server-side validation such that the particular set of validations occur very frequently within the same application. This avoids writing complex code in validation() method in every form bean. Using validator framework, there are different pre-written validations in place. You can customize these validations in XML file.

What are client-side and server-side vaidations?
Client-side validations: These are the validations that id done using javascripts. There is always a danger involved that the user can get through (crack-through) these validations. But for some simple validations, like converting lower-case to upper-case or date validations can be done, you can use javascripts.
Server-side validations: These are the validations done in server-side using Java components (Form bean or in business logics) where the user has no chance to crookedly get through the system.

What are the advantages & differences between using validate() method in form over validations using validator framework.
Refer to the previous-answer.

Define the terms authentication and authorisation.
Authentication is the process/rule that validates if the user is allowed to access the system or not.
Authorization is the process/rule that validates if the user is allowed to access the particular part of the system or not. This occurs after user's successful authentication.

What are the components provided in J2EE to perform authentication and authorization?
Authentication – RequestProcessor and/or Filter.
Authorization - DTO, JDO or Java or Action classes.

Give the difference between between 'DispatchAction' and 'Action'.
A DispatchAction contains various different methods other than the standard execute() method in Action classes. These methods are executed based on some request parameter. For example, you can code in such a way that three buttons (namely Insert, Delete, Update) buttons correspond to different methods such as insert(), delete() and udpate(). The submit button in JSP would have the property that has the value which matches to any one of the methods defined in DispatchAction class.

What is pagination technique? How can you design them in struts?
Pagination is the technique where the bulk of results are split into different pages and only the information where the user can conveniently see are displayed in a page. (Like in Goooooogle). This can be achieved in many ways, but the simplest method is to have a query string (say http://www.testwebsite?pageNumber=2) would lead to information corresponding to resultset rows from 11 to 20. Assuming that you want to display 10 related rows of information, you can set the formula as follows:
Starting row = (pageNumber-1) * + 1 which is equal to 11.
Ending row = Starting row + which is equal to 20.

How can you populate the drop-down list using form properties?
There are many ways for this. But the best method is to use which defines collection that needs to be used to populate the drop-down list, the property to store the selected value and the collection that is used to display the labels (what we see in JSP page). For Example,

html:options collection="form-collection-property"
property="form-property"
labelProperty="form-another-collection-property"

What is the XML parser provided in struts? Can you use it for other purposes?
'Digester' framework. Yes we can use for our applications to store and parse our application-related data.

Difference between Struts 1.0 and 1.1
  • Perform() method was replaced by execute()
  • DynaActionForms are introduced.
  • Tiles Concept is introduced
  • We can write our own Controller by Inheriting RequestProcessor

12 comments:

Praveen said...

Hi...your effort is much appreciated, such as to share your experiences with us....
It's very helpful for everyone, also to me , as I'm keen to take up a career in J2EE & that too with Struts!

Pls continue your effort

Thank u :)
Praveen

Sound of anklets said...

Hi! I have worked on Struts and hence find most of your blogs quite helpful and educative.
Thank you for your efforts.

Ganesan said...

Hi Lakshimi, Your Blog very useful to me and very helpful for those who grow in the Java/J2ee field. Great ur Excellent Job....

Unknown said...

What is advantage of tiles over jsp? As jsp is also having <@ include> and jsp:include

Thank u,
Shailesh.

Unknown said...

Good collection of Question and Well Defind answers.

thanks for sharing it.

Regards
Y.T.Sudhamathi

keerthi said...

hey!!!! you are doing a great job!!best wishes!

avnskirankumar choice said...

Yes, Really.
People like me like it very well.
Because i know the concepts good how to code and how to configure.But, I am not sure to give answer in a proper way when someone asked me like FAQ's.
So i like this article.
Thanks.

Radhi said...

Thank you soooooooooooooo much,I am searching a job,it is very very useful for me and very nice collection of questions from struts.

Unknown said...

Useful post. Thanks for sharing.
java training in chennai

Sankar said...

Very nice explanation.

sankar
java training in chennai

Unknown said...

Good questions as well as answers..Thanks a lot..

Unknown said...

Great Blog Thanks.

Here You Can Find Your First Round Interview question For Job .

Struts Interview Questions

OOPs Interview Questions

JSP Interview Questions