aspose file tools*
The moose likes Distributed Java and the fly likes jsp vs rmi Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "jsp vs rmi" Watch "jsp vs rmi" New topic
Author

jsp vs rmi

Rajasekhar Pentakota
Ranch Hand

Joined: Aug 28, 2007
Posts: 38

How can I call the rmi methods from jsp.

After running the following jsp shows the error given below the code.

=======================================================================
<html>
<head>
<title>JSP and RMI</title>
</head>
<body>
<%@ page import='java.rmi.*' %>
<%@ page import='java.rmi.Naming' %>
<%@ page import='server1.*' %>
<%@ page import='interface1.*' %>
<%@ page import='java.lang.*' %>

<%
try
{
RajaService rp=(RajaService)Naming.lookup("rmi://localhost/RajaService");
out.println("5 sqare is : "+rp.sqare(5));
}
catch(Exception ex){out.println(ex.getMessage());}

%>

</body>
</html>

==========================================================================
Error is
====================
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 12 in the jsp file: /RSCJSP.jsp
Generated servlet error:
The method sqare(int) is undefined for the type RajaService


org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.

==========================================================


Thanks
Raja
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42264
    
  64
As pointed out to you elsewhere, it's much better design to do this kind of thing in a backing bean or a servlet than in a JSP.

Anyway, does the RajaService class have a square(int) method?


Ping & DNS - my free Android networking tools app
Rajasekhar Pentakota
Ranch Hand

Joined: Aug 28, 2007
Posts: 38

yes RajaService has sqare method.

The followingis the Interface RajaService


======================
package interface1;

import java.rmi.RemoteException;

//
// PowerService Interface
//
// Interface for a RMI service that calculates powers
//
public interface RajaService extends java.rmi.Remote
{
// Calculate the square of a number
public int square ( int number )
throws RemoteException;

}
============================
The following code is Server class which implements the RajaService

========================
package server1;

import java.math.*;
import java.rmi.*;
import java.rmi.server.*;
import interface1.RajaService;


public class RajaServiceServer extends UnicastRemoteObject implements RajaService
{
public RajaServiceServer() throws RemoteException
{
super();
}

// Calculate the square of a number
public int square ( int number )
throws RemoteException
{
return number * number;
}


public static void main ( String args[] ) throws Exception
{
// Assign a security manager, in the event that dynamic
// classes are loaded
if (System.getSecurityManager() == null)
System.setSecurityManager ( new RMISecurityManager() );

RajaServiceServer svr = new RajaServiceServer();

Naming.bind ("RajaService", svr);

System.out.println ("Service bound....");
}
}
=========================================================
Rajasekhar Pentakota
Ranch Hand

Joined: Aug 28, 2007
Posts: 38

The bean class code is

=====================================================

package client1;

import java.rmi.*;
import java.io.*;
import server1.RajaServiceServer;
import interface1.RajaService;

public class RajaJspServiceClient
{

public int getsq(int sq) throws Exception
{
if (System.getSecurityManager() == null)
{
System.setSecurityManager
(new RMISecurityManager());
}

// Call registry for PowerService
RajaService service = (RajaService) Naming.lookup
("rmi://localhost/RajaService");

return service.square(sq);

}

}
=====================================================

and the jsp code is

========================================================
<html>
<head>
<title>JSP and RMI</title>
</head>
<body>
<%@ page import='java.rmi.*' %>
<%@ page import='java.rmi.Naming' %>
<%@ page import='server1.*' %>
<%@ page import='interface1.*' %>
<%@ page import='java.lang.*' %>

<jsp:useBean id="counter" scope="request" class="client1.RajaJspServiceClient" />

<%
out.println("Welcome...");
out.println("The sqare of 10 is : " + counter.getsq(10));
%>



</body>
</html>

====================================================================
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp vs rmi