6 Replies Latest reply on Jun 19, 2003 9:00 PM by 843835

    RMI client in jsp

    843835
      Hi,

      I have a simple java RMI server running and I am able to run a simple java RMI client to access it. Can somebody guide me how to write a jsp client for it. Is it possible to access java rmi server from a jsp rmi client?

      My java client is :


      import java.rmi.*;
      import java.rmi.registry.*;

      public class StockMarketClient {

      public static void main(String[] args) {
      try {
      if(System.getSecurityManager() == null) {
      System.setSecurityManager( new RMISecurityManager() );
      }
      StockMarket market = (StockMarket)Naming.lookup("rmi://localhost/NASDAQ");
      System.out.println( "The price of MY COMPANY is "
      + market.get_price("MY_COMPANY") );
      }
      catch( Exception e ) {
      System.out.println( e );
      }
      }
      }



      I tried putting this code in a bean like :


      import java.rmi.*;
      import java.rmi.registry.*;

      public class BeanStockMarketClient implements java.io.Serializable
      {

      public void getRMIClientStuff()
      {
           String res = "";
           try
           {
                if(System.getSecurityManager() == null)
                {
                     System.setSecurityManager( new RMISecurityManager() );
                }
                StockMarket market = (StockMarket)Naming.lookup("rmi://localhost/NASDAQ");
                System.out.println( "The price of MY COMPANY is " + market.get_price("MY_COMPANY") );
                float num = market.get_price("MY_COMPANY");
                }
                catch( Exception e )
                {
                System.out.println( e );
                }
           }
           
           
      }


      Put the class file in /web-inf/classes folder and tried :


      <jsp:useBean id="rmi" class="BeanStockMarketClient" ></jsp:useBean>
      <%
      rmi.getRMIClientStuff();
      %>


      But I get an exception that means it is not able to find bean.


      i also tried :


      <%@ page import="java.rmi.*" %>
      <HTML>
      <HEAD>

      <TITLE>
      RMI Test
      </TITLE>
      </HEAD>
      <BODY>
      <%
           if(System.getSecurityManager() == null)
                {
                     System.setSecurityManager( new RMISecurityManager() );
                }

      StockMarket ref = null;
      try {
      ref = (StockMarket)Naming.lookup("rmi://localhost/NASDAQ");
      } catch (Exception ex) {
      ex.printStackTrace();
      }
      %>
      <H2>The following output is from StocketMarketServer :</H2><P><%=ref.get_price("MY_COMPANY")%></P>
      </BODY>
      </HTML>


      But then I get :

      C:\tomcat\Tomcat 4.1\work\Standalone\localhost\_\myjsp\testRmi2_jsp.java:55: cannot resolve symbol
      symbol : class StockMarket
      location: class org.apache.jsp.testRmi2_jsp
      StockMarket ref = null;
      ^


      ...

      and other exceptions.


      Please help me in solving this problem.

        • 1. Re: RMI client in jsp
          843835
          You have to import the classes to use them. In the last example that you posted there is no import statement for the StocketMarketServer class.

          • 2. Re: RMI client in jsp
            843835
            The default package for servlets in Weblogic is jsp_servlet. I guess there is something similar in Tomcat, so you need to import any classes that you need.
            • 3. Re: RMI client in jsp
              843835
              I tried the import in second example but now I am getting the security exception in tomcat window and null pointer exception in browser.


              java.security.AccessControlException: access denied (java.util.PropertyPermission catalina.base read)
              at java.security.AccessControlContext.checkPermission(AccessControlContext.java:267)
              at java.security.AccessController.checkPermission(AccessController.java:394)
              at java.lang.SecurityManager.checkPermission(SecurityManager.java:540)
              at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1273)
              at java.lang.System.getProperty(System.java:555)
              at org.apache.catalina.startup.HostConfig.appBase(HostConfig.java:386)
              at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:404)
              at org.apache.catalina.startup.HostConfig.run(HostConfig.java:1012)
              at java.lang.Thread.run(Thread.java:479)


              Any ideas how to solve this?










              • 4. Re: RMI client in jsp
                843835
                Till now I haven't done any change in tomcat files regarding rmi. Please tell me what files I need to change for tomcat 4.1.18. And do I need to change the server command . Currently I am using :


                java -Djava.security.policy=policy.all StockMarketServer

                and my policy file is is my src dir as:

                policy.all


                grant {
                permission java.security.AllPermission "", "";
                };

                What more changes do I need?
                • 5. Re: RMI client in jsp
                  843835
                  That means that you are finding the classes now. Here is a link that may help you resolve the permissions problem you now have:

                  http://patriot.net/~tvalesky/easyrmi.html
                  • 6. Re: RMI client in jsp
                    843835
                    Do I need to make any change in catalina.policy? And how should I start my server? Currently the policy file that i specify is in dir where server is started from. When I start tomcat with -security option I get this exception in browser :

                    java.lang.NullPointerException
                         at org.apache.jsp.testRmi2_jsp._jspService(testRmi2_jsp.java:69)
                         at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
                         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
                         at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:

                    ..

                    and in tomcat window :


                    Starting service Tomcat-Standalone
                    Apache Tomcat/4.1.18
                    java.security.AccessControlException: access denied (java.net.SocketPermission localhost resolve
                    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:267)
                    at java.security.AccessController.checkPermission(AccessController.java:394)
                    at java.lang.SecurityManager.checkPermission(SecurityManager.java:540)
                    at java.lang.SecurityManager.checkConnect(SecurityManager.java:1037)
                    at java.net.InetAddress.getAllByName0(InetAddress.java:554)
                    at java.net.InetAddress.getAllByName0(InetAddress.java:535)
                    at java.net.InetAddress.getByName(InetAddress.java:444)
                    at java.net.Socket.<init>(Socket.java:95)
                    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.ja
                    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.ja
                    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:494)
                    at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
                    at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:169)
                    at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:313)


                    ....