Remote deployment and RMI

from Head First Java chapter 18

Bavatharany Mahathevan
4 min readOct 29, 2021

When you have to run your application with another JVM /machine/server, you have to connect them rather than connecting Socket or I/O.

It is about getting a remote object to run the method from another machine. This is Known as RMI (remoter method Invocate).

In other words, In order to get results from a method that is in another machine /another JVM.you to need an object of another machine named a remote object.

It means that object from another heap. But you can not get the reference directly.

So, let's see designing the remote method call

You have to consider creating four things which are client app, client helper server app and server helper. here, The Server app is a remote service and has a method that wants to be invoked by the client.

however, They both are not communicating directly,

So, They have a client helper and server helper.

The client calls a method on the client helper and the client helper send a request to the server helper.

likewise, the server helper receives the request from the client helper (through a Socket connection), unpacks the information about the call, and then invokes the real method on the real service object.

Then the server sends the result to the server helper, The server helper sends back the result to the client helper after that client receives it from the client helper.

  • Here, The RMI provides the client helper and service helper object for you.
  • helper looks like the Real Service. The Client and Server do not know that they are talking indirectly, In other words, They don't know that we have helper objects.
  • So that, The RMI provides the same method to the helper object that you wants to run on the server.
  • So, the client class a method like local method class while client helper call method over the networking.
  • However, the client has to take responsibility for the Exceptions.
  • And, The helper objects use protocols to transfer the information about the remote call.
  • If you have both ends of Java (Java to Java ), you can use the RMI protocol.
  • In RMI, the client helper is known as a stub and the server helper is known as the skeleton.

Making remote services.

The chapter explains a five-step to make remote services. Let's see

Step 1: make a remote interface.

Step 2: make remote implementation.

step 3: Generate the stubs and skeletons using rmic

Step 4: Start the RMI registry (rmiregistry)

Step 5 : Start the remote service

Step 1: make a remote interface.

Step 2 : Extend UnicastRemoteObject makes creating object easily and declare a no-arg constructor for makes sure your superclass also throw the exception.

By calling a static method lookup(), The client gets the stub (client helper ) object from RMI registry(server).

client code

Servlets

Servlets are Java classes that run entirely on(and with ) an HTTP web server.

The web server runs the servlet code when the client‘s request in interacting web search needs the help of a java servlet.

For example: if a client submits information in a web page form, the servlet can process the information, add it into the database, and send back a customized, confirmation response page.

Steps to making and running a servlet

  1. find out where your servlets need to be placed
  2. get the servlets.jar and add it to your classpath
  • as the servlets are not part of the standard java libraries, You have to download them from Java.sun.com or get them from a servlet capable web server and add the servlet jar file into your classpath

3. write a servlet class by extending HTTPservlet like

public class MyServletA extends HttpServlet { … }

4. write an HTML page that invokes your servlet.

to web server starts the servlet and class the appropriate methods like doGET() ,doPOST()

5. make your servlet and HTML page available to your server.

--

--