Search This Blog

Thursday 23 February 2017

JSP Implicit Objects

JSP Implicit Objects

JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without declaring them explicitly. These objects are created by JSP Engine during translation phase (while translating JSP to Servlet). They are being created inside service method so the developer can directly use them within Scriptlet without initializing and declaring them.

There are total 8+1 implicit objects available in JSP.

The request Object
The request object is an instance of a javax.servlet.http.HttpServletRequest object. Each time a client requests a page the JSP engine creates a new object to represent that request.
The request object provides methods to get HTTP header information including form data, cookies, HTTP methods etc.
The response Object:
The response object is an instance of a javax.servlet.http.HttpServletResponse object. The server creates the response object to represent the response to the client.
The out Object:
The out object is an instance of a javax.servlet.jsp.JspWriter object and is used to send content in a response.
The JspWriter object contains most of the methods as the java.io.PrintWriter class.
The session Object:
The session object is an instance of javax.servlet.http.HttpSession and behaves exactly the same way that session objects behave under Java Servlets.
The session object is used to track client session between client requests.
The application Object:
The application object is an instance of a javax.servlet.ServletContext.
Application object is a representation of the JSP page through its entire lifecycle. This object is created when the JSP page is initialized and will be removed when the JSP page is removed by the jspDestroy() method.
The config Object:
The config object is an instantiation of javax.servlet.ServletConfig.
The config object allows the JSP programmer access to the Servlet or JSP engine initialization parameters such as the paths or file locations etc.
The pageContext Object:
The pageContext object is an instance of  javax.servlet.jsp.PageContext. The pageContext object is used to represent the entire JSP page.
This object stores references to the request and response objects for each request. The application, config, session, and out objects are derived by accessing attributes of pageContext object.
The page Object:
Page object is a reference to the current Servlet instance (Converted Servlet, generated during translation phase from a JSP page). We can simply use this in place of it.
The exception Object
Exception object is used in exception handling for displaying the error messages. This object is only available to the JSP pages, which has isErrorPage set to true.


JSP Implicit Objects
JSP Implicit Objects



Wednesday 22 February 2017

Builder Design Pattern in Java

Builder Design Pattern in Java
Intent
Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Motivation
As an application grows complex, the complexity of the classes and objects used also increases. Complex objects are constructed by using other objects and needs special handling when being constructed.
An application might need a mechanism for building complex objects that is independent from the ones that make up the object. In this scenario, one might want to try using the Builder design pattern.
Builder pattern allows a client object to construct a complex object by specifying only its type and content, being shielded from the details related to the objects representation. This way the construction process can be used to create different representations. The logic of this process is isolated form the actual steps used in creating the complex object, so the process can be used again to create a different object form the same set of simple objects as the first one.

Applicability
Use the Builder pattern when
·         The algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
·         The construction process must allow different representations for the object that's constructed.

Consequences
·         Builder pattern lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product. It also hides how the product gets assembled.
·         Builder pattern isolates code for construction and representation. The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented.
·         Builder pattern gives you finer control over the construction process.

Implementation
We will implement Builder Pattern by using following classes.
1.      Our first class would be Meal class, which represents food items in a meal. It represents a drink, main course, and side.
public class Meal {

      private String drink;
      private String mainCourse;
      private String side;

      public String getDrink() {
            return drink;
      }

      public void setDrink(String drink) {
            this.drink = drink;
      }

      public String getMainCourse() {
            return mainCourse;
      }

      public void setMainCourse(String mainCourse) {
            this.mainCourse = mainCourse;
      }

      public String getSide() {
            return side;
      }

      public void setSide(String side) {
            this.side = side;
      }

      public String toString() {
            return "Drink : " + drink + ", Main Course : " + mainCourse + ", Side Dish : " + side;
      }

}

2.      Here we create a Builder interface, MealBuilder. It methods will be used to build a meal and to retrieve the meal.
public interface MealBuilder {
   public void buildDrink();

   public void buildMainCourse();

   public void buildSide();

   public Meal getMeal();
}

3.      Our first implementation of MealBuilder is ItalianMealBuilder. Its constructor creates a meal. Its methods are implemented to build the various parts of the meal. It returns the meal using getMeal() method.
public class ItalianMealBuilder implements MealBuilder {

   private Meal meal;

   public ItalianMealBuilder() {
         meal = new Meal();
   }

   @Override
   public void buildDrink() {
         meal.setDrink("Martini");
   }

   @Override
   public void buildMainCourse() {
         meal.setMainCourse("Simple Spinach Lasagna");
   }

   @Override
   public void buildSide() {
         meal.setSide("Spring Green Risotto");
   }

   @Override
   public Meal getMeal() {
         return meal;
   }
}

4.      Similarly second implementation of MealBuilder is JapaneseMealBuilder. Its constructor creates a meal. Its methods are implemented to build the various parts of the meal. It returns the meal using getMeal() method.
public class JapaneseMealBuilder implements MealBuilder{
   private Meal meal;

   public JapaneseMealBuilder() {
         meal = new Meal();
   }

   @Override
   public void buildDrink() {
         meal.setDrink("Chuhai");
   }

   @Override
   public void buildMainCourse() {
         meal.setMainCourse("Tofu and Sweet Potato");
   }

   @Override
   public void buildSide() {
         meal.setSide("Onigiri");
   }

   @Override
   public Meal getMeal() {
         return meal;
   }
}





5.      The MealDirector class takes a MealBuilder as a parameter in its constructor. A different type of meal will be assembled by the MealDirector depending on the Concrete Builder passed in to the constructor.
public class MealDirector {
   private MealBuilder mealBuilder = null;

   public MealDirector(MealBuilder mealBuilder) {
         this.mealBuilder = mealBuilder;
   }

   public void constructMeal() {
         mealBuilder.buildDrink();
         mealBuilder.buildMainCourse();
         mealBuilder.buildSide();
   }

   public Meal getMeal() {
         return mealBuilder.getMeal();
   }
}

6.      The Main class is used as entry point to the application and demonstrate our builder pattern code.
public class Main {
   public static void main(String[] args) {

         MealBuilder mealBuilder = new ItalianMealBuilder();
         MealDirector mealDirector = new MealDirector(mealBuilder);
         mealDirector.constructMeal();
         Meal meal = mealDirector.getMeal();
         System.out.println("Italian Meal : " + meal);
        
         mealBuilder = new JapaneseMealBuilder();
         mealDirector = new MealDirector(mealBuilder);
         mealDirector.constructMeal();
         meal = mealDirector.getMeal();
         System.out.println("Japanese Meal : " + meal);
   }
}


When we execute the Main class we get the following output

Builder Design Pattern in Java
Builder Design Pattern in Java