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=""



Step 3: Directory Structure

Ensure your project structure looks like this:

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

Step 4: Create Model Class

Create a simple model class 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) { = name; = email;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

Step 5: Create Service Class

Create a service class 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", "[email protected]");

Step 6: Create Controller Class

Create a controller class 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;

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="" prefix="c" %>
<!DOCTYPE html>
    <h1>Hello, ${}!</h1>
    <p>Your email is ${}</p>

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.


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


    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:


    Post a Comment

    Leave Comment