Interview Questions - J2EE
Struts2
Struts2 mainly originates form pervious Webwork2 project.
The main concepts in Struts2 are ActionContext, Interceptors, Action, Result, ValueStack and OGNL.
First every request must be handled by interceptor stack, these interceptors are provided and configured by Struts2, the main pre-configured interceptors includes, params interceptor, validation, fileupolad etc.
Interceptor is a key part of the Struts 2 framework; these interceptors are invoked both before and after the action.
Interceptor allows common, cross-cutting tasks to be defined in clean, reusable components that can keep separate from the action code.
Then Struts2 will call Action the request corresponds, developers can implement whatever logic, usually Action will call business service method to do real work.
At last Action would return a control string that tells Struts2 which results would render the view that’ll be returned in the request response.
Then Struts2 would return the response to client.
In the process, Struts 2 uses the ValueStack as a storage area for application data that will be needed during the processing of a request.
And OGNL is used to reference and manipulate properties on the ValueStack.
Struts2 VS Struts1
1.Action is POJO, Action does not need include any Struts2 specific API, even implanting the interface is optional, thus Action is easy to test, we don't need to mock HTTP objects.
2.There is no ActionForms any more.
3.Enhanced Results, except JSP, Struts2 also supports other view technologies such
as Free-marker, Velocity, PDF etc.
4.Struts2 provides easy Spring integration,
5.Struts2 also provides Intelligent Defaults, we can use annotations and conventions to drastically reduce XML-based configuration.
6.Struts2 provides easy plugging mechanisms, we can easily extend Struts2 functionality, and there are already many Struts2 plugin, such as restful, Json etc.
Maven:
Compared with Ant, Maven is very easy to use, Using ant, we have to write its build file build.xml, from scratch, all by ourselves, it is very painful, and boring,
1.First, Maven provides Archetypes, we can use just one command to generate java project, and its pom.xml file in seconds, and the pom.xml file is much simpler than ANt's build.xml.
2.Second, Maven follows best practices, it defines Standard Project Layout, and developers can quickly understand the project's layout.
3.Third, Use Maven to manage project's dependencies, in the past, if one project need third-party libraries, developers directly put jars to the project lib directory, Using maven, we just need to decalre the dependency in pom.xml, maven will download the jars, and put it into local repository, this can save disk space, and if we need to update the jar to new version, we just need to modify the pom.xml.
4.Fourthly, maven introduces the concept, standard life cycles. user just need to learn a small set of commands to invoke defined goals to build any Maven project,Maven provides many built-in life-cycles: such as validate,compile,test,package,install,deploy.
Maven is a plugin execution framework, it is easy to write maven plugin to extend maven's functionality, there are already hundreds of maven plugin, such as mvn eclipse:eclipse.
Also we can run mvn ant:ant to generate ant build files from pom.xml directly, also maven can invoke ant target.
EJB3:
EJB 3 learns and benefits a lot from several technologies such as POJO, IOC, AOP, Spring and Hibernate.
EJB3's chief advantages include:
1.EJB3 supports AOP, that is aspect oriented programming, First we can use Interceptors to encapsulate common cross-cutting concerns in one place. Secondly, developers can use annotation to declare transaction and security.
2.EJB3 supports Dependency Injection, we can use annotation @EJB, @Resource to inject and refer another EJB, or external resource directly.
3.EJB2 demand that SessionBean and EntityBean implement many unnecessary callback methods, such as ejbActivate,ejbPassivate,ejbCreate etc, normally these methods are empty, in EJB3, developers don't need to write these methods at all, and if they really want to provide this function, they can annotate any method with annotation @PreDestroy,@PrePassivate etc.
4.Home interface is removed from EJB3; instead EJB3 enables simple lookup process.
5.EJB3 entity bean is totally redesigned, JB3.0 entity bean doesn't have home, local or remote interfaces, it is just a POJO with newly introduced annotation such as @Entity, @Id and uses entity manager to make java Objects persisted and detached, and also there is no Data Transfer Object any more in EJB3, and Persistence has its own specification: Java Persistence. Other OR mapping project such as Hibernate, Toplink supports JPA.
6.Annotations are used in EJB3, EJB3 use it as a "replacement" for XML Deployment Descriptors, Using Annotation can simplify configuration drastically. Also developers can choose to use all annotations, all XML or a mix of annotations and a XML, and EJB deployment descriptors are not required in ejb3.0.
Spring
Spring is a full Stack Web Framework, its main purpose is to make J2EE easier to use, and it provides many features.
Two key components of Spring are IOC or DI that is inverse of control, or dependency injection.
Spring supports AOP, that is aspect oriented programming.
Spring supports declarative transaction management through the Spring AOP framework.
Spring abstracts and integates with a number of related frameworks, Spring makes it easy to work with JDBC, JPA, Hibernate, or struts, and EJB, JMS, Email, Scheduling, Quartz, Test support and many others.
Aslo Spring has its own web framewrok, SpringMVC, and Security framework, Spring Security.
In one word, Spring is a very important framework for j2ee enterprise application.
AOP
AOP means Aspect-oriented programming.
It is used to complement traditional OOP; OOP is good for implementing the core business logic, but not the crosscutting concerns.
Crosscutting concern is a functionality that spans multiple modules of an application.
In applications, there are core business concerns and crosscutting concerns.
Crosscutting concerns are very common in enterprise applications such as logging, validation, authentication, and transaction.
Traditional object-oriented approach would cause two problems.
1.First is code tangling. Methods would include core logic and other crosscutting concerns; this will lead to poor code maintainability and reusability.
2.Another problem is code scattering, we have to repeat same statements and logics multiple times in multiple modules to fulfill a single requirement, if later we need to change the logic; we have to change in all places.
AOP provides another way to implement crosscutting concerns.
Instead of the classes and interfaces of OOP, the main programming elements of AOP are aspects.
We implement the crosscutting concerns as aspects, and then through XML or Annotation, we weave aspects into various objects, in this way, we place the crosscutting concern in only one place.
Spring has its AOP implementation, and AspectJ is the most complete and popular AOP framework in the Java community.
Hibernate
Hibernate is an object/relational mapping project of Jboss, Hibernate maps Database Table to Java Class, Table's column to Class Field.
Hibernate lets us develop persistent classes following OO idiom - including association, inheritance, polymorphism, composition, and collections.
Hibernate allows us to use its portable HQL to express queries in object-oriented terms—using classes and properties of classes, Using HQL, we don't need write different SQL for different Databse any more. we also it supports native SQL.
Hibernate supports Annotation and XML configuration.
Also Hibernate supports JPA specification and allows us to write queries with the standardized JPA Query Language.
Dojo
Dojo is mainly divided into three projects: Dojo, Dijit, and Dojox.
Dojo module provides many wonderful basic and core functions, such as Language and Browser Utilities, DOM Utilities, String Utilities, and asynchronous requests (dojo.xhrGet,dojo.xhrPost), JSON support, Drag-and-Drop(dojo.dnd), Animation and Special Effects,.
And Dojo normalizes browser, allowing the same source code to work in several browsers. Dojo also fixes several gross browser errors such as memory leaks in IE's event system.
Dijit is Dojo widget framework, it packs a fantastic library of widgets, such as Form Widgets, Layout Widgets, such as tab/split containers, Tree, Calendar, TimePicker, Dialog, Slider, Progress Bar, AutoComplete etc...
We can create Dijit widgets declaratively or programmatically.
Declarative widgets use nonstandard HTML attributes such as dojoType=.
We can also create same widgets through JavaScript, These are called programmatic widgets.
DojoX is a collection of subprojects for Dojo Extensions and Experimental, such as
Grid, Charting, highlight, FishEye, ColorPicker etc.
Dojo also provides several tools like ShrinkSafe, Checkstyle.
Dojo's loader, packaging and build system is very powerful.
It modularizes Dojo - a large project into any number of independent source files yet packages it into only a few highly compressed files.
dojo.require maps a module name to a URL, downloads, and then evals that resource.
Main API:
dojo.require
Asynchronous Request
dojo.xhrGet
dojo.xhrPost
dojo.connect(myButton, "click", myFunction)