Sobes.tech
Junior

How to create a web application in Java?

sobes.tech AI

Answer from AI

Creating a web application in Java can be done using frameworks and technologies from the Java Enterprise Edition (Java EE) ecosystem or Spring.

Main steps and components:

  1. Choosing a technology/framework:

    • Java EE: Uses specifications such as Servlet/JSP, JSF, JAX-RS (RESTful), JPA. Requires an application server (Tomcat, Jetty, WildFly, GlassFish).
    • Spring Framework: A popular framework with a wide range of modules (Spring MVC, Spring Boot, Spring Data). Spring Boot allows creating standalone applications with an embedded server.
  2. Project structure:

    • Uses a build system like Maven or Gradle.
    • Typical structure for a web application (WAR file):
      ├── src
      │   ├── main
      │   │   ├── java       // Java code (servlets, controllers, services)
      │   │   ├── resources  // Resources (config files, properties)
      │   │   └── webapp     // Web resources (HTML, CSS, JS, JSP, WEB-INF)
      │   └── test           // Tests
      └── pom.xml or build.gradle // Build file
      
    • WEB-INF contains web.xml (deployment descriptor for Java EE) and classes/libraries.
  3. Frontend:

    • HTML, CSS, JavaScript.
    • Server-Side Rendering (SSR) technologies: JSP, Thymeleaf, Freemarker.
    • Frontend frameworks (Client-Side Rendering - CSR): React, Angular, Vue.js (in this case, the backend in Java provides REST API).
  4. Backend:

    • Servlets: Fundamental Java EE technology for handling HTTP requests. They process requests and generate responses.
    • JSP (JavaServer Pages): Allow embedding Java code in HTML for dynamic content. Often used with servlets (servlet as controller, JSP as view).
    • Frameworks:
      • Spring MVC: Implements the Model-View-Controller (MVC) pattern. Uses controllers for request handling, models for data, and views for display.
      • Spring Boot: Simplifies setup and deployment of Spring applications. Includes an embedded server, auto-configuration.
      • JSF (JavaServer Faces): A component-based UI framework for Java EE.
      • JAX-RS: Java EE specification for creating RESTful web services.
  5. Data access:

    • JDBC (Java Database Connectivity): Low-level API for database interaction.
    • ORM (Object-Relational Mapping) frameworks: JPA (Hibernate, EclipseLink), Spring Data JDBC/JPA. Allow working with databases using Java objects.
  6. Deployment:

    • WAR file: Packaged and deployed on a Java EE application server (Tomcat, Jetty, etc.).
    • Standalone JAR (Spring Boot): Contains an embedded server and can be run as a regular executable JAR.

Example of creating a simple web application using Spring Boot (REST API):

  1. Use Spring Initializr (start.spring.io) to generate a basic project with dependencies Spring Web and Spring Data JPA (for database example).
  2. Create a controller class to handle HTTP requests:
    package com.example.demo.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController // Annotation indicating this is a REST controller
    public class HelloWorldController {
    
        @GetMapping("/hello") // Handles GET requests to /hello
        public String sayHello(@RequestParam(value = "name", defaultValue = "World") String name) {
            // Accepts an optional parameter 'name'
            return String.format("Hello, %s!", name); // Returns a greeting string
        }
    }
    
  3. Create an entity class and repository using Spring Data JPA (if working with a database).
  4. Configure data source in application.properties or application.yml.
  5. Run the main Spring Boot application class.

This approach allows quickly creating a functional web application, focusing on business logic.