Skip to content

TahsinEfe/Simple-Java-HTTP-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Java HTTP Server

This repository contains a simple HTTP server implemented in Java. It demonstrates basic server-client communication, handling static files, and providing API endpoints for dynamic content, including a To-Do list.

Table of Contents

Features

  • Basic HTTP Server: Handles HTTP GET, POST, PUT and DELETE requests.
  • Static File Serving: Serves HTML, CSS, and JavaScript files from the src/main/resources/Webroot directory.
  • Dynamic API Endpoints:
    • /api/time: Returns the current server time in JSON format.
    • /api/weather: Returns mock weather data based on the client's IP address.
    • /api/todos: Provides CRUD operations for a simple To-Do list.
  • To-Do List Management: Allows users to add, list, update, and delete to-do items through a simple web interface.
  • Request Logging: Logs all incoming requests with the client IP addresses to access.log file.
  • IP Logging: Logs all client IP address to ip_log.txt file.

Getting Started

Prerequisites

Running the Server

  1. Clone the Repository:

    git clone [YOUR_REPOSITORY_URL]
    cd [YOUR_REPOSITORY_DIRECTORY]
  2. Compile the Java Code:

    • If you are using an IDE it can usually compile for you.
    • If you are not using an IDE you can compile the code with the following command:
    javac src/main/java/com/serversocket/serversocket/*.java
    

Run the Server:

 java com.serversocket.serversocket.SimpleHTTPServer
  • This will start the server on port 8080.

Access the Server: *Open your web browser and go to http://localhost:8080 to access the main page. *From here, you can test the various server features using the provided buttons.

Project Structure

simple-http-server/
├── src/
   └── main/
       ├── java/
          └── com/
              └── serversocket/
                  └── serversocket/
                      ├── ClientHandler.java   // Handles client requests
                      ├── RequestParser.java   // Parses HTTP requests
                      └── SimpleHTTPServer.java // Main server class
       └── resources/
           └── Webroot/
                ├── index.html        // Main web page
                ├── script.js         // Client-side scripts
                └── style.css        // Stylesheet
├── access.log         // Logs incoming requests
├── ip_log.txt         // Logs client IP addresses
└── README.md          // This file

*src/main/java/com/serversocket/serversocket: Contains the server's Java code.

  • ClientHandler.java: Handles individual client connections, processes requests, and sends responses.

  • RequestParser.java: Parses HTTP request headers and query parameters.

  • SimpleHTTPServer.java: Sets up the server and listens for client connections.

  • src/main/resources/Webroot: Contains static web files (HTML, CSS, JavaScript).

    • index.html: The main web page with buttons and the to-do panel.

    • script.js: Contains javascript to manage events and make request to the server.

    • style.css: Style sheet for the web page.

  • access.log: Logs incoming request information.

  • ip_log.txt: Logs IP addresses of connected clients.

API Endpoints

/api/time

  • Method: GET
  • Description: Retrieves the current server time
  • Response: JSON object with the current timestamp:
     {
        "time": "Thu Oct 26 10:00:00 EDT 2023"
     }

/api/weather

  • Method: GET
  • Description: Retrieves mock weather data based on the client's IP address.
  • Response: JSON object with mock weather details:
      {
 "city": "Global City",
 "temperature": "20°C",
 "condition": "Partly Cloudy"
 }

/api/todos

  • Method: GET, POST, PUT, DELETE

  • Description: Manages the To-Do list.

    • GET: Retrieves the list of tasks.
    • POST: Adds a new task. Needs the task query parameter.
    • PUT: Updates an existing task. Needs the task and updatedTask query parameters.
    • DELETE: Deletes an existing task. Needs the task query parameter.

Request Example:

 GET /api/todos
POST /api/todos?task=Buy Milk
PUT /api/todos?task=Buy Milk&updatedTask=Buy Almond Milk
DELETE /api/todos?task=Buy Milk

*Response:

  • GET - returns a list of tasks
   [
        "Buy Milk",
        "Learn Java",
        "Get Groceries"
    ]
  *  `POST` - returns a 201 Created if task is added
  *   `PUT` - returns 200 OK if task is updated
  *   `DELETE` - returns 200 OK if task is deleted.

To-Do List Functionality

  1. Click "To-Do List" Button: The to-do panel is toggled.
  2. Add Task: Enter a task in the input field and click "Add".
  3. List Tasks: Click "List" to display all stored tasks.
  4. Update Task: Enter an existing task in the task field, then enter the updated task in the update task field, click the "Update" button.
  5. Delete Task: Enter a task to delete in the task field and click "Delete."

Technologies Used

  • Java: For server-side logic.
  • Java Standard Library: For socket programming and I/O.
  • Gson: For serializing Java objects into JSON responses.
  • HTML, CSS, JavaScript: For creating the web interface.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published