This repository contains three modular Java projects demonstrating the use of Enterprise JavaBeans (EJB), Remote Method Invocation (RMI), and Web Integration with JSP. The projects work together to create a simple application for currency conversion, featuring a modular design with a Core module, an AppModule (EJB implementation), and a WebApp (Servlet-based web interface).
- Project Structure
- Core Module
- AppModule (EJB)
- WebApp
- Running the Application
- Technologies Used
- License
Java-EJB-RMI/
├── Core/
│ ├── src/main/java/lk/jiat/web/core/remote/CurrencyConverter.java
│ └── pom.xml
│
├── AppModule/
│ ├── src/main/java/lk/jiat/web/ejb/CurrencyConverterBean.java
│ └── pom.xml
│
└── WebApp/
├── src/main/java/lk/jiat/web/servlet/Home.java
├── src/main/webapp/index.jsp
└── pom.xml
The Core module defines the CurrencyConverter interface used for the conversion between USD and LKR. This is a remote interface, implemented by the AppModule.
CurrencyConverter.java: Defines the remote interface for currency conversion.
package lk.jiat.web.core.remote;
import jakarta.ejb.Remote;
@Remote
public interface CurrencyConverter {
double convertToLKR(double amount);
double convertToUSD(double amount);
}The AppModule implements the CurrencyConverter interface and contains the actual business logic for currency conversion. The CurrencyConverterBean is a stateless EJB bean that performs the conversion and manages the lifecycle of the bean.
CurrencyConverterBean.java: Implements the currency conversion methods and lifecycle methods.
package lk.jiat.web.ejb;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.ejb.Stateless;
import lk.jiat.web.core.remote.CurrencyConverter;
@Stateless
public class CurrencyConverterBean implements CurrencyConverter {
private static final double USD_TO_LKR_RATE = 300.00;
public CurrencyConverterBean() {
System.out.println("CurrencyConverterBean Constructor called: " + this.hashCode());
}
@PostConstruct
public void init() {
System.out.println("CurrencyConverterBean Created");
}
@PreDestroy
public void destroy() {
System.out.println("CurrencyConverterBean Destroyed");
}
@Override
public double convertToLKR(double amount) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return amount * USD_TO_LKR_RATE;
}
@Override
public double convertToUSD(double amount) {
return amount / USD_TO_LKR_RATE;
}
}The WebApp is a simple Servlet-based web application that integrates with the Core and AppModule to display the currency conversion result. It uses the CurrencyConverter EJB to convert a fixed amount of USD to LKR and displays the result on a webpage.
Home.java: The main servlet that interacts with the CurrencyConverter EJB and displays results.index.jsp: A simple JSP page displaying the results of the conversion.
package lk.jiat.web.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lk.jiat.web.core.remote.CurrencyConverter;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.IOException;
@WebServlet("/home")
public class Home extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("<h1>This is Home Page</h1>");
try {
InitialContext context = new InitialContext();
CurrencyConverter converter = (CurrencyConverter) context.lookup("java:global/app-module/CurrencyConverterBean!lk.jiat.web.core.remote.CurrencyConverter");
double lkr = converter.convertToLKR(1200);
response.getWriter().println("LKR: " + lkr);
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
}This is Home Page
LKR: 360000.0- Java 11 (or newer)
- Maven
- GlassFish (or any JEE compatible server)
-
Build the Projects:
-
In each project directory (
Core,AppModule,WebApp), run:mvn clean install
-
-
Deploy
AppModuleon GlassFish:- Deploy the
AppModule/target/app-module.warto your GlassFish server.
- Deploy the
-
Start the WebApp:
- Deploy the
WebApp/target/webapp.warto your server.
- Deploy the
-
Access the WebApp:
- Open your browser and visit
http://localhost:8080/webapp/hometo view the results.
- Open your browser and visit
- Java 11
- Jakarta EE (formerly Java EE) - for EJB, Servlets, and JNDI
- Maven - for build automation
- GlassFish - as the application server for running EJBs
- JSP - for the web interface
This project is licensed under the MIT License - see the LICENSE file for details.
