Java Interview Questions - 28

Java Interview Questions - 28

1.What is Struts Validator Framework?

The “Struts Framework” provides the functionality to validate the form data.It can be used to validate the data in the user’s browser as well as on the server side. Struts Framework checks the JavaScript code and it can be used to validate the form data on the client browser.
Server side validation of form data can be accomplished by subclassing your “form” Bean with DynaValidatorForm class. The “Validator” framework was developed by David Winterfeldt as a third-party “add-on” to Struts.Now the Validator framework is part of the “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 any making any additional settings.

2.Describe the details of XML files used in the “Validator Framework”?

The Validator Framework uses two XML configuration files 1) validator-rules.xml and 2) validation.xml.The validator-rules.xml defines the standard validation routines.
These are reusable and used in validation.xml to define the form specific validations. The validation.xml defines the validations applied to a form bean.

3.How would you display “validation fail” errors on a JSP page?

Following tag displays all the errors: <html:errors/>

4.What is an “Action Class”?

The “Action Class” is part of the “Model” and 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”, we need to subclass and overwrite the execute() method.All the database and business processing is done in the “Action” class.It is advisable to perform all the database related work in the “Action” class.The ActionServlet (command) passes the parameterized class to ActionForm using the execute() method.
The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file according to the value of the returned ActionForward object.

5.Write code of any Action Class?

Here is the code of Action Class that returns the ActionForward object. package;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, return mapping.findForward("testAction");}}

6.What is an “ActionForm”?

An “ActionForm” is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the “ActionForm” object is automatically populated on the server side with data entered from a form on the client side. 

7.How can one enable front-end validation based on the xml in validation.xml?

The <html:javascript>tag allows front-end validation based on the xml in validation.xml. For example the code:generates the client side JavaScript for the form "logonForm" as defined in the validation.xml file.
The <html:javascript> when added in the JSP file generates the client side validation script.

8.Default Sub-application

To maintain backward compatibility, Struts 1.1 allows one default sub-application per application. The URI of the resources (i.e. JSPs, HTMLs, etc) in the default sub-application will have an empty sub-app prefix. This means when an existing 1.0 application is "dropped" into Struts 1.1, theoretically, it will automatically become the default sub-application.

9.What is the Jakarta Struts Framework?

Jakarta Struts is an open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications.
Jakarta Struts is a robust architecture and can be used for the development of applications of any size. The “Struts framework” makes it easier to design scalable, reliable Web applications.

10.What is an ActionServlet?

The class org.apache.struts.action.ActionServlet is called the ActionServlet. In the Jakarta Struts Framework this class plays the role of controller. All the requests to the server go through the “Controller”. The “Controller” is responsible for handling all the requests.

11.How can one make any “Message Resources” definitions file available to the “Struts Framework” environment?

“Message Resources” definitions file are simple .properties files and these files contain the messages that can be used in the struts project. “Message Resources” definition files can be added to the struts-config.xml file through <message-resources /> tag. Example:  <message-resources parameter="MessageResources" />Both JSF and Struts will continue to exist for a while. The Struts community is aware of JSF and is positioning itself to have strong support for JSF. See What about JSTL and JavaServer faces?
From a tools perspective, if you look at the support for JSF versus Struts in WebSphere Studio, the Struts tools are focused around the controller aspects. The Web Diagram editor helps build your Struts configuration and the wizards/editors build Struts artifacts. The JSF tools are geared towards building pages, and in essence, hide the JSF framework from you. Expect WebSphere Studio to support both frameworks for a while. As JSF matures, expect to see some of the controller aspects in JSF to become toolable.

12.Why do the Struts tags provide for so little formatting?

The Struts tags seem to provide only the most rudimentary functionality. Why is there not better support for date formatting and advanced string handling?
Three reasons: First, work started on the JSTL and we didn't want to duplicate the effort. Second, work started on Java Server Faces, and we didn't want to duplicate that effort either.
Third, in a Model 2 application, most of the formatting can be handled in the ActionForms (or in the business tier), so all the tag has to do is spit out a string. This leads to better reuse since the same "how to format" code does not need to be repeated in every instance. You can "say it once" in a JavaBean and be done with it. Why don't the Struts taglibs offer more layout options?
Since the Struts tags are open source, you can extend them to provide whatever additional formatting you may need. If you are interested in a pre-written taglib that offers more layout options, see the struts-layout taglib.
In the same arena, there is a well regarded contributor taglib that can help you create Menus for your Struts applications.

13.Why does the <html:link> tag URL-encode javascript and mailto links?

The <html:link> tag is not intended for use with client-side references like those used to launch Javascripts or email clients. The purpose of link tag is to interject the context (or module) path into the URI so that your server-side links are not dependent on your context (or module) name. It also encodes the link, as needed, to maintain the client's session on the server. Neither feature applies to client-side links, so there is no reason to use the <html:link> tag. Simply markup the client-side links using the standard tag.

14.Why does the option tag render selected=selected instead of just selected?

Attribute minimization (that is, specifying an attribute with no value) is a place where HTML violates standard XML syntax rules. This matters a lot for people writing to browsers that support XHTML, where doing so makes the page invalid. It's much better for Struts to use the expanded syntax, which works the same on existing browsers interpreting HTML, and newer browsers that expect XHTML-compliant syntax. Struts is following the behavior recommended by the XHTML specification

15.Do I have to use JSPs with my application?

The short answer to this question is: No, you are not limited to JavaServer Pages.
The longer answer is that you can use any type of presentation technology which can be returned by a web server or Java container. The list includes but is not limited to:
* JavaServer Pages,* HTML pages,* WML files,* Java servlets,* Velocity templates, and * XML/XLST Some people even mix and match apparently unrelated technologies, like PHP, into the same web application.

16. 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:o Validator-Rules.xml file o Validation.xml file

17. 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

18.Provide an overview of web container.

The web container is used to implement the web components functionality. The functionality specifies an environment, called as runtime environment which the web components security, deployment, life-cycle management transaction management, request-response services. A container manages the servlet / JSP/ EJB functionality. J2EE server runs the components and the containers. A web container will also manage the database connection pooling, data persistence and access to the J2EE API.

19.Explain the Java Servlet programming model.

The servlets are server side components in a web application. These components platform independent and on any server as the java technology is platform independence. The servlets are dynamically loaded by the JRE of servlets. When a request is received from a client the web container / web server will ignite the servlet. The servlet processes the request the sends the response back to the container / web server which in turn sends the response back to the client.

20.Describe in brief about deployment descriptors.

A deployment descriptor describes the configuration information to deploy a web application. For a web application, it directs the deployment tool that is to deploy a module for a specific container such as ejb container / servlet/jsp container. For web applications the deployment descriptor is web.xml and must be placed in a WEB-INF directory of a web application root directory. For EE deployment descriptor the name should be application.xml and must be placed in a META-INF directory at the top level of the application .ear file.

21.Why it called Struts?

Because the designers want to remind us of the invisible underpinnings that hold up our houses, buildings, bridges, and ourselves when we are on stilts. This excellent description of Struts reflect the role the Struts plays in developing web applications.

22.Do we need to pay the Struts if being used in commercial purpose?

No. Struts is available for commercial use at no charge under the Apache Software License. You can also integrate the Struts components into your own framework just as if they were written in house without any red tape, fees, or other hassles.

23.What are the core classes of Struts?

Action, ActionForm, ActionServlet, ActionMapping, ActionForward are basic classes of Structs.

24.What is the design role played by Struts?

The role played by Structs is controller in Model/View/Controller(MVC) style. The View is played by JSP and Model is played by JDBC or generic data source classes. The Struts controller is a set of programmable components that allow developers to define exactly how the application interacts with the user.

25.How Struts control data flow?

Struts implements the MVC/Layers pattern through the use of ActionForwards and ActionMappings to keep control-flow decisions out of presentation layer.

26.What configuration files are used in Struts?

ApplicationResources.propertiesstruts-config.xml These two files are used to bridge the gap between the Controller and the Model.

27. Is ActionForm part of the Model in MVC?

People think the ActionForm is part of the Model. They read the MVC description and think... Hey!... this is the Model's state, lets move this as parameters all over the Model. Now you have a tight coupling between the Model and the Struts framework.There are plenty of examples in which the ActionForm properties are mapped 1:1 on DTO POJO objects and those used instead, but people think its a waste of code and time and just pass it along to the Model.

28.Why are some of the class and element names counter-intuitive?

The framework grew in the telling and, as it evolved, some of the names drifted. The good thing about a nightly build, is that everything becomes available to the community as soon as it is written. The bad thing about a nightly build is that things like class names get locked down early and then become difficult to change.

29.Why is ActionForm a base class rather than an interface?

The MVC design pattern is very simple to understand but much more difficult to live with. You just need this little bit of Business Logic in the View logic or you need just that little bit of View logic in the Business tier and pretty soon you have a real mess.
Making ActionForm a class takes advantage of the single inheritance restriction of Java to it makes it more difficult for people to do things that they should not do.
ActionForms implemented as interfaces encourage making the property types match the underlying business tier instead of Strings, which violates one of the primary purposes for ActionForms in the first place (the ability to reproduce invalid input, which is a fundamental user expectation). ActionForms as an interface would also encourage using existing DAO objects as ActionForms by adding ‘implements ActionForm’ to the class. This violates the MVC design pattern goal of separation of the view and business logic.
Since the goal of struts is to enforce this separation, it just makes more sense for Struts to own the ActionForm. DynaActionForms relieve developers of maintaining simple ActionForms. For near zero maintenance, try Niall Pemberton's LazyActionForm

30.Do ActionForms have to be true JavaBeans?

The utilities that Struts uses (Commons-BeanUtils since 1.1) require that ActionForm properties follow the JavaBean patterns for mutators and accessors (get*,set*,is*). Since Struts uses the Introspection API with the ActionForms, some containers may require that all the JavaBean patterns be followed, including declaring "implements Serializable" for each subclass. The safest thing is to review the JavaBean specification and follow all the prescribed patterns.
Since Struts 1.1, you can also use DynaActionForms and mapped-backed forms, which are not true JavaBeans. For more see ActionForm classes in the User Guide and Using Hashmaps with ActionForms in this FAQ.

31.Can I use multiple HTML form elements with the same name?

Yes. Define the element as an array and Struts will autopopulate it like any other.
private String[] id= {};public String[] getId() { return; }public void setItem(String id[]) { = id;}And so forth

32.Can I use multiple HTML form elements with the same name?

Yes. The issue is that only one action class can be associated with a single form. So the real issue is how do I decode multiple submit types to a single Action class. There is more than one way to achieve this functionality.
The way that is suggested by struts is right out of the javadoc for LookupDispatchAction . Basically, LookupDispatchAction is using the keys from ApplicationProperties.resources as keys to a map of actions available to your Action class. It uses reflection to decode the request and invoke the proper action. It also takes advantage of the struts <html:submit> tags and is straight forward to implement.
You can roll your own with JavaScript events and javascript:void (document.forms["myform"].submit) on any html element. This gives you control of how you want your page to look. Again you will have to decode the expected action in the execute method of your action form if you choose this route.

33.Why doesn't the focus feature on the <html:form> tag work in every circumstance?

Unfortunately, there is some disagreement between the various browsers, and different versions of the same browser, as to how the focus can be set. The <html:form> tag provides a quick and easy JavaScript that will set the focus on a form for most versions of most browsers. If this feature doesn't work for you, then you should set the focus using your own JavaScript. The focus feature is a convenient "value-add" -- not a core requirement of the tag. If you do come up with a JavaScript that provides the final solution to this project, please post your patch to this Bugzilla ticket.

34.Why are my checkboxes not being set from ON to OFF?

A problem with a checkbox is that the browser will only include it in the request when it is checked. If it is not checked, the HTML specification suggests that it not be sent (i.e. omitted from the request). If the value of the checkbox is being persisted, either in a session bean or in the model, a checked box can never unchecked by a HTML form -- because the form can never send a signal to uncheck the box. The application must somehow ascertain that since the element was not sent that the corresponding value is unchecked.
The recommended approach for Struts applications is to use the reset method in the ActionForm to set all properties represented by checkboxes to null or false. The checked boxes submitted by the form will then set those properties to true. The omitted properties will remain false. Another solution is to use radio buttons instead, which always submit a value.
It is important to note that the HTML specification recommends this same behavior whenever a control is not "successful". Any blank element in a HTML form is not guaranteed to submitted. It is therefor very important to set the default values for an ActionForm correctly, and to implement the reset method when the ActionForm might kept in session scope.

35.Can't I just create some of my JavaBeans in the JSP using a scriptlet?

Struts is designed to encourage a Model 2/MVC architecture. But there is nothing that prevents you from using Model 1 techniques in your JavaServer Pages, so the answer to the question is "Yes, you can".
Though, using Model 1 techniques in a Struts application does go against the grain. The approach recommended by most Struts developers is to create and populate whatever objects the view may need in the Action, and then forward these through the request. Some objects may also be created and stored in the session or context, depending on how they are used.
Likewise, there is nothing to prevent you from using scriptlets along with JSP tags in your pages. Though, many Struts developers report writing very complex scriplet-free applications and recommend the JSP tag approach to others.
For help with Model 1 techniques and scriptlets, you might consider joining the Javasoft JSP-interest mailing list, where there are more people still using these approaches.

36.Why aren't the Struts tags maintained as part of the Jakarta Taglibs project ?

Development of both products began about the same time. Leading up to the release of 1.0, it was thought better to continue to develop the taglibs alongside the controller. Now that 1.0 is out, the JavaServer Pages Standard Taglib is in active development. Once work on JSTL stabilizes, the Struts taglibs will be revisited. Tags which are not linked directly to the framework may be hosted at Jakarta Taglibs instead.

37.Are the Struts tags XHTML compliant ?

If you use an <html:html xhtml="true> or <html:xhtml/> element on your page, the tags will render as XHTML (since Struts 1.1).

38.What about JSTL and JavaServer Faces ?

JSTL, the JavaServer Standard Tag Library, is a set of JSP tags that are designed to make it easier to develop Web applications. JavaServer Faces (JSF) is a specification for a new technology that promises to make it easier to write MVC applications, both for the Web and for the desktop.
The inventor of Struts, Craig McClanahan, is the specification co-lead for JavaServer Faces (JSR 127), and architect of the reference implemenation as well as Java Studio Creator. Both JSTL and JSF are complementary to Struts.
The mainstay of the Struts framework is the controller components, which can be used with any Java presentation technology. As new technologies become available, it is certain that new "glue" components will also appear to help these technologies work as well with Struts.
Struts originally came bundled with a set of custom JSP tags. Today, several extensions are available to help you use Struts with other popular presentation technologies, like XSLT and Velocity. Likewise, extensions for JSTL and JSF are now available as well.
The JSTL reference implementation is available through the Jakarta Taglibs site. A JSTL taglibs for Struts, Struts-El , is available and distributed with Struts beginning with the 1.1 release.
The JSF specification and reference implementation is available through Sun's The JSF specification and reference implementation is available through Sun's Java ServerFaces page. An early-release JavaServer Faces taglib for Struts, Struts-Faces, is also in early release and available through the nightly build. The Struts Faces taglib is expected to work with any compliant JSF implementation, including MyFaces.

39.Is there a particularly good IDE to use with Struts

Struts should work well with any development environment that you would like to use, as well as with any programmers editor. The members of the Struts development team each use their own tools such as Emacs, IDEA, Eclipse, and NetBeans.

40.Why was reload removed from Struts (since 1.1)?

The problem with ReloadAction was that Struts was trying to act like a container, but it couldn't do a proper job of it. For example, you can't reload classes that have been modified, or (portably) add new classes to a running web application (even if the container supported it).
Meanwhile, as Struts 1.1 was being developed, work progressed on things like Tomcat's reload command via the Manager webapp. This feature allows you to quickly reload-on-demand, complete with saving and restoring your session). It started to make even less sense for Struts to half-implement a feature that containers are implementing fully.
A more minor point is that freezing the configuration information at application startup time allows Struts to safely access the mapping information without bothering with synchronization. The "startup-only" strategy creates a modest but real improvement in performance for all users.
So, ReloadAction is not supported since Struts 1.1 for two reasons: * It never did let you reload everything that you would really want to -- particularly changed classes -- so many people ended up having to reload the webapp anyway.
* Containers are starting to offer reload-on-demand features which does the same thing as the Struts ReloadAction, only better.* Not supporting ReloadAction lets Struts avoid doing synchronization locks around all the lookups (like figuring out which action to use, or the destination of an ActionForward) so applications can run a little faster.
Of course, if someone came up with an implementation that solved these problems without creating any others, we would not be opposed to including a new ReloadAction.

