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:
- JDK (Java Development Kit) - preferably the latest version.
- Apache Tomcat - latest version.
- IDE (Integrated Development Environment) - Eclipse, IntelliJ IDEA, or any other Java IDE.
- Maven - for managing project dependencies.
Step 2: Create a Maven Project
- Open your IDE and create a new Maven project.
- 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
- Build the Maven project using
mvn clean install
. - Deploy the
mvc-webapp.war
file to Apache Tomcat. - 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
- What is a Servlet in Java?
- Servlet Life Cycle
- Servlet Interface Example
- GenericServlet Class Example
- HttpServlet Class Example Tutorial
- HttpServlet doGet() Method Example
- HttpServlet doPost() Method Example
- @WebServlet Annotation Example
- @WebInitParam Annotation Example
- @WebListener Annotation Example
- @WebFilter Annotation Example
- @MultipartConfig Annotation Example
- How to Return a JSON Response from a Java Servlet
- Servlet Registration Form + JDBC + MySQL Database Example
- Login Form Servlet + JDBC + MySQL Example
- Servlet JDBC Eclipse Example Tutorial
- JSP Servlet JDBC MySQL CRUD Example Tutorial
- Servlet + JSP + JDBC + MySQL Example
- Registration Form using JSP + Servlet + JDBC + Mysql Example
- Login Form using JSP + Servlet + JDBC + MySQL Example
- JSP Servlet Hibernate CRUD Example
- JSP Servlet Hibernate Web Application
- Hibernate Registration Form Example with JSP, Servlet, MySQL
- Login Form using JSP + Servlet + Hibernate + MySQL Example
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