Handling GET and POST requests in Jakarta Servlets involves overriding the doGet() and doPost() methods provided by the HttpServlet class. Here’s a step-by-step guide:
1. Import Necessary Packages
First, ensure you import the jakarta.servlet and jakarta.servlet.http packages in your servlet class.
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;
import java.io.PrintWriter;
2. Create a Servlet Class
Your servlet class should extend the HttpServlet class. The doGet() method will handle GET requests, while the doPost() method will handle POST requests.
3. Override doGet and doPost Methods
- Use the
HttpServletRequestobject to get request data. - Use the
HttpServletResponseobject to send a response to the client.
4. Example Code
Here’s a complete example:
package org.kodejava.servlet;
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;
import java.io.PrintWriter;
@WebServlet("/example") // Annotation to map this servlet to "/example"
public class ExampleServlet extends HttpServlet {
// Handles GET requests
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
// Get query parameter (e.g., ?name=John)
String name = request.getParameter("name");
// Prepare response
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Hello, " + (name != null ? name : "Guest") + "!</h1>");
out.println("<form method='POST' action='/example'>");
out.println("<label for='postName'>Enter your name:</label>");
out.println("<input type='text' id='postName' name='name'>");
out.println("<button type='submit'>Submit</button>");
out.println("</form>");
out.println("</body></html>");
}
// Handles POST requests
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
// Get form data (e.g., from POST body)
String name = request.getParameter("name");
// Prepare response
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h1>Thank you, " + (name != null ? name : "Guest") + "!</h1>");
out.println("<a href='/example'>Go back</a>");
out.println("</body></html>");
}
}
5. Explanation
- Annotations: The
@WebServlet("/example")annotation maps this servlet to the/exampleURL. - GET and POST
- The
doGet()method handles requests sent asGET, typically used when the client fetches data. - The
doPost()method handles requests sent asPOST, commonly used to send form data to the server.
- The
- Parameters: Use
request.getParameter("paramName")to retrieve parameters from the request.
6. Deploy and Test
- Add the servlet to your Jakarta EE web application.
- Access the servlet:
GETrequest: Open `http://localhost:8080/your-app-context/example` in a browser.POSTrequest: Submit the HTML form created indoGet()or use a tool like Postman.
7. Keynotes
- Always handle exceptions (
IOExceptionandServletException) properly in production. - Use appropriate HTTP response headers and status codes.
- Consider character encoding (
request.setCharacterEncoding("UTF-8")andresponse.setCharacterEncoding("UTF-8")) for supporting special characters.
This is how you handle GET and POST requests in a Jakarta Servlet effectively!
Maven dependencies
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