41.What is a modular application? What does module-relative mean?

Since Struts 1.1, the framework supports multiple application modules. All applications have at least one root, or default, module. Like the root directory in a file system, the default application has no name. (Or is named with an empty string, depending your viewpoint.) Developing an application with only a default module is no different from how applications were developed under Struts 1.0. Since Struts 1.1, you can add additional modules to your application, each of which can have their own configuration files, messages resources, and so forth. Each module is developed in the same way as the default module. Applications that were developed as a single module can added to a multiple module application, and modules can promoted to a standalone application without change. For more about configuring your application to support multiple modules, see Configuring Applications in the User Guide.
But to answer the question =:0), a modular application is a Struts application that uses more than one module. Module-relative means that the URI starts at the module level, rather than at the context level, or the absolute-URL level.
* Absolute URL: http://localhost/myApplication/myModule/ * context-relative: /myModule/ * module-relative: / The Struts Examples application is a modular application that was assembled from several applications that were created independently.

42.Why is it called Struts?

It's a reference to struts in the architectural sense, a reminder of the nearly invisible pieces that hold up buildings, houses, and bridges.

43.Do I have to credit Struts on my own website?

You need to credit Struts if you redistribute your own framework based on Struts for other people to use. (See the Apache License for details.) But you do not need to credit Struts just because your web application utilizes the framework. It's the same situation as using the Apache HTTPD server or Tomcat. Not required if its just running your web site.

44.Where can I get a copy of Struts?

The best place to download Struts is at The nightly builds are very stable, and recommended as the best place to start today.

45.How do I install Struts?

To develop applications with Struts, you can usually just add the Struts JAR file to your Java development environment. You can then start using the Struts classes as part of your own application. A blank Struts application (in the webapps directory, open struts-blank.war) is provided, which you can just copy to get a quick-start on your own brainchild.
Since the full source code for Struts is available, we also provide complete instructions for compiling your own Struts JAR from scratch. (This is actually easier than it looks!)
Your Struts application can usually be deployed using a standard WAR file. In most cases, you simply deposit the WAR file on your application server, and it is installed automatically. If not, step-by-step installation instructions for various servlet containers are available.

46.When do I need "struts.jar" on my classpath?

When you are compiling an application that uses the Struts classes, you must have the "struts.jar" on the classpath your compiler sees -- it does not have to be on your CLASSPATH environment variable.
Why is that an important distinction? Because if you are using a servlet container on your development machine to test your application, the "struts.jar" must not be on your CLASSPATH environment variable when running the container. (This is because each Web application must also have their own copy of the Struts classes, and the container will become confused if it is on the environment path as well.)
There are several general approaches to this issue: * Use ANT for building your projects -- it can easily assemble classpaths for the compiler. (This is how Struts itself is built, along with Tomcat and most other Java-based projects).
* Use an IDE where you can configure the "class path" used for compilation independent of the CLASSPATH environment variable.* Use a shell script that temporarily adds struts.jar to the classpath just for compilation, for example javac -classpath /path/to/struts.jar:$CLASSPATH $@

47.Does Struts include its own unit tests?

Struts currently has two testing environments, to reflect the fact that some things can be tested statically, and some really need to be done in the environment of a running servlet container.
For static unit tests, we use the JUnit framework. The sources for these tests are in the "src/test" hierarchy in the source repository, and are executed via the "test.junit" target in the top-level build.xml file. Such tests are focused on the low-level functionality of individual methods, are particularly suitable for the static methods in the org.apache.struts.util utility classes. In the test hierarchy, there are also some "mock object" classes (in the org.apache.struts.mock package) so that you can package up things that look like servlet API and Struts API objects to pass in as arguments to such tests.
Another valuable tool is Struts TestCase which provides a useful harness for Action classes that can be used with JUnit or Cactus.

48.If the framework doesn't do what I want, can I request that a feature be added?

First, it's important to remember that Struts is an all-volunteer project. We don't charge anyone anything to use Struts. Committers and other developers work on Struts because they need to use it with their own applications. If others can use it too, that's "icing on the cake". If you submit a patch for a feature that a Committer finds useful, then that Committer may choose to volunteer his or her time to apply the patch. If you just submit an idea without a patch, it is much less likely to be added (since first someone else has to volunteer their time to write the patch).
We are grateful for any patches, and we welcome new ideas, but the best way to see that something gets added to the framework is to do as much of the work as you can, rather than rely on the "kindness of strangers". Worst case, you can apply the patch to your copy of Struts and still use the feature in your own application. (Which is what open source is ~really~ all about.)

49.Where can I get help with Struts?

The Struts package comes complete with a Users Guide to introduce people to the framework and its underlying technologies. Various components also have their own in-depth Developers Guide, to cover more advanced topics. Comprehensive Javadocs are included along with the full source code. For your convenience, these are bundled together as a self-installing application. The struts-documentation.war is the same bundle that is deployed as the Struts Web site.
The Strut's mailing list is also very active, and welcomes posts from new users. Before posting a new question, be sure to consult the MAILING LIST ARCHIVE and the very excellent How To Ask Questions The Smart Way by Eric Raymond. Please do be sure to turn off HTML in your email client before posting.

50.What's the difference between Struts and Turbine? What's the difference between Struts and Espresso?

If you are starting from scratch, packages like Turbine and Espresso can be very helpful since they try to provide all of the basic services that your team is likely to need. Such services include things like data persistence and logging.
If you are not starting from scratch, and need to hook up your web application to an existing infrastructure, then "plain vanilla" Struts can be a better choice. The core Struts framework does not presuppose that you are using a given set of data persistence, presentation, or logging tools. Anything goes =:0)
Compared to other offerings, Struts endeavors to be a minimalist framework. We try leverage existing technologies whenever we can and provide only the missing pieces you need to combine disparate technologies into a coherent application. This is great when you want to select your own tools to use with Struts. But, if you prefer a more integrated infrastructure, then packages like Turbine or Espresso (which uses Struts) are perfectly good ways to go.
See also * < >* < >
* < >

51.What is ActionForm?

An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side.

52.What is Struts Validator Framework?

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.

53.Give the Details of XML files used in Validator Framework?

The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, these are reusable and used in validation.xml. to define the form specific validations. The validation.xml defines the validations applied to a form bean. How you will display validation fail errors on jsp page? - The following tag displays all the errors: < html:errors/ >

54.Why do we need Struts?

Java technologies give developers a serious boost when creating and maintaining applications to meet the demands of today's public Web sites and enterprise intranets. Struts combines Java Servlets, Java ServerPages, custom tags, and message resources into a unified framework. The end result is a cooperative, synergistic platform, suitable for development teams, independent developers, and everyone in between.

55.How does Struts work?

Java Servlets are designed to handle requests made by Web browsers. Java ServerPages are designed to create dynamic Web pages that can turn billboard sites into live applications. Struts uses a special Servlet as a switchboard to route requests from Web browsers to the appropriate ServerPage. This makes Web applications much easier to design, create, and maintain.

56.Is Struts compatible with other Java technologies?

Yes. Struts is committed to supporting industry standards. Struts acts as an integrator of Java technologies so that they can be used in the "real world".

57.Who wrote Struts?

There are several active committers to the Struts project, working cooperatively from around the globe. Dozens of individual developers and committers contributed to the Struts 1.x codebase. All interested Java developers are invited to contribute to the project. Struts is a Apache Software Foundation project, with the mission to "provide secure, enterprise-grade server solutions based on the Java Platform that are developed in an open and cooperative fashion".
Struts was created by Craig R. McClanahan and donated to The Apache Software Foundation in May 2000. Craig was the primary developer of both Struts 1.x and Tomcat 4. Tomcat 4 was the basis for the official reference implementation for a servlet 2.3 and JSP 1.2 container.
Craig's current focus is as architect of the Sun Java Studio Creator (formerly Project Rave). Craig also serves as the Specification Lead for JavaServer Faces (JSR-127), and is the Web Layer Architect for the Java2 Enterprise Edition (J2EE) platform as a whole.

58.How is the MVC design pattern used in Struts framework?

In the MVC design pattern, application flow is mediated by a central Controller. The Controller delegates requests to an appropriate handler. The handlers are tied to a Model, and each handler acts as an adapter between the request and the Model. The Model represents, or encapsulates, an application's business logic or state. Control is usually then forwarded back through the Controller to the appropriate View. The forwarding can be determined by consulting a set of mappings, usually loaded from a database or configuration file. This provides a loose coupling between the View and Model, which can make an application significantly easier to create and maintain.
Controller--Servlet controller which supplied by Struts itself; View --- what you can see on the screen, a JSP page and presentation components; Model --- System state and a business logic JavaBeans.

59.Who makes the Struts?

Struts is hosted by the Apache Software Foundation(ASF) as part of its Jakarta project, like Tomcat, Ant and Velocity.

60.What helpers in the form of JSP pages are provided in Struts framework?

--struts-html.tld--struts-bean.tld --struts-logic.tld 61.Is Struts efficient?

The Struts is not only thread-safe but thread-dependent(instantiates each Action once and allows other requests to be threaded through the original object.
ActionForm beans minimize subclass code and shorten subclass hierarchies The Struts tag libraries provide general-purpose functionality The Struts components are reusable by the application
The Struts localization strategies reduce the need for redundant JSPs The Struts is designed with an open architecture--subclass available The Struts is lightweight (5 core packages, 5 tag libraries)
The Struts is open source and well documented (code to be examined easily) The Struts is model neutral

62.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.

63.What is ActionServlet?

The class org.apache.struts.action.ActionServlet is the called the ActionServlet. In the the Jakarta Struts Framework this class plays the role of controller. All the requests to the server goes through the controller. Controller is responsible for handling all the requests.

64.How you will make available any Message Resources Definitions file to the Struts Framework Environment?

Message Resources Definitions file are simple .properties files and these files contains the messages that can be used in the struts project. Message Resources Definitions files can be added to the struts-config.xml file through < message-resources / > tag. Example: < message-resources parameter= MessageResources / >

65.What is the different actions available in Struts?

The different kinds of actions in Struts are:

66.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 single UserAction. like add user, delete user and update user
DispatchAction execute the action based on the parameter value it receives from the user.

67. 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

68.What is the difference between ForwardAction and IncludeAction?

The difference between ForwardAction and InculdeAction are:
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.

69.What is difference between LookupDispatchAction and DispatchAction?

The difference between LookupDispatchAction and DispatchAction are given below:
LookupDispatchAction is the subclass of the DispatchActionActual method that gets called in LookupDispatchAction whereas DispatchAction dispatches the action based on the parameter value.

70.What is LookupDispatchAction?

The LookupDispatchAction class is a subclass of DispatchActionThe 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 subclassIt 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.

71. 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

72.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.

73. What are the various Struts tag libraries?

The various Struts tag libraries are:
i.HTML Tags: used to create the struts input forms and GUI of web page.
ii.Bean Tags: used to access bean and their properties.
iii.Logic Tags: used to perform the logical operation like comparison
iv.Template Tags: used to changes the layout and view.
v.Nested Tags: used to perform the nested functionality
vi.Tiles Tags: used to manages the tiles of the application

74. 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.

75. 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 documentatio,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

76. Difference between Html tags and Struts specific HTML Tags

Difference between HTML tag and Struts specific HTLM tags are:
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.

77. 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.

78. How to display validation errors on JSP page?

Validation error: Validation error are those error which arises when user or client enters the invalid format data into the form. For this validation of data struts enables the programmer with the Validator() method which validates both the data from client side and the server side.
We can display all error in the JSP page by using the following syntax in the code.
SYNTAX: <html:error/>

79. How to use forward action to restrict a strut application to MVC?

We can use the ForwarAction to restrict the Struts application to Model View Controller by following coding:
<forward name="CitizenDetailsPage"
path="/" />

<action path=”/gotoCitizenDetails”
type=”org.apache.struts.actions.ForwardAction” />

80. What is ActionMapping?

In action mapping is the mapping of the action performed by the user or client on the application.
-We specify the action class for a specific user’s action. Like we provide the path or URL and different view based on user event.
-We can also define where control of the page deviate in case of validation error in the form.
-We can include ActionMapping in code like this:
<action path="/a" type=myclasse.A name="myForm">
<forward name="Login" path="/login.jsp"/>
<forward name="error" path="/error.jsp"/>

81. What is role of 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 no. of requests at same time.

82. How to combine the Struts with Velocity Template?

We can combine Struts and Velocity template by performing following steps:
1. Set classpath to Velocity JARs
2. Make web.xml file to identify the Velocity servlet.
3. Select Velocity toolbox.xml in WEB-INF directory.
4. Modify struts-config to point its views to Velocity templates instead of JSPs.
5. Create a Velocity template for each page you want to render.

83. In how many ways duplicate form submission can occurs?

The submission form can be duplicated by the any of the following ways:
Using refresh button.By clicking submit button more than once before the server sent back the response.By clicking back navigation button present in browser.The browser is restores to submit the form again.
By clicking multiple times on a transaction that is delayed than usual.

84. What is the difference between Struts 1 and struts2?

The difference between struts1 and struts2 are below:
Struts1 uses ActionServlet as Controller where as Struts2 uses Filter as a controller.
Struts1 uses the ActionForm for mapping the JSP forms but in struts2 there no such ActionForm.
Struts1 uses Validation() method to validate the data in the page forms where as struts 2 validation of data is ensure by Validator Framework.
In Struts 1 the bean and logic tag libraries are often replaced by JSTL, but Struts 2 has such tag libraries that we don’t need to use JSTL.

85. What are the steps used to setup dispatch action?

To setup the dispatch action the following steps are used:
Create a subclass for DispatchAction.Create method for logical action and their related actions.Request Parameter is created for each action.Define ActionMapping.The JSP takes on the subclass defined for dispatch action method names as their values

86. What is difference between Interceptors and Filters?

The difference between Interceptors and filter are below:
Filters are based on Servlet Specification whereas Interceptors are based on Struts2.
Filters are executed only when patter matches whereas Interceptors executes for all request qualifies for a front controller.
Filters are not Configurable method calls whereas Interceptors methods can be configured.

87. What are the Custom tags?

Custom Tags are User Defined Tags, which means that user can create those tags depending upon their need in the application.When a JSP page consisting of user- defined or custom tag is translated into a Servlet, the custom is also get translated into operation
Help in fast development of the application due to custom tag reusability.

88. What is the difference between empty default namespace and root namespace?

The difference between the empty default namespace and root name space are:
When namespace attribute is not defined then it is referred to as Empty Default Namespace whereas when name space attribute is assign with forward slash(/) then it is referred to as Root Name Space.
The root namespace must be matched.

89. What is the difference between RequestAware and ServletRequestAware interface?

The difference between RequestAware and ServletRequestAware are:
RequestAware enables programmer with the attributes in the Servlet Request as a map whereas ServletRequestAware enables programmer with HttpServletRequest object.
ServletRequestAware are more flexible than RequestAware.servletRequestAware makes action class highly coupled with Servlet environment which is not possible in RequestAware.

90. What are inner class and anonymous class?

Inner class: classes that are defined within other classes.
The nesting is a relationship performed between two different classes.
An inner class can access private members and data.
Inner classes have clearly two benefits:
o Name control
o Access control.
Anonymous class: Anonymous class is a class defined inside a method without a name.
It is instantiated and declared in the same method.It does not have explicit constructors.

91.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.

92.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.

93.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.

94.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.

95.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"

96.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.

97.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.

98.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.

99.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.

100.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

101.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.

102.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.

Also Read:



Post a Comment