To create a basic servlet using the HttpServlet
class in Jakarta Servlet API, follow these steps:
Steps to Create a Basic Servlet:
- Create a Java Class Extending
HttpServlet
:- Your servlet class should extend the
HttpServlet
class provided in the Jakarta Servlet API.
- Your servlet class should extend the
- Define Servlet Annotations or Configuration:
- Use the
@WebServlet
annotation to define the servlet and map it to a URL pattern. Alternatively, you can configure it in theweb.xml
file if annotations are not used.
- Use the
- Override
doGet
ordoPost
Methods:- Override
doGet
for handlingGET
requests anddoPost
forPOST
requests (based on the type of request you expect).
- Override
- Set the Content Type in Response:
- The response content type can be set using
response.setContentType()
.
- The response content type can be set using
- Write the Response to Output:
- Use a
PrintWriter
object received fromresponse.getWriter()
to write the response content.
- Use a
Code Example:
Here’s a sample implementation for a basic servlet:
package org.kodejava.servlet;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "HelloWorldServlet", urlPatterns = {"/hello", "/helloworld"})
public class HelloWorld extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head><title>Hello World Servlet</title></head>");
writer.println("<body>Hello World! How are you doing?</body>");
writer.println("</html>");
writer.close();
}
}
Explanation of Code:
@WebServlet
Annotation:name
: Specifies the servlet name.urlPatterns
: Maps the servlet to specific URL patterns (in this example,/hello
).
doGet
Method:- Handles
GET
requests sent to/hello
. - Sets the content type for the response to
text/html
so that the client knows the returned content is HTML. - Dynamically writes an HTML page to the response using the
PrintWriter
.
- Handles
- Response Lifecycle:
- Any request to
http://<server>:<port>/hello
is routed to thedoGet
method based on the@WebServlet
mapping.
- Any request to
Deploying the Servlet in Jakarta EE:
- Place your servlet class in the Java package structure under
src/main/java
. - Ensure your project is configured to use a Jakarta Servlet container like Apache Tomcat or Jakarta EE compatible application server.
- Deploy the
.war
file or run directly if your IDE supports it.
To build the .war
file you can follow in steps in this post: How to build WAR file for Jakarta EE Servlet Container?.
Maven dependencies
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
Latest posts by Wayan (see all)
- How do I secure servlets with declarative security in web.xml - April 24, 2025
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025