Share
Explore

EJB Number Guessing Game

Here is the complete lab for creating and deploying a Number Guessing Game EJB application on the GlassFish server.

EJB Lab: Number Guessing Game

Objective

Develop and deploy a simple number guessing game as a Jakarta EE application using GlassFish. The application will prompt the user to guess a number, and the server will respond with "Higher," "Lower," or "Correct."

Step 1: Verify GlassFish Installation

Ensure the GlassFish server is installed and running:
cd /path/to/glassfish/bin
./asadmin start-domain

Open your browser and navigate to:
http://localhost:4848
Confirm the GlassFish admin console is accessible.

Step 2: Create the Maven Project

Open a Terminal:
Navigate to your projects directory:
mkdir NumberGuessingGame
cd NumberGuessingGame
Generate a Maven Project: Run the following Maven command to create the project structure:

mvn archetype:generate -DgroupId=com.example.ejb -DartifactId=NumberGuessingGame -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Verify the Project Structure: Ensure the following structure is created:

NumberGuessingGame/
├── pom.xml
├── src/
├── main/
├── java/
├── webapp/
├── WEB-INF/

Step 3: Implement the Game Logic

Create the EJB (HelloBean.java):
File Name: HelloBean.java
Location: src/main/java/com/example/ejb/
Content:
package com.example.ejb;

import jakarta.ejb.Stateless;

@Stateless
public class HelloBean {
public String guessNumber(int guess, int target) {
if (guess < target) {
return "Higher";
} else if (guess > target) {
return "Lower";
} else {
return "Correct! You guessed the number.";
}
}
}

Create the REST Endpoint (NumberGuessingGameResource.java):
File Name: NumberGuessingGameResource.java
Location: src/main/java/com/example/ejb/
Content:
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("/game")
public class NumberGuessingGameResource {

@Inject
private HelloBean helloBean;

private final int targetNumber = 7; // Fixed target number for simplicity

@GET
public Response playGame(@QueryParam("guess") int guess) {
String result = helloBean.guessNumber(guess, targetNumber);
return Response.ok(result).build();
}
}

Optional: Create an Index Page (index.html):
File Name: index.html
Location: src/main/webapp/
Content:
<!DOCTYPE html>
<html>
<head>
<title>Number Guessing Game</title>
</head>
<body>
<h1>Welcome to the Number Guessing Game!</h1>
<form method="GET" action="api/game">
<label for="guess">Enter your guess (1-10):</label>
<input type="number" name="guess" id="guess" required>
<button type="submit">Submit</button>
</form>
</body>
</html>

Step 4: Update the pom.xml File

Open pom.xml: Ensure it includes the Jakarta EE dependency:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.ejb</groupId>
<artifactId>NumberGuessingGame</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

Step 5: Build the Application

Run Maven Build Command:
mvn clean package
Verify Build Output:
Check the target/ directory for the .war file:
NumberGuessingGame/target/NumberGuessingGame.war

Step 6: Deploy the Application

Deploy Using Command Line:
asadmin deploy target/NumberGuessingGame.war
Verify Deployment:
Open the GlassFish Admin Console at http://localhost:4848.
Navigate to Applications and ensure NumberGuessingGame is listed as deployed.

Step 7: Test the Application

Access the Game in a Browser:
Open your browser and navigate to:
http://localhost:8080/NumberGuessingGame/api/game?guess=5

You should receive one of the following responses:
"Higher"
"Lower"
"Correct! You guessed the number."
Test Using Curl:
curl "http://localhost:8080/NumberGuessingGame/api/game?guess=5"

Step 8: Review and Clean Up

Stop the GlassFish Server:
./asadmin stop-domain
Reflect on Key Concepts:
Dependency Injection: Observe how HelloBean is injected into NumberGuessingGameResource using @Inject.
Separation of Concerns: Note the clear division between the EJB logic and the REST endpoint.
This lab includes all necessary files and steps for a complete EJB application.
Each file's purpose, location, and content are explicitly outlined to ensure a smooth experience.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.