Friday, December 16, 2011

Difference between ServletConfig and ServletContext

Following table lists down the main differences and others things are discussed afterwords:
ServletConfigServletContext(It should have been ApplicationContext)
One ServletConfig object is created per servlet One ServletContext object is created per Application
Can be used to pass deployment time information to the Servlet. Information is stored as parameters in the deployment descriptor(web.xml) Can be used to share Application level information across servlets and is this information is also stored in the web.xml as well as parameters (So it kind of acts as GlobalContext or Application Context)
Provides a set of methods to set and get the initialization configuration information Provides a set of methods to the servlet so that servlet can communicate with the Container
------------------------Servlet can access a ServletContext object only through ServletConfig object

Working with ServletConfig:

We will explaing here how to declare the Servlet init Parameters in in the Deployment Descriptor(web.xml) and how to retrieve them in the servlet also how to get the object of Servlet Context from ServletConfig.
web.xml: Declaring init parameters for a specific servlet

    
    SampleServ
    SampleServ
    in.javaespresso.web.SampleServ
    
     Sample Init Parameters
     servParamservValue
  
Servlet Code: Getting init parameters inside the servlet
getServletConfig().getInitParameter("servParam");
Servlet Code: Getting ServletContext object using ServletConfig
ServletContext context = getServletConfig().getServletContext();

Working with ServletContext:

Let us get the context paramteres declared in the web.xml in the servlet and jsp
web.xml: Declaring context parameters

    Sample context parameters
    emailjava.espresso@gmail.com
Servlet Code: Accessing context params in Servlet
getServletContext().getInitParameter("email")
JSP code: Accessing context params in JSP
<%
getServletContext().getInitParameter("email");
%>

Please find the code for the article attached here

Monday, November 14, 2011

Remote Method Invocation(RMI) Example

Here, we will learn basics of RMI and will implement a small example

Remote Method Invocation Basics
As the name suggests,it is a system to call remote method,which may be on other JVM or other computer. It is built on top of Sockets. But,you don't have to deal with sockets and protocols directly. You can directly call a method on some other machine and it works,as if you have called it locally. To achieve this it passes the objects over the network(objects should be Serializable).

Drawback: RMI can only be used in java. Interaction with other language platform is not possible.

Requirements of RMI
Client Requirement
Let us suppose there is a server Object which is registered.
* Client Object have to find the Server Object(by looking it up in registry)
* Serialize the parameters and send
* Deserialize the response from the server

Server Requirement
* Server should be a remote object
* The remote object should be registered(rmiregistry is used for registration)

Example

HelloInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloInterface extends Remote {
 public String sayHello() throws RemoteException;
 }

Hello.java
import java.rmi.RemoteException;
public class Hello implements HelloInterface 
{

private String message; 
public Hello (String msg) throws RemoteException {
message = msg;
 }
public String sayHello() throws RemoteException {
 return message;  
 }
}
HelloServer.java
import java.net.MalformedURLException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

class HelloServer {

 public static void main(String[] argv)  throws RemoteException, MalformedURLException {
  
  try{
   Hello remoteObj=new Hello("This is test RMI");
    Remote obj = UnicastRemoteObject.exportObject(remoteObj, 9502);
          Registry r = LocateRegistry.createRegistry(9502);
          r.bind("RemoteHelloServer", obj);
   System.out.println("Hello Server is ready.");
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  
 }
}

Steps for compilation:
1)javac HelloInterface.java
2)javac Hello.java
3)javac HelloWorld.java
4)rmiregistry //In a separate command prompt
5)rmic -vcompat Hello //will generate Hello_Stub.class and Hello_Skel.class
6)java HelloWorld

After step 6,Server is up

Steps for client code:
* Copy the HelloInterface.java and Hello_Stub.class file in client area
* Write a client program HelloClient.java, which looks up in registry.

HelloClient.java
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

class HelloClient {
 public static void main(String[] args) {
  try {
   Registry registry;
  HelloInterface hello;
  registry=LocateRegistry.getRegistry(
                "localhost",(new Integer(9502)).intValue());
  String name = "RemoteHelloServer";
  
   hello = (HelloInterface) registry.lookup(name);
   System.out.println(hello.say());
  } catch (Exception e) {
   System.out.println("HelloClient exception: " + e);
  }
 }
}

Compile and run the client code.
After this it is easy to expose your methods with RMI.