Call RMI Service using Servlet Application
In this example, I will show you how to call RMI service using servlet application. This example was tested with Apache Tomcat environment and output has been shared in the same post.
1. Developing RMI Service
In this part, we are going to develop the RMI Service, Which can be called by the servlet application.
Project Structure
Creating the Interface
package com.dineshkrish.rmi; import java.rmi.Remote; import java.rmi.RemoteException; /** * * @author Dinesh Krishnan * */ public interface Authentication extends Remote { public boolean authenticate(String userName, String password) throws RemoteException; }
Implementing the Interface
package com.dineshkrish.rmi; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; /** * * @author Dinesh Krishnan * */ public class AuthenticationImpl extends UnicastRemoteObject implements Authentication { protected AuthenticationImpl() throws RemoteException { } @Override public boolean authenticate(String userName, String password) throws RemoteException { if ((userName != null && !userName.isEmpty()) && (password != null && !password.isEmpty())) { if ((userName.equalsIgnoreCase("admin")) && (password.equalsIgnoreCase("admin"))) { return true; } } return false; } }
Publishing the Service
package com.dineshkrish.rmi; import java.rmi.AlreadyBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; /** * * @author Dinesh Krishnan * */ public class Publish { // Port Number you can change based on your system availability private static final int PORT = 5252; public static void main(String[] args) { try { // Defining Object Authentication authentication = new AuthenticationImpl(); // Creating RMI Registry with Port Registry registry = LocateRegistry.createRegistry(PORT); // Binding the Object registry.bind("auth", authentication); System.out.println("Authentication Service running at " + PORT + " port..."); } catch (RemoteException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (AlreadyBoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Run it
Authentication Service running at 5252 port…
2. Developing the Servlet Application
Project Structure
Servlet Class
package com.dineshkrish.controller; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dineshkrish.model.AuthenticationService; /** * * @author Dinesh Krishnan * */ public class LoginController extends HttpServlet { private String userName; private String password; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { userName = request.getParameter("userName"); password = request.getParameter("password"); if ((userName != null && !userName.isEmpty()) && password != null && !password.isEmpty()) { AuthenticationService service = new AuthenticationService(); boolean status = service.verifyUser(userName, password); if(status) { RequestDispatcher dispatcher = request.getRequestDispatcher("success.html"); dispatcher.forward(request, response); } else { RequestDispatcher dispatcher = request.getRequestDispatcher("error.html"); dispatcher.forward(request, response); } } else { RequestDispatcher dispatcher = request.getRequestDispatcher("error.html"); dispatcher.forward(request, response); } } }
Calling the RMI Service
package com.dineshkrish.model; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.NotBoundException; import java.rmi.RemoteException; import com.dineshkrish.rmi.Authentication; /** * * @author Dinesh Krishnan * */ public class AuthenticationService { private final String SERVICE_URL = "rmi://localhost:5252/auth"; public boolean verifyUser(String userName, String password) { try { Authentication authentication = (Authentication)Naming.lookup(SERVICE_URL); return authentication.authenticate(userName, password); } catch (MalformedURLException | RemoteException | NotBoundException e) { System.out.println(e.getMessage()); e.printStackTrace(); } return false; } }
Developing Views Component
login.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Login Application</title> </head> <body> <form action="login" method="post"> <h1>Login Page!!!</h1> <table cellpadding="5" cellspacing="5"> <tr> <td><label>Username</label></td> <td><input type="text" name="userName" /></td> </tr> <tr> <td><label>Password</label></td> <td><input type="password" name="password" /></td> </tr> <tr> <td></td> <td align="right"><input type="submit" value="Login" /></td> </tr> </table> </form> </body> </html>
success.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Success Page | Login Servlet + RMI Example</title> </head> <body> <h1>You are logged in successfully...</h1> </body> </html>
error.html
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Error Page | Login Servlet + RMI Example</title> </head> <body> <h1>Your Username / Password is incorrect...</h1> </body> </html>
Configuration File web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>LoginClient</display-name> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>Login</servlet-name> <servlet-class>com.dineshkrish.controller.LoginController</servlet-class> </servlet> <servlet-mapping> <servlet-name>Login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>
Download Source Code
1. RMI Service download here
2. Servlet Client download here
Output
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet