Where this activity fits in our progression through this Course:
Next week we will introduce JPA Java persistance architecture in the container of an EJB.
JPA will enable us to connect with a Database.
We will experiment with both SQL and Json databases.
=> All of these things build up to our ultimate goal of delivering the class project which will be a Service Oriented Architect E Commerce Website.
Future class activities
SOAP (Assignment) : compare and contrast SOAP and REST
Preamble: From Servlets to Enterprise JavaBeans
As we progress through CSD-4464, we're making a significant architectural leap from basic web components (Servlets) to enterprise-grade distributed components (EJBs).
This transition mirrors the evolution of enterprise Java applications and directly addresses several of our course learning outcomes, particularly CLO 3 (using MVC architecture in multi-tier environments) and CLO 4 (database manipulation using CRUD operations).
Why GlassFish Instead of Tomcat?
While Apache Tomcat EE served us well for Servlet-based applications, Enterprise JavaBeans require a fully Jakarta EE (formerly J2EE) compliant application server.
Here's why:
1. **Container Services**:
- Tomcat provides basic container services for web components (Servlets, JSP)
- GlassFish provides comprehensive container services including:
- Transaction management
- Security
- Resource pooling
- Load balancing
- State management
2. **Component Model**:
- Servlets: Single-tier web components
- EJBs: Multi-tier distributed components with:
- Instance pooling
- Declarative transaction management
- Security integration
- Dependency injection
- Remote method invocation
3. **Architecture Capabilities**:
- Tomcat: Web container only
- GlassFish: Complete application server with:
- EJB container
- Web container
- JPA provider
- JMS provider
- Full clustering support
The Power of Enterprise JavaBeans
EJBs bring several unique capabilities to your development toolkit:
1. **Business Logic Isolation**:
- Separate business logic from presentation
- Promote reusability across different clients
- Enable modular application design
2. **Declarative Services**:
- Transaction management through annotations
- Security through deployment descriptors
- Resource management handled by container
3. **Scalability Features**:
- Instance pooling
- Load balancing
- Failover support
- State management
Assignment Instructions
Deliverables
1. **Source Code**:
- Complete Maven project structure
- Properly implemented HelloBean (EJB)
- RESTful service implementation
- All configuration files
2. **Documentation**:
- Step-by-step deployment guide
- Screenshots of:
- GlassFish admin console showing deployment
- Application output
- Test results
- Brief explanation of EJB concepts implemented
3. **Deployment Package**:
- WAR file
- Deployment descriptor
- Any additional configuration files
### Submission Format
1. Create a ZIP file containing:
- Source code directory
- Documentation (PDF)
- Deployment package
2. File naming convention:
- Format: `CSD4464_Lab4_StudentID_LastName`
- Example: `CSD4464_Lab4_123456_Smith`
3. Submit through Moodle assignment portal
Learning Outcome Alignment
This lab specifically addresses the following course learning outcomes:
- CLO 3.1: "Build and manage a Spring web application"
- CLO 3.4: "Implement a variety of Spring MVC features"
- CLO 3.9: "Implement Contexts and Dependency Injection for the discovery of EJBs"
Late submissions will incur a 10% penalty per day up to a maximum of 3 days. No submissions will be accepted after the third day without prior arrangement with the instructor.
Project Delivery Steps:
Enterprise JavaBeans (EJB) Lab Using GlassFish Server and Sublime Text Editor
This lab will guide you through creating, deploying, and testing a simple Enterprise JavaBeans (EJB) application on the GlassFish server. {A step up from our previous Apache EE Server}
We will use Sublime Text Editor to focus on the core principles of Jakarta EE without the complexities of a sophisticated IDE.
Lab Objective
To build and deploy an EJB-based Jakarta EE application using GlassFish.
By the end of this lab, you will:
Create a simple stateless EJB (HelloBean) that processes a greeting. Create a RESTful service (HelloResource) that uses dependency injection to call the EJB. Deploy the application on GlassFish Server. Test the application using a web browser or a tool like curl or postman. Prerequisites
GlassFish server installed and running. {We need to learn how to install and work with GlassFish.} Maven installed and configured. {We need to learn how to install and work with Maven: A build tool.} Step-by-Step Instructions
Step 1: Verify Your Environment
Open a terminal, navigate to the GlassFish installation directory, and start the server: cd /path/to/glassfish/bin
./asadmin start-domain
Open http://localhost:4848 in a browser to ensure the server is running. Step 2: Set Up the Project Directory Structure
Create the Project Folder: Open a terminal, create a folder for your project, and navigate to it: mkdir SimpleEJBProject
cd SimpleEJBProject
Generate a Maven project with the following command: mvn archetype:generate -DgroupId=com.example.ejb -DartifactId=SimpleEJB -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Verify that the SimpleEJB folder has been created with the following structure: css
Copy code
SimpleEJB/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/
│ │ └── webapp/
│ │ └── WEB-INF/
Step 3: Create EJB and RESTful Service
A. Write HelloBean.java
Navigate to the Java Source Directory: In your terminal, go to the src/main/java/com/example/ejb/ folder: bash
Copy code
mkdir -p src/main/java/com/example/ejb
cd src/main/java/com/example/ejb
Create the HelloBean.java File: Open Sublime Text and create a new file. Save it as HelloBean.java in the src/main/java/com/example/ejb/ directory. package com.example.ejb;
import jakarta.ejb.Stateless;
@Stateless
public class HelloBean {
public String sayHello(String name) {
return "Hello, " + name + "! Welcome to Jakarta EE EJB.";
}
}
This is a stateless EJB that takes a name as input and returns a greeting message. B. Write HelloResource.java
Create the HelloResource.java File: In Sublime Text, create a new file and save it as HelloResource.java in the same src/main/java/com/example/ejb/ directory. package com.example.ejb;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
@Path("/hello")
public class HelloResource {
@Inject
private HelloBean helloBean;
@GET
public Response sayHello(@QueryParam("name") String name) {
String message = helloBean.sayHello(name != null ? name : "Guest");
return Response.ok(message).build();
}
}
This is a RESTful web service that listens at /api/hello and calls HelloBean using dependency injection to process requests. Step 4: Configure pom.xml
Open pom.xml in Sublime Text: Find pom.xml in the project root directory. Add the Jakarta EE Dependency: Add the following dependency inside the <dependencies> tag: <dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>9.1.0</version>
<scope>provided</scope>
</dependency>
Ensure Packaging is Set to create awar file: {Web Archive Resource file} Add the <packaging> tag to specify that the project should be packaged as a WAR file: <packaging>war</packaging>
Step 5: Build the Project
Navigate to the project root and build the project: Ensure the build completes without errors and that the target directory contains SimpleEJB.war. Step 6: Deploy the Application to GlassFish
Deploy Using Command Line: Deploy the WAR file to GlassFish using the asadmin deploy command: ./asadmin deploy target/SimpleEJB.war
Open the GlassFish Admin Console at http://localhost:4848, go to Applications, and confirm that SimpleEJB is listed as a deployed application. Step 7: Test the Application
Access the Application in a Browser: Open your browser and navigate to: http://localhost:8080/SimpleEJB/api/hello?name=Student
Verify the response: "Hello, Student! Welcome to Jakarta EE EJB." Alternatively, use the following command: curl "http://localhost:8080/SimpleEJB/api/hello?name=Student"
Step 8: Clean Up
Stop the GlassFish Server: Stop the server when you’re done: Reflect on how dependency injection allowed HelloResource to use HelloBean seamlessly, and how the GlassFish server managed the application lifecycle. Hand-in Submission
Submit a .zip file containing:
The entire project directory (SimpleEJB), including all source files and the pom.xml. The generated SimpleEJB.war file in the target/ directory. A PDF report documenting: Screenshots of the deployed application in the Admin Console. Browser or curl output showing successful execution. Grading Rubric
This version ensures that all steps are explicitly outlined, keeping the focus on coding and practices, with Sublime Text as the primary tool. Students will experience the core workflow without unnecessary distractions.