Servlets are Java programs that run on a web server and handle client requests. In this article, we will learn how to create, compile, configure, and run a simple Hello World Servlet in Apache Tomcat without using any Java IDE like Eclipse.
- Create and compile a basic servlet using Java.
- Configure the servlet in the web.xml file.
- Deploy and run the servlet on the Apache Tomcat server.
Note: Running a server like Tomcat to run our servlet. if it is not already installed, you can install it using this article: How to Install Apache Tomcat on Windows?
Basic Terms we Know
we have a awareness for these terms before directly running a servlet Application
GenericServlet
- it is a protocol-independent servlet class that can be extended for different protocols like HTTP, FTP, and SMTP.
- Provides implementations of init() and destroy() methods.
- Implements the ServletConfig interface.
- Used as the parent class for protocol-specific servlets.
HttpServlet
- HttpServlet is used for handling HTTP requests in web applications.
- Provides implementation for the service() method.
- Calls doGet() for GET requests and doPost() for POST requests.
- Commonly used for developing web-based servlets.
Prerequisites
- Java JDK installed
- Apache Tomcat Server installed
- Command Prompt and Text Editor
Steps For Creating Servlet Program Using Text Editor
Follow these steps to create a simple Servlet program without Using any IDE.
Step 1: Create Project Directory Structure
Since we are not using any IDE, first create the required directory structure manually inside the webapps folder of Tomcat.
- classes folder stores the compiled servlet class files.
- WEB-INF contains configuration files for the web application.
TOMCAT_HOME
└── webapps
└── net
└── WEB-INF
├── classes
└── web.xml
Step 2: Create Servlet File
Create a file named HelloWorldServlet.java using any text editor.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body></html>");
out.close();
}
}
Step 3: Compile the Servlet
Compile the servlet using Command Prompt.
javac HelloWorldServlet.java
Step 4: Place Class File
Copy the generated HelloWorldServlet.class file into the WEB-INF/classes folder.
- The servlet container loads classes from this directory.
- Tomcat uses this class during request processing.
Step 5: Configure Servlet in web.xml
Add servlet configuration in the web.xml file.
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>
Step 6: Start Tomcat Server
Start Tomcat server and it loads and initializes the servlet and servlet becomes ready to handle requests.
Step 7: Access the Servlet
Open the browser and enter the following URL
http://localhost:8080/net/servlet/HelloWorld
Output:
.png)
Explanation: The response is displayed in the browser The request is handled by doGet().