Skip to content

xCaptain404/java-gradle-webapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Java Gradle Webapp

A fully functional Java web application built with Gradle for build automation, designed to run on Apache Tomcat without a database.

🎯 Project Overview

This project demonstrates:

  • Java Servlet development
  • REST API endpoints
  • Gradle build automation
  • Web application structure and deployment
  • In-memory data management (no database)

πŸ—οΈ Project Structure

java-gradle-webapp/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/
β”‚   β”‚   β”œβ”€β”€ java/
β”‚   β”‚   β”‚   └── com/example/
β”‚   β”‚   β”‚       β”œβ”€β”€ controller/
β”‚   β”‚   β”‚       β”‚   β”œβ”€β”€ HelloServlet.java
β”‚   β”‚   β”‚       β”‚   └── UserServlet.java
β”‚   β”‚   β”‚       β”œβ”€β”€ model/
β”‚   β”‚   β”‚       β”‚   └── User.java
β”‚   β”‚   β”‚       └── service/
β”‚   β”‚   β”‚           └── UserService.java
β”‚   β”‚   └── webapp/
β”‚   β”‚       β”œβ”€β”€ WEB-INF/
β”‚   β”‚       β”‚   └── web.xml
β”‚   β”‚       β”œβ”€β”€ index.html
β”‚   β”‚       └── error.html
β”‚   └── test/
β”‚       └── java/
β”œβ”€β”€ gradle/
β”‚   └── wrapper/
β”‚       β”œβ”€β”€ gradle-wrapper.jar
β”‚       └── gradle-wrapper.properties
β”œβ”€β”€ build.gradle
β”œβ”€β”€ settings.gradle
β”œβ”€β”€ gradlew
β”œβ”€β”€ gradlew.bat
β”œβ”€β”€ .gitignore
└── README.md

πŸ”§ Technologies Used

  • Java 11 - Programming language
  • Gradle 7.5.1 - Build automation and dependency management
  • Apache Tomcat 9 - Servlet container (target deployment)
  • Servlet 4.0 API - Web application framework
  • GSON - JSON serialization/deserialization
  • SLF4J - Logging framework
  • JUnit 4 - Testing framework

πŸ“‹ Prerequisites

  • Java 11 or higher installed
  • Gradle 7.5.1 (or use the included Gradle Wrapper)
  • Apache Tomcat 9 (for deployment)
  • Linux VM with Tomcat installed (for deployment)

πŸš€ Getting Started

1. Clone the Repository

git clone https://github.com/xCaptain404/java-gradle-webapp.git
cd java-gradle-webapp

2. Build the Project

Using Gradle Wrapper:

# Linux/Mac
./gradlew build

# Windows
.\gradlew.bat build

Or with Gradle installed:

gradle build

3. Run Locally with Gretty Plugin

./gradlew appRun

The application will be accessible at: http://localhost:8080

4. Build WAR File

./gradlew war

The WAR file will be created at: build/libs/java-gradle-webapp-1.0.0.war

πŸ’‘ API Endpoints

1. Hello Servlet

GET /hello

Simple servlet that returns HTML response.

Example:

curl http://localhost:8080/hello

2. User API - Get All Users

GET /api/user?action=list

Returns all users in JSON format.

Response:

[
  {
    "id": 1,
    "name": "John Doe",
    "email": "john@example.com",
    "city": "New York",
    "createdAt": "2024-05-22T10:30:00"
  },
  ...
]

3. User API - Get User by ID

GET /api/user?action=get&id=1

Returns a specific user by ID.

Response:

{
  "id": 1,
  "name": "John Doe",
  "email": "john@example.com",
  "city": "New York",
  "createdAt": "2024-05-22T10:30:00"
}

4. User API - Create User

POST /api/user

Creates a new user.

Parameters:

  • name (required) - User's full name
  • email (required) - User's email address
  • city (required) - User's city

Example:

curl -X POST http://localhost:8080/api/user \
  -d "name=Alice Johnson" \
  -d "email=alice@example.com" \
  -d "city=Boston"

Response:

{
  "id": 4,
  "name": "Alice Johnson",
  "email": "alice@example.com",
  "city": "Boston",
  "createdAt": "2024-05-22T10:35:00"
}

πŸ“¦ Deployment to Linux Tomcat VM

1. Build the WAR file

./gradlew war

2. Copy WAR to Tomcat

# On your development machine
scp build/libs/java-gradle-webapp-1.0.0.war user@your-vm-ip:/path/to/tomcat/webapps/

# Or rename for root deployment
scp build/libs/java-gradle-webapp-1.0.0.war user@your-vm-ip:/path/to/tomcat/webapps/ROOT.war

3. Restart Tomcat

# SSH into the VM
ssh user@your-vm-ip

# Navigate to Tomcat
cd /path/to/tomcat

# Stop Tomcat
bin/shutdown.sh

# Start Tomcat
bin/startup.sh

# Check logs
tail -f logs/catalina.out

4. Access the Application

http://your-vm-ip:8080/
or
http://your-vm-ip:8080/java-gradle-webapp (if not deployed as ROOT)

πŸ”¨ Gradle Commands

# Build the project
./gradlew build

# Clean build
./gradlew clean

# Run tests
./gradlew test

# Build WAR
./gradlew war

# Display Gradle tasks
./gradlew tasks

# Show dependencies
./gradlew dependencies

# Run locally with Gretty
./gradlew appRun

# Debug run
./gradlew appRunDebug

πŸ“š Understanding Gradle

This project uses Gradle for build automation. Key files:

build.gradle

Defines:

  • Plugins (java, war, gretty)
  • Dependencies (servlets, logging, JSON processing)
  • Build properties
  • Custom tasks

settings.gradle

Defines the root project name

gradle wrapper

Allows building without Gradle installation

πŸ§ͺ Testing

Run tests using:

./gradlew test

πŸ“„ File Descriptions

Controllers

  • HelloServlet.java - Basic servlet demonstrating simple request/response
  • UserServlet.java - REST API servlet for user management

Models

  • User.java - User entity/POJO

Services

  • UserService.java - Business logic for user operations (in-memory storage)

Web Files

  • index.html - Interactive frontend for testing the API
  • error.html - Error page
  • web.xml - Web application deployment descriptor

πŸ” Security Notes

This is a demonstration project. For production:

  • Add input validation and sanitization
  • Implement authentication and authorization
  • Use HTTPS
  • Add CSRF protection
  • Implement proper error handling
  • Use a proper database instead of in-memory storage
  • Add rate limiting

πŸ“– Learning Resources

🀝 Contributing

Feel free to fork and submit pull requests for improvements.

πŸ“„ License

This project is open source and available under the MIT License.

πŸ‘€ Author

xCaptain404

πŸ†˜ Troubleshooting

Port 8080 already in use

./gradlew appRun -Dhttp.port=8081

Gradle wrapper permission denied

chmod +x gradlew

Build fails with Java version error

Ensure Java 11+ is installed:

java -version

WAR deployment not working

  • Check Tomcat logs: logs/catalina.out
  • Verify WAR file syntax: unzip -l java-gradle-webapp-1.0.0.war
  • Ensure correct permissions on webapps folder

Happy coding! πŸš€

About

Fully working Java web application for Deployment testing using Gradle

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors