twg
asked on
servlet as a client for RMI
I want to write a servlet which will actually be the client for RMI.
where shall I put the code in the servlet? for example: the following is the code for creating RMI client
try{
RemoteInterface server = (RemoteInterface)Naming.lo okup("Serv erTest");
serverString = server.message("Hello there");
System.out.println("The server says:\n " + serverString);
}
catch (Exception e)
{
System.out.println("Error while performing RMI");
}
if I want to put this in a servlet,should I put it in the init() method or somewhere else?
are there any other changes that have to be done? Please give me a code example.
where shall I put the code in the servlet? for example: the following is the code for creating RMI client
try{
RemoteInterface server = (RemoteInterface)Naming.lo
serverString = server.message("Hello there");
System.out.println("The server says:\n " + serverString);
}
catch (Exception e)
{
System.out.println("Error while performing RMI");
}
if I want to put this in a servlet,should I put it in the init() method or somewhere else?
are there any other changes that have to be done? Please give me a code example.
you can put this code everywhere ...
ASKER
Thanks heyhey but putting this code (at least in init() method) doesn't work.
It throws an exception and prints to the dos window "Error while performing RMI".You probably know the "hello there" so i want that the client will print his message on the browser (using the servlet).
It throws an exception and prints to the dos window "Error while performing RMI".You probably know the "hello there" so i want that the client will print his message on the browser (using the servlet).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just clikc around this
http://www.servlets.com/jsp/examples/index.html
http://www.servlets.com/jsp/examples/index.html
Hi twq,
1. IF you want RMI call for every server request thread,the remote method is to be invoked put it in service(or doGet() or doPost() or any..).
2.If you think, RMI call only once, put it in inti() method.
It is your application strategy.
Best of luck
1. IF you want RMI call for every server request thread,the remote method is to be invoked put it in service(or doGet() or doPost() or any..).
2.If you think, RMI call only once, put it in inti() method.
It is your application strategy.
Best of luck
ASKER
Thanks heyhey but putting this code (at least in init() method) doesn't work.
It throws an exception and prints to the dos window "Error while performing RMI".You probably know the "hello there" so i want that the client will print his message on the browser (using the servlet).
It throws an exception and prints to the dos window "Error while performing RMI".You probably know the "hello there" so i want that the client will print his message on the browser (using the servlet).
replace
catch (Exception e)
{
System.out.println("Error while performing RMI");
}
with
catch (Exception e)
{
e.printStackTrace();
}
and post here the full exception stacktrace
catch (Exception e)
{
System.out.println("Error while performing RMI");
}
with
catch (Exception e)
{
e.printStackTrace();
}
and post here the full exception stacktrace
hey sorry I had not meant to answer but only comment,feel freet o reject it.
ASKER
thanks everyone. I checked the examples from the book and I changed few lines in my code but still it doesn't work and throws the exception. therefor I enclose my code and if you can tell me what's wrong with it, I'll be gratefull.
the RMI server:
----------------
import java.rmi.*;
import java.rmi.server.UnicastRem oteObject;
import java.rmi.registry.*;
//creating an implementing class
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface
{
String name;
public RemoteObject (String name) throws RemoteException
{
super();
this.name = name;
}
public String message (String message) throws RemoteException
{
String rs = "My name is:" + name + ",thanks for your message:" + message;
System.out.println ("Returning:" + rs);
return "My name is:" + name + ",thanks for your message:" + message;
}
public static void main (String args[])
{
try{
//create the registry on port 1099
Registry reg = java.rmi.registry.LocateRe gistry.cre ateRegistr y(1099);
String myName = "ServerTest";
RemoteObject theServer = new RemoteObject (myName);
Naming.rebind(myName, theServer);
System.out.println ("Ready to continue");
}
catch (Exception e)
{
System.out.println ("an exception occured while creating server"+ e);
}
}//end of main
}//end of class
the RMI client (which is actually a servlet):
-------------------------- ---------- -
import java.io.*;
import javax.servlet.*;
import java.rmi.*;
import java.rmi.registry.*;
import javax.servlet.http.*;
public class RemoteClient implements Servlet {
private ServletConfig config;
private String serverString;
RemoteInterface server;
public void init (ServletConfig config) throws ServletException {
this.config = config;
try{
Registry registry = LocateRegistry.getRegistry ();
RemoteInterface server = (RemoteInterface)registry. lookup("Se rverTest") ;
serverString = server.message("Hello there");
System.out.println( "The server says:\n " + serverString);
}
catch (Exception e)
{
System.out.println("Error while performing RMI");
} }
}//end of init
public void destroy() {}
public ServletConfig getServletConfig() {
return config;
}//end of destroy
public String getServletInfo() {
return "A Simple servlet";
}//end of getServletInfo
public void service (ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType ("text/html");
PrintWriter out = res.getWriter();
out.println ( "<html>" );
out.println ( "<head>" );
out.println ( "<title>A Sample Servlet</title>" );
out.println ( "</head>" );
out.println ( "<body>" );
out.println ( "<h1>A Servlet as a RemoteClient</h1>" );
out.println ( "<h1>The server says:\n" +serverString+ "</h1>");
out.println ( "</body>" );
out.println ( "</html>" );
out.close();
}//end of service
}
the RMI server:
----------------
import java.rmi.*;
import java.rmi.server.UnicastRem
import java.rmi.registry.*;
//creating an implementing class
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface
{
String name;
public RemoteObject (String name) throws RemoteException
{
super();
this.name = name;
}
public String message (String message) throws RemoteException
{
String rs = "My name is:" + name + ",thanks for your message:" + message;
System.out.println ("Returning:" + rs);
return "My name is:" + name + ",thanks for your message:" + message;
}
public static void main (String args[])
{
try{
//create the registry on port 1099
Registry reg = java.rmi.registry.LocateRe
String myName = "ServerTest";
RemoteObject theServer = new RemoteObject (myName);
Naming.rebind(myName, theServer);
System.out.println ("Ready to continue");
}
catch (Exception e)
{
System.out.println ("an exception occured while creating server"+ e);
}
}//end of main
}//end of class
the RMI client (which is actually a servlet):
--------------------------
import java.io.*;
import javax.servlet.*;
import java.rmi.*;
import java.rmi.registry.*;
import javax.servlet.http.*;
public class RemoteClient implements Servlet {
private ServletConfig config;
private String serverString;
RemoteInterface server;
public void init (ServletConfig config) throws ServletException {
this.config = config;
try{
Registry registry = LocateRegistry.getRegistry
RemoteInterface server = (RemoteInterface)registry.
serverString = server.message("Hello there");
System.out.println( "The server says:\n " + serverString);
}
catch (Exception e)
{
System.out.println("Error while performing RMI");
} }
}//end of init
public void destroy() {}
public ServletConfig getServletConfig() {
return config;
}//end of destroy
public String getServletInfo() {
return "A Simple servlet";
}//end of getServletInfo
public void service (ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType ("text/html");
PrintWriter out = res.getWriter();
out.println ( "<html>" );
out.println ( "<head>" );
out.println ( "<title>A Sample Servlet</title>" );
out.println ( "</head>" );
out.println ( "<body>" );
out.println ( "<h1>A Servlet as a RemoteClient</h1>" );
out.println ( "<h1>The server says:\n" +serverString+ "</h1>");
out.println ( "</body>" );
out.println ( "</html>" );
out.close();
}//end of service
}
ASKER
thanks everyone. I checked the examples from the book and I changed few lines in my code but still it doesn't work and throws the exception. therefor I enclose my code and if you can tell me what's wrong with it, I'll be gratefull.
the RMI server:
----------------
import java.rmi.*;
import java.rmi.server.UnicastRem oteObject;
import java.rmi.registry.*;
//creating an implementing class
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface
{
String name;
public RemoteObject (String name) throws RemoteException
{
super();
this.name = name;
}
public String message (String message) throws RemoteException
{
String rs = "My name is:" + name + ",thanks for your message:" + message;
System.out.println ("Returning:" + rs);
return "My name is:" + name + ",thanks for your message:" + message;
}
public static void main (String args[])
{
try{
//create the registry on port 1099
Registry reg = java.rmi.registry.LocateRe gistry.cre ateRegistr y(1099);
String myName = "ServerTest";
RemoteObject theServer = new RemoteObject (myName);
Naming.rebind(myName, theServer);
System.out.println ("Ready to continue");
}
catch (Exception e)
{
System.out.println ("an exception occured while creating server"+ e);
}
}//end of main
}//end of class
the RMI client (which is actually a servlet):
-------------------------- ---------- -
import java.io.*;
import javax.servlet.*;
import java.rmi.*;
import java.rmi.registry.*;
import javax.servlet.http.*;
public class RemoteClient implements Servlet {
private ServletConfig config;
private String serverString;
RemoteInterface server;
public void init (ServletConfig config) throws ServletException {
this.config = config;
try{
Registry registry = LocateRegistry.getRegistry ();
RemoteInterface server = (RemoteInterface)registry. lookup("Se rverTest") ;
serverString = server.message("Hello there");
System.out.println( "The server says:\n " + serverString);
}
catch (Exception e)
{
System.out.println("Error while performing RMI");
} }
}//end of init
public void destroy() {}
public ServletConfig getServletConfig() {
return config;
}//end of destroy
public String getServletInfo() {
return "A Simple servlet";
}//end of getServletInfo
public void service (ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType ("text/html");
PrintWriter out = res.getWriter();
out.println ( "<html>" );
out.println ( "<head>" );
out.println ( "<title>A Sample Servlet</title>" );
out.println ( "</head>" );
out.println ( "<body>" );
out.println ( "<h1>A Servlet as a RemoteClient</h1>" );
out.println ( "<h1>The server says:\n" +serverString+ "</h1>");
out.println ( "</body>" );
out.println ( "</html>" );
out.close();
}//end of service
}
the RMI server:
----------------
import java.rmi.*;
import java.rmi.server.UnicastRem
import java.rmi.registry.*;
//creating an implementing class
public class RemoteObject extends UnicastRemoteObject implements RemoteInterface
{
String name;
public RemoteObject (String name) throws RemoteException
{
super();
this.name = name;
}
public String message (String message) throws RemoteException
{
String rs = "My name is:" + name + ",thanks for your message:" + message;
System.out.println ("Returning:" + rs);
return "My name is:" + name + ",thanks for your message:" + message;
}
public static void main (String args[])
{
try{
//create the registry on port 1099
Registry reg = java.rmi.registry.LocateRe
String myName = "ServerTest";
RemoteObject theServer = new RemoteObject (myName);
Naming.rebind(myName, theServer);
System.out.println ("Ready to continue");
}
catch (Exception e)
{
System.out.println ("an exception occured while creating server"+ e);
}
}//end of main
}//end of class
the RMI client (which is actually a servlet):
--------------------------
import java.io.*;
import javax.servlet.*;
import java.rmi.*;
import java.rmi.registry.*;
import javax.servlet.http.*;
public class RemoteClient implements Servlet {
private ServletConfig config;
private String serverString;
RemoteInterface server;
public void init (ServletConfig config) throws ServletException {
this.config = config;
try{
Registry registry = LocateRegistry.getRegistry
RemoteInterface server = (RemoteInterface)registry.
serverString = server.message("Hello there");
System.out.println( "The server says:\n " + serverString);
}
catch (Exception e)
{
System.out.println("Error while performing RMI");
} }
}//end of init
public void destroy() {}
public ServletConfig getServletConfig() {
return config;
}//end of destroy
public String getServletInfo() {
return "A Simple servlet";
}//end of getServletInfo
public void service (ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType ("text/html");
PrintWriter out = res.getWriter();
out.println ( "<html>" );
out.println ( "<head>" );
out.println ( "<title>A Sample Servlet</title>" );
out.println ( "</head>" );
out.println ( "<body>" );
out.println ( "<h1>A Servlet as a RemoteClient</h1>" );
out.println ( "<h1>The server says:\n" +serverString+ "</h1>");
out.println ( "</body>" );
out.println ( "</html>" );
out.close();
}//end of service
}
have u checked the Example pointed ?
ASKER
what do you mean "the example pointed"?
ASKER
I have checked this example and as I said before - I changed a few lines according to this example but it still doesn't work and that's why I sent to you my code so you can tell me what's wrong with it.
ASKER
by the way, the following is the full stacktrace:
:java.rmi.UnmarshalExcepti on: error unmarshalling return; nested exception is
java.lang.ClassNotFoundExc eption: RemoteObject_Stub
java.lang.ClassNotFoundExc eption: RemoteObject_Stub)
at java.io.ObjectInputStream. inputObjec t(ObjectIn putStream. java:981)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:369)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:232)
at com.sun.rmi.registry.Regis tryImpl_St ub.lookup( Unknown Source)
at RemoteClient.initRemoteCli ent.java:2 1
)
at com.sun.web.core.ServletWr apper.load Servlet(Se rvletWrapp er.java:91
at com.sun.web.core.ServletWr apper.hand leRequest( ServletWra pper.java: 109)
)
at com.sun.web.core.InvokerSe rvlet.serv ice(Invoke rServlet.j ava:169)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 840
at com.sun.web.core.ServletWr apper.hand leRequest( ServletWra pper.java: 140)
)at com.sun.web.core.Context.h andleReque st(Context .java:375)
at com.sun.web.server.Connect ionHandler .run(Conne ctionHandl er.java:13 5
at(Context.java:375)
at com.sun.web.server.Connect ionHandler .run(Conne ctionHandl er.java:13 5)
:java.rmi.UnmarshalExcepti
java.lang.ClassNotFoundExc
java.lang.ClassNotFoundExc
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at com.sun.rmi.registry.Regis
at RemoteClient.initRemoteCli
)
at com.sun.web.core.ServletWr
at com.sun.web.core.ServletWr
)
at com.sun.web.core.InvokerSe
at javax.servlet.http.HttpSer
at com.sun.web.core.ServletWr
)at com.sun.web.core.Context.h
at com.sun.web.server.Connect
at(Context.java:375)
at com.sun.web.server.Connect
Hi Twg,
Hav you followed all the steps in sequnece.
1.Compiling Remote Interface.(Which extends Remote interface )
2.Compiling Remote Server (which implements that remote interface)
3.Using rmic for RemoteServer.
4.Copying the stub and remote interface to client side.
5.Compiling Remote Client
6.Startingf RMI registry on server side.
7.Running RMI client.
Error:_
From the stack trace, it is clear that
Stub is not presented on Client side.
That's all.
Best of luck
Hav you followed all the steps in sequnece.
1.Compiling Remote Interface.(Which extends Remote interface )
2.Compiling Remote Server (which implements that remote interface)
3.Using rmic for RemoteServer.
4.Copying the stub and remote interface to client side.
5.Compiling Remote Client
6.Startingf RMI registry on server side.
7.Running RMI client.
Error:_
From the stack trace, it is clear that
Stub is not presented on Client side.
That's all.
Best of luck
ASKER
Thank you vey much ravindra76. you were right. Th estub was missing on the client side.
Thank you very much again.
Thank you very much again.
Hey Ravi answerd and u gave me points!
Man he should have got those,
Ravi if he doesnt post it due to any reason I will ask Customer Service to post a dummy question for you.
Regards
Man he should have got those,
Ravi if he doesnt post it due to any reason I will ask Customer Service to post a dummy question for you.
Regards
Hi Amit,
No Problem.
Main goal: Clarifying the doubt was successful.
Thank you
No Problem.
Main goal: Clarifying the doubt was successful.
Thank you
no no,
thanks for reminding me but I will right now send a mail to IanB or Linda.
thanks for reminding me but I will right now send a mail to IanB or Linda.
ASKER
Thank you all again and I hope the points will go to the right person.
The problem was that Ravindra76 answered in a "Comment" and not in the "Answer" textbox.
sorry for the mess.
The problem was that Ravindra76 answered in a "Comment" and not in the "Answer" textbox.
sorry for the mess.
dont worry I have sent a Email to IanB right now.
he would most probably post a 50 point question in Java for ravindra76.
he would most probably post a 50 point question in Java for ravindra76.
ASKER
Thanks again and bye for now(untill the next question).
Hi,
I have been directed to this question by mbormann regarding allocating points. To rectify this, I have posted a question (Q.10250143) for ravindra76 in this topic area, so he can claim the points for this question.
Ian
Community Support @ Experts Exchange
I have been directed to this question by mbormann regarding allocating points. To rectify this, I have posted a question (Q.10250143) for ravindra76 in this topic area, so he can claim the points for this question.
Ian
Community Support @ Experts Exchange
'Hi Amit,
No Problem.
Main goal: Clarifying the doubt was successful.
Thank you'
that's nice :)
No Problem.
Main goal: Clarifying the doubt was successful.
Thank you'
that's nice :)