Java MVC Web Application using JSP and Servlet - Step by Step Tutorial

In this tutorial, we will create a Java MVC (Model-View-Controller) web application using JSP and Servlet. We'll use the latest versions of JSP and Servlet, and we won't be using the web.xml file for configuration. Instead, we'll use annotations.

Step 1: Setting Up the Development Environment

Before we start, ensure you have the following tools installed:

  1. JDK (Java Development Kit) - preferably the latest version.
  2. Apache Tomcat - latest version.
  3. IDE (Integrated Development Environment) - Eclipse, IntelliJ IDEA, or any other Java IDE.
  4. Maven - for managing project dependencies.

Step 2: Create a Maven Project

  1. Open your IDE and create a new Maven project.
  2. Update the pom.xml file to include the necessary dependencies.
<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</groupId>
    <artifactId>mvc-webapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet.jsp</groupId>
            <artifactId>jakarta.servlet.jsp-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet.jsp.jstl</groupId>
            <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jakarta.servlet.jsp.jstl</artifactId>
            <version>3.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>
        </plugins>
    </build>
</project>

Step 3: Directory Structure

Ensure your project structure looks like this:

mvc-webapp/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           ├── controller/
│   │   │           │   └── HelloController.java
│   │   │           ├── model/
│   │   │           │   └── User.java
│   │   │           └── service/
│   │   │               └── UserService.java
│   │   ├── resources/
│   │   └── webapp/
│   │       ├── WEB-INF/
│   │       └── index.jsp
└── pom.xml

Step 4: Create Model Class

Create a simple model class User.java in the com.example.model package.

package com.example.model;

public class User {
    private String name;
    private String email;

    public User() {}

    public User(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Step 5: Create Service Class

Create a service class UserService.java in the com.example.service package.

package com.example.service;

import com.example.model.User;

public class UserService {
    public User getUserDetails() {
        return new User("John Doe", "john.doe@example.com");
    }
}

Step 6: Create Controller Class

Create a controller class HelloController.java in the com.example.controller package.

package com.example.controller;

import com.example.model.User;
import com.example.service.UserService;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/hello")
public class HelloController extends HttpServlet {
    private UserService userService;

    public void init() {
        userService = new UserService();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = userService.getUserDetails();
        request.setAttribute("user", user);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }
}

Step 7: Create JSP Page

Create a JSP file index.jsp in the src/main/webapp directory.

<%@ taglib uri="http://jakarta.apache.org/taglibs/standard-compat" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Hello, ${user.name}!</h1>
    <p>Your email is ${user.email}</p>
</body>
</html>

Step 8: Run the Application

  1. Build the Maven project using mvn clean install.
  2. Deploy the mvc-webapp.war file to Apache Tomcat.
  3. Start the Tomcat server and navigate to http://localhost:8080/mvc-webapp/hello.

You should see the welcome message with the user details.

Conclusion

In this tutorial, we created a simple Java MVC web application using JSP and Servlet with the latest versions. We used annotations to configure our servlets and avoided using the web.xml file. This approach helps in creating clean and modern Java web applications.

Related Servlet Posts

    Comments

    1. Nice blog, Thank you to share such an information about Java MVC Web Application using JSP and Servlet blog with us. For more information about java visit:https://www.clariwell.in/best-java-course-in-pune

      ReplyDelete

    Post a Comment

    Leave Comment