Important Java Concepts

What is the difference between Web Server and Web Container?

  • Application Server must have both a web container and EJB container.
  • Tomcat is a web container, not a full J2EE application server because tomcat does not have EJB container.
  • Example of most common full J2EE servers is WebLogic, WebSphere and JBoss.

What is Enterprise JavaBeans (EJB)?

  • Enterprise JavaBeans (EJB) technology is the server-side component architecture for Java Platform, Enterprise Edition (Java EE).
  • It also includes the new Java Persistence API (JPA) for the management of persistence and object/relational mapping with Java EE and Java SE.
  • The EJB specification intends to provide a standard way to implement the back-end 'business' code typically found in enterprise applications (as opposed to 'front-end' interface code).
  • Deployment descriptors are not required in ejb3.0 unlike ejb2.x
  • Annotations are used in ejb3.0

XML based vs. Annotation based configuration. Which one is better?

  • Both have their own usage, so mixing of both is recommended
  • For example using spring, XML based configuration should used for dependency injection portion while using transactional management marking a method as transactional with an annotation makes perfect sense.


Forward vs. Redirect. What is difference between the them?

Forward

  • The forward action terminates the action of the current page and forwards the request to another resource such as a JSP page or a Java Servlet.
  • Syntax 
          <jsp:forward page="Relative URL" />

  • Forward action Example:
      Let us consider following two files (a) date.jps and (b) main.jsp as follows:
          Following is the content of date.jsp file:
     <p>
     Today's date: <%= (new  java.util.Date()).toLocaleString()%>
     </p>
       Here is the content of main.jsp file:
     <html>
     <head>
     <title>The include Action Example</title>
     </head>
     <body>
     <center>
     <h2>The include action Example</h2>
     <jsp:forward page="date.jsp" />
     </center>
     </body>
     </html>

Now call main.jsp. Here it discarded the contents from main page and displayed the contents from forwarded page only.

Today's date: 12-Sep-2010 14:54:22

Redirect

  • Page redirection is generally used when a document moves to a new location and we need to send the client to this new location
  • The simplest way of redirecting a request to another page is using method sendRedirect() of response object.
  • You can also use setStatus() and setHeader() methods together to achieve the same redirection
  • JSP example showing page redirection

     <%@ page import="java.io.*,java.util.*" %>
     <html>
     <head>
     <title>Page Redirection</title>
     </head>
     <body>
     <center>
     <h1>Page Redirection</h1>
     </center>
     <%
       // new location to be redirected
       String site = new String("http://www.photofuntoos.com");
       response.setStatus(response.SC_MOVED_TEMPORARILY);
       response.setHeader("Location", site);
     %>
     </body>
     </html>



Difference between Composition vs. Aggregation vs. Association?

Composition

  • Composition is a strong type of Aggregation (strong association).
  • Part- whole relationship where part cannot exist without a whole
  • The life cycle of child is fully dependent on parent object, if parent object deletes all child object will also be deleted
  • Example:
    • Relationship between House and Rooms. House can contain multiple rooms, there is no independent life of room and if we delete the house, room will be automatically deleted.
final class Car {  
  private final Engine engine;  
 
  Car (EngineSpecs specs) {  

   // lives within car object- Engine cannot exist without car

    engine = new Engine(specs);  
  }  
 
  void move() {  
    engine.work();  
  }  

 
Here the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car.


Aggregation

  • Aggregation is a weak association
  • Part- whole relationship where part can exist without a whole
  • Example 1: 
       final class Car { 
       private Engine engine;  
 
       void setEngine(Engine engine) {
            this.engine = engine;
        }  
 
       void move() {  
           if (engine != null)  
           engine.work();  
       }  
    }
  
  • Example 2:
         public class Person {

         private Address address;

          public Person(Address  address)

          {
             this.address = address;
          }
     }
Here the outside world can still have a reference to the Engine or Address and it can exist without Car or Person respectively
  

Association

  • Association is the relation between two objects. In other words its connectivity between two objects. Aggregation and compositions are form of association.

No comments:

Post a Comment