J2EE Tutorial - Running RMI Example
greeter.java
import java.rmi.*;
public interface greeter extends Remote
{
String greetme(String s) throws RemoteException;
}
greeterimpl.java
import javax.rmi.PortableRemoteObject;
import javax.naming.*;
import java.rmi.*;
import java.rmi.server.*;
import greeter.*;
public class greeterimpl extends
PortableRemoteObject implements greeter
{
String s;
public static void main(String args[])
{
try
{
Context initialNamingContext = new InitialContext();
// System.setSecurityManager(new RMISecurityManager());
greeterimpl obj = new greeterimpl("sam");
initialNamingContext.rebind("sam",obj);
System.out.println("remote server ready!");
System.out.println("sam is registered & waiting for call");
}
catch(Exception e1) 0
{
System.out.println("error"+e1);
} 1
}
public greeterimpl(String a) throws RemoteException
{ 2
s = " SUN'S RMI-IIOP(RMI-CORBA) PROGRAM WELCOMES .........";
}
public String greetme(String a) throws RemoteException 3
{
return s+ a;
} 4
}
greeterclientservlet.java
import javax.servlet.*; 5
import java.io.*;
import javax.servlet.http.*;
import greeter.*; 6
import greeterimpl.*;
import _greeterimpl_Tie.*;
import java.rmi.*; 7
import java.rmi.server.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.*; 8
import java.util.*;
public class greeterclientservlet extends HttpServlet
{ 9
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws
ServletException,IOException 0
{
response.setContentType("text/html");
PrintWriter out=response.getWriter(); 1
try
{
String s1=request.getParameter("text1"); 2
Hashtable hash1 = new Hashtable();
hash1.put("java.naming.factory.initial","com.sun.jndi.cosnaming.CNCtxFactory");
hash1.put("java.naming.provider.url","iiop://localhost:900"); 3
Context context1 = new InitialContext(hash1);
greeter obj=
(greeter)PortableRemoteObject.narrow(context1.lookup("sam"),greeter.class); 4
String s = obj.greetme(s1);
out.println("<html>");
out.println("<body>"); 5
out.println(s);
out.println("</body>");
out.println("</html>"); 6
}
catch(Exception e1) { out.println("error"+e1); }
} 7
}
greeterclientservlet.htm
<html> 8
<body>
<form method=post action="http://localhost:8080/servlet/greeterclientservlet">
ENTER NAME <input type=text name="text1" size=20> 9
<br>
<input type=submit>
</form> 0
</body>
</html>
1
How to compile and run the RMI-IIOP program?
1) We require jndi package for running this program.
We create a folder in c:\jdk1.3\bin\rmicorba. 2
2) We brought jndi folder into c:\jdk1.3\bin\rmicorba.
3) cd to c:\jdk1.3\bin\rmicorba.
4) set path=c:\windows;c:\windows\command;c:\jdk1.3\bin; 3
5) set classpath=c:\jdk1.3\bin\rmicorba
6) create greeter.java in this folder.
7) compile greeter.java 4
8) create and compile greeterimpl.java
9) MOST IMPORTANT STEP.
>rmic -iiop greeterimpl 5
(creates corba style stub and tie class files.)
10) start tnameserv // ( this is transient name server)
>tnameserv 6
11) Go to another window ,set path & classpath as before.
start the server and register the remote object. (this line must be typed most carefully without break!) (continuously).
>java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory 7
-Djava.naming.provider.url=iiop://localhost helloimpl
( -Djava means we are supplying runtime properties)
12) If every thing is done correctly , you will find the following 8
message in the console.
'remote server ready!'
'sam is registered and waiting for call' 9
13) Now compile greeterclientservlet.java
We have to set classpath=%classpath%;c:\jsdk2.0\src
14) After compiling copy all the classfiles in c:\jdk1.3\bin\rmicorba folder to 0
c:\tomcat\webapps\root\web-inf\classes folder
15) copy greeterclientservlet.htm to c:\tomcat\webapps\root directory.
16) Start the tomcat server as already seen. 1
17) type the URL as 'http://localhost:8080/greeterclientservlet.htm"
in the browser.
18) We get the form. Type your name ( say 'Thomas') and submit. 2
19) We will get the greeting :
" SUN'S RMI-IIOP(RMI-CORBA) PROGRAM WELCOMES ...Thomas"
20) Thus we have invoked the remote object's method in Corba style. 3
21) So far so good. But how about the automatic generation of IDL for non-java end? This is the best part of RMI--IIOP,
See step 9 listed above. We used the -iiop flag while invoking rmic compiler.
If we use -idl flag instead, we get the idl file automatically generated. 4
We can then distribute this idl file to other corba customers.
You would have noticed that , we have changed the order in which distributed order technologies in the j2ee basket were mentioned. Instaed of taking up JAVA-IDL after RMI, we took up RMI-IIOP. This way, we can easily compare RMI & RMI-IIOP.
Just to complete the picture, we will now see the JAVA-IDL version of the same program and then on to the meat of J2EE (IE) EJB. 5
There are 4 files as follows:
1) greeter.idl
2) greeterserver.java 6
3) greeterservlet.java
4) greeterservlet.htm
We begin with greeter.idl , which is the interface file written in OMG-IDL. 7
Let us edit this file in say, c:\idl folder.
Set path for the dos window
c:\idl>set path=c:\windows\command;c:\jdk1.3\bin 8
Also set classpath:
c:\idl>set classpath=c:\idl
Create greeter.idl as given below. 9
greeter.idl
interface greeter
{ 0
string greetme(in string s);
};
1
We now use the idl compiler in jdk1.3 as follows:
c:\idl>idlj -fall greeter.idl
This command creates a number of java source files as follows: 2
1) greeter.java
2) _greeterImplBase.java
3) _greeterStub.java 3
4) greeterHelper.java
5) greeterHolder.java
6) greeterOperations.java 4
These files are created in the same folder as greeter.idl because we have not specified 'module' in the idl file. This is a simple method.
Now, create greeterserver.java in the same folder as follows:
greeterserver.java 5
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import java.io.*; 6
public class greeterserver
{
public static void main(String[] args) 7
{
try
{ 8
ORB orb = ORB.init(args,null);
greeterservant ref = new greeterservant();
orb.connect(ref); 9
org.omg.CORBA.Object objRef =orb.resolve_initial_references("NameService");
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("greeter",""); 0
NameComponent path[] = {nc};
ncRef.rebind(path,ref);
System.out.println("server ready...waiting for client"); 1
java.lang.Object sync = new java.lang.Object();
synchronized(sync)
{ 2
sync.wait();
}
} 3
catch(Exception e1)
{
System.out.println(" "+e1); 4
}
}//main
}//class 5
//-------------
class greeterservant extends _greeterImplBase
{ 6
public String greetme(String s)
{
return "How are you....?"+s; 7
}
}
Compile all the java files as follows: 8
c:\idl>javac *.java
This compiles all the java files in the folder.
We now create the servlet file which is the client for the corba program. 9
greeterservlet.java
import org.omg.CORBA.*;
import org.omg.CosNaming.*; 0
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 1
import java.util.*;
public class greeterservlet extends HttpServlet
{ 2
greeter server;
public void init(ServletConfig config) throws ServletException
{ 3
super.init(config);
try
{ 4
ORB orb = ORB.init((String[])null,null);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService"); 5
NamingContext ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("greeter","");
NameComponent path[] = {nc}; 6
server = greeterHelper.narrow(ncRef.resolve(path));
System.out.println("servlet init ok!");
} catch(Exception e1){System.out.println(""+e1);} 7
} //init
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{ 8
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String a =request.getParameter("text1"); // name 9
System.out.println(a);
String s1=server.greetme(a);
out.println(s1); 0
}
} //--servlet over-----
1
To compile the servlet file, we should set classpath as follows:
c:\idl>set classpath=%classpath%;c:\jsdk2.0\src;
c:\idl>javac greeterservlet.java 2
This command compiles the servlet.
The following html file invokes the servlet. 3
<html>
<body>
<form method=post action="http://localhost:8080/servlet/greeterservlet"> 4
<input type=text name='text1'>
<input type=submit>
</form> 5
</body>
</html>
Now, we copy greeterservlet.htm to c:\tomcat\webapps\root 6
Then we copy all the class files in c:\idl folder to:
c:\tomcat\webapps\root\web-inf\classes folder.
We are now ready to test our corba program. 7
Start Tomcat as before.
Start the browser and type the URL as:
"http://localhost:8080/greeterservlet.htm" 8
We get a form with a textbox. Fill up your name,say Thomas and
submit.
We will get : 9
"How are you...?Thomas".
We have completed the first leg of our long journey.
In the second part, we will take up EJB , XML etc. In the previous instalment , we familiarized ourselves with Servlet, JSP, JavaMail, JDBC, RMI, RMI-IIOP and IDL. as, these were preliminaries to grapple with EJB. Now , it is time to takeup EJB.