Share
Explore

My First Servlet

1. Set Up Your Environment
Install Java Development Kit (JDK):
Download the latest JDK from the Oracle website (or an OpenJDK distribution).
Install the JDK, following the on-screen instructions.
Install Apache Tomcat:
Download the latest stable version of Tomcat from the Apache Tomcat website.
Extract the downloaded archive to a location of your choice (e.g., C:\apache-tomcat-10.1.13 on Windows or /opt/apache-tomcat-10.1.13 on macOS/Linux).
Install IntelliJ IDEA:
Download the Community Edition of IntelliJ IDEA from the JetBrains website.
Install IntelliJ IDEA, following the provided instructions.
2. Create a New Project in IntelliJ IDEA
Launch IntelliJ IDEA: Open IntelliJ IDEA.
New Project: Click "New Project."
Java Enterprise: Select "Java Enterprise" from the left-hand menu.
Project Settings:
Give your project a name (e.g., "HelloWorldServlet").
Choose a project location.
Select the JDK you installed.
Application Server: Click "New" next to "Application Server" and choose "Tomcat Server."
Specify the "Tomcat Home" directory (where you extracted Tomcat).
Click "Next."
Java EE Version: Choose "Jakarta EE 9" (or a later version).
Dependencies: Make sure "Servlet" is selected in the dependencies list.
Finish: Click "Finish" to create the project.
3. Create the Servlet
Locate src directory: In the Project view, find the src directory under your project's main directory (e.g., HelloWorldServlet/src).
Create a Java class: Right-click on the src directory and select "New" -> "Java Class."
Name the class: Name the class HelloWorldServlet.
Write the code: Replace the default class content with the following code:
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter;
@WebServlet("/hello") // This annotation maps the servlet to the URL "/hello" public class HelloWorldServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set the content type of the response response.setContentType("text/html");
// Get a PrintWriter to write the response PrintWriter out = response.getWriter();
// Write the HTML content out.println("<html><body>"); out.println("<h1>Hello, World!</h1>"); out.println("</body></html>"); } }
4. Run the Servlet
Run Configuration:
Click "Add Configuration..." in the top right corner.
Click the "+" button and select "Tomcat Server" -> "Local."
In the "Deployment" tab, click the "+" button and add your "HelloWorldServlet" artifact.
Click "OK."
Start Tomcat: Click the "Run" button (green play icon) to start the Tomcat server.
Access the servlet: Open a web browser and go to the URL http://localhost:8080/HelloWorldServlet/hello (replace "HelloWorldServlet" with your project name if it's different).
You should see the "Hello, World!" message displayed in your web browser!
Explanation
@WebServlet("/hello"): This annotation tells Tomcat to map requests to the "/hello" URL path to your HelloWorldServlet class.
doGet() method: This method handles HTTP GET requests to the servlet.
response.setContentType("text/html"): This line sets the content type of the response to HTML, so the browser knows how to display it.
PrintWriter out = response.getWriter(): This gets a PrintWriter object that you can use to write the HTML content to the response.
out.println(...): These lines write the actual HTML code that will be sent to the browser.
This comprehensive guide should help anyone with no prior knowledge create and run a simple "Hello, World!" servlet using IntelliJ IDEA and Apache Tomcat. Remember to replace "HelloWorldServlet" with your actual project name if you chose a different one.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.