# v-eenay/simple-servlet-gallery architecture

> Architecture diagram of the GitHub repository v-eenay/simple-servlet-gallery, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/v-eenay/simple-servlet-gallery
- Repository: https://github.com/v-eenay/simple-servlet-gallery

## Overview

##### Main Components
- Frontend (View Layer):
- JSP pages (located under src/main/webapp/WEB-INF/view and its subdirectories)
- Static assets (CSS, JS in src/main/webapp/assets)
- Controllers (Presentation/Controller Layer):
- Servlets handling various user actions (LoginServlet, RegisterServlet, ImageDisplayServlet, etc. in the src/main/java/com/example/verysimpleimagegallery/controller and its admin sub-package)
- Business Logic / Service Layer:
- Service classes such as AuthService and GalleryService handling core business rules
- Data Access / Persistence Layer:
- DAO classes (GalleryItemDAO, UserDAO) for database interactions
- Database:
- Database connectivity via JDBC indicated by DbConnectionUtil and the configuration files (db.properties, schema.sql in src/main/resources)
- Utility Components:
- Utility classes like PasswordHashUtil for security (password handling)
- Filters:
- Authentication and authorization filters (AuthFilter, AdminAuthFilter) for managing access and session security

##### Relationships & Data Flow
a. A client's browser sends a request (via HTTP) and reaches the Frontend, typically beginning with a JSP rendered from the WEB-INF directory.
b. Requests are routed by the web.xml configuration to the appropriate Servlet controllers.
c. Controllers call methods from service classes (GalleryService, AuthService) to process business logic.
d. The service layer interacts with DAOs to perform database operations (CRUD via JDBC using DbConnectionUtil).
e. Results are passed back through the controller to a view (JSP) for rendering.
f. Filters (AuthFilter and AdminAuthFilter) are applied to requests to ensure proper security handling before controllers process them.

##### Highlighted Architectural Patterns & Design Principles
- MVC Pattern: Clearly differentiating between Model (Java objects in the model package), View (JSPs), and Controller (Servlets).
- DAO Pattern: Encapsulating database interaction in dedicated classes.
- Use of Filters for cross-cutting concerns like authentication (separation of concerns).
- RESTful Controllers: While this is a traditional servlet-based web app, the URI structure and request handling follow similar REST principles.
- Modularization: Splitting functionality into clear packages (controller, dao, service, model, util) helps with maintainability and scalability.

##### Technologies, Frameworks, and Tools
- Java Servlets & JSP: Core technologies for handling requests and dynamic page rendering.
- Apache Maven: Used for build automation (pom.xml, mvnw scripts).
- JDBC: For database connectivity.
- Apache Tomcat (or similar): As the deployed servlet container.
- Modern HTML/CSS and JavaScript: For the client-side, ensuring a responsive and interactive UI.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Client["Browser (Client)"]:::client

    %% Web Server (Servlet Container) containing Filters and Controllers
    subgraph "Web Server (Servlet Container)"
        direction TB
        Filters["Security Filters\n(AuthFilter,AdminAuthFilter)"]:::filter
        Controllers["Controllers\n(LoginServlet,RegisterServlet,ImageDisplayServlet,...)"]:::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        AuthService["AuthService"]:::service
        GalleryService["GalleryService"]:::service
    end

    %% Utility Components
    subgraph "Utility Components"
        PasswordHashUtil["PasswordHashUtil"]:::util
    end

    %% Data Access Layer (DAO)
    subgraph "Data Access Layer (DAO)"
        UserDAO["UserDAO"]:::dao
        GalleryItemDAO["GalleryItemDAO"]:::dao
    end

    %% Database Connectivity: Database and Config Files
    subgraph "Database Connectivity"
        Database["Database (DbConnectionUtil)"]:::database
        ConfigFiles["Configuration (db.properties,schema.sql)"]:::database
    end

    %% View Layer
    subgraph "View Layer"
        JSP["JSP Files"]:::view
        Assets["Static Assets"]:::view
    end

    %% Flow relationships
    Client -->|"HTTP_Request"| Filters
    Filters -->|"FilterChain"| Controllers

    Controllers -->|"Invoke"| AuthService
    Controllers -->|"Invoke"| GalleryService

    AuthService -->|"Uses"| PasswordHashUtil

    AuthService -->|"Calls"| UserDAO
    GalleryService -->|"Calls"| GalleryItemDAO

    UserDAO -->|"DB_Operations"| Database
    GalleryItemDAO -->|"DB_Operations"| Database
    Database -.->|"Uses"| ConfigFiles

    Controllers -->|"RenderView"| JSP
    JSP -->|"SendsResponse"| Client
    Client -->|"StaticContentReq"| Assets

    %% Click Events
    click JSP "https://github.com/v-eenay/simple-servlet-gallery/tree/main/src/main/webapp/WEB-INF/view"
    click Assets "https://github.com/v-eenay/simple-servlet-gallery/tree/main/src/main/webapp/assets"
    click Controllers "https://github.com/v-eenay/simple-servlet-gallery/tree/main/src/main/java/com/example/verysimpleimagegallery/controller"
    click AuthService "https://github.com/v-eenay/simple-servlet-gallery/blob/main/src/main/java/com/example/verysimpleimagegallery/service/AuthService.java"
    click GalleryService "https://github.com/v-eenay/simple-servlet-gallery/blob/main/src/main/java/com/example/verysimpleimagegallery/service/GalleryService.java"
    click UserDAO "https://github.com/v-eenay/simple-servlet-gallery/blob/main/src/main/java/com/example/verysimpleimagegallery/dao/UserDAO.java"
    click GalleryItemDAO "https://github.com/v-eenay/simple-servlet-gallery/blob/main/src/main/java/com/example/verysimpleimagegallery/dao/GalleryItemDAO.java"
    click Database "https://github.com/v-eenay/simple-servlet-gallery/blob/main/src/main/java/com/example/verysimpleimagegallery/util/DbConnectionUtil.java"
    click ConfigFiles "https://github.com/v-eenay/simple-servlet-gallery/tree/main/src/main/resources"
    click PasswordHashUtil "https://github.com/v-eenay/simple-servlet-gallery/blob/main/src/main/java/com/example/verysimpleimagegallery/util/PasswordHashUtil.java"
    click Filters "https://github.com/v-eenay/simple-servlet-gallery/tree/main/src/main/java/com/example/verysimpleimagegallery/filter"

    %% Styles
    classDef client fill:#f9f,stroke:#333,stroke-width:2px;
    classDef filter fill:#ff9,stroke:#333,stroke-width:2px;
    classDef controller fill:#9f9,stroke:#333,stroke-width:2px;
    classDef service fill:#9ff,stroke:#333,stroke-width:2px;
    classDef dao fill:#fc9,stroke:#333,stroke-width:2px;
    classDef database fill:#f99,stroke:#333,stroke-width:2px;
    classDef view fill:#99f,stroke:#333,stroke-width:2px;
    classDef util fill:#ccf,stroke:#333,stroke-width:2px;
```
