Wednesday, September 26, 2007

Usage of Properties file

This demonstrates the usage of Properties file where the commom values used through out the project can be stored.
-----------------------------------------------------------------------------------------------------------------

package com.mahesh.struts;

import java.util.ResourceBundle;

public class ResourceManager {
private static ResourceBundle bundle=ResourceBundle.getBundle("com.mahesh.struts.
ApplicationResources");


public static String getString(String key){
return bundle.getString(key)
}

}


com.mahesh.struts.ApplicationResources => This is a simple file having key-value pairs. The value can be retrieved from it using the key. The value can be retrieved using the "getString(key)" method.

The key-value pairs will be written in "com.mahesh.struts.ApplicationResources" is as shown below,
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.1.125:3306/
database.user=root
database.password=passwd

The values are retrieved using the key as shown below,

String driverClass = ResourceManager.getString("database.driver");

String dbUrl = ResourceManager.getString("database.url");

String dbUser = ResourceManager.getString("database.user");

String dbPassword = ResourceManager.getString("database.password");

request.getRequestDispatcher and getServletContext().getRequestDispatcher

The only difference is that the pathname specified in 'ServletRequest.getRequestDispatcher' does not require a leading slash, so you can use it with a relative URL. 'ServletContext.getRequestDispatcher' requires a leading slash.

Example:

RequestDispatcher dispatcher=request.getRequestDispatcher("Coreservlet");
(or)
RequestDispatcher dispatcher=request.getRequestDispatcher("/Coreservlet");


But,

RequestDispatcher
dispatcher=getServletContext().getRequestDispatcher
("/Coreservlet");

dispatcher.forward() and response.sendRedirect()

dispatcher.forward()

This will forward all the request parameters to the jsp/servlet mentioned in
RequestDispatcher dispatcher=request.getRequestDispatcher("/Coreservlet");

But the url in the browser will remain the same.


response.sendRedirect()

This will forward all the request parameters to the jsp/servlet mentioned in
response.sendRedirect("/MVC2practice/Coreservlet");

But the url in the browser will not remain the same.

With a sendRedirect(), the only way to pass data is through the session or with web parameters (url?name=value). It does not handle request parameters here as done in "dispatcher.forward(req,res);"

Singleton design pattern

The Singleton design pattern is used for,
1. to create only one object of a class at a time, and
2. that too the object can be created only by its own class.


Necessiates

1. The class' s constructor has to be declared 'private' . This makes the class to create its object by itself only.
2. The clone() method has to be overridden from the base class. This avoids the object to be cloned by any other class.
3. The instantiation (object creation) is done in its own method. And that method should be synchronized and should be declared static.
4. It is declared static because, only then the other classes can call the method without object.


Example:

 class Singleton {  
private static Singleton singletonObject;

// A private Constructor prevents any other class from instantiating.
private Singleton() {
// Optional Code
}
public static synchronized Singleton getSingletonObject() {
if (singletonObject == null) {
singletonObject = new Singleton();
}
return singletonObject;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}

public class SingletonObjectDemo {
public static void main(String args[]) {
// Singleton obj = new Singleton(); Compilation error not allowed

//create the Singleton Object..
Singleton obj = Singleton.getSingletonObject();

// Your Business Logic
System.out.println("Singleton object obtained");
}
}

JSP Implicit Objects

These are Jsp system defined objects. We no need to create these objects to access them.

1. out
out.println("mahesh");

2. request
request.getParameter("abc");
request.getSession();
etc

3. response
response.getWriter();

4. session
session.isNew();
session.getAttribute("abc");
session.setAttribute("abc",123);
session.removeAttribute("abc");
session.getAttributeNames();
session.getServletContext(();
session.invalidate();

5. pageContext
pageContext.getServletContext();
pageContext.getServletConfig();
pageContext.getSession();
pageContext.getRequest();
pageContext.getResponse();
pageContext.getAttribute("abc");
pageContext.setAttribute("abc",123);
6. page

7. config
config.getServletContext();
config.getInitParameter("abc");
config.getInitParameterNames();
config.getServletName();

8.application
application.getInitParameter("abc"); //here this refers to context parameters becoz this is application.
application.getInitParameterNames();
application.getAttribute("abc");
application.setAttribute("abc",123);
application.removeAttribute("abc");
application.getRequestDispatcher(String path);