Servlet HTTP Methods
HTTP Methods in Servlets
HttpServlet provides a separate method for each HTTP verb. You override only the methods your Servlet needs to handle:
| Method | HTTP Verb | Use Case |
|---|---|---|
doGet() | GET | Retrieve data, display pages |
doPost() | POST | Submit forms, create resources |
doPut() | PUT | Update/replace a resource |
doDelete() | DELETE | Delete a resource |
doHead() | HEAD | Like GET but no response body |
doOptions() | OPTIONS | Returns supported HTTP methods |
doPatch() | PATCH | Partial update (override service()) |
package com.example;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.*;
@WebServlet("/form")
public class FormServlet extends HttpServlet {
// GET: Show the form
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE html><html><body>");
out.println("<h2>Registration Form</h2>");
out.println("<form method='post' action='/form'>");
out.println(" Name: <input type='text' name='name'/><br/>");
out.println(" Email: <input type='email' name='email'/><br/>");
out.println(" <button type='submit'>Register</button>");
out.println("</form></body></html>");
}
// POST: Process the form
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
String email = req.getParameter("email");
// Validate
if (name == null || name.trim().isEmpty()) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Name is required");
return;
}
// Process (save to DB, etc.)
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("<h2>Registration Successful!</h2>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>Email: " + email + "</p>");
}
}
HTTP Status Codes
| Code | Constant | Meaning |
|---|---|---|
| 200 | SC_OK | Success |
| 201 | SC_CREATED | Resource created |
| 301 | SC_MOVED_PERMANENTLY | Permanent redirect |
| 302 | SC_FOUND | Temporary redirect |
| 400 | SC_BAD_REQUEST | Invalid request |
| 401 | SC_UNAUTHORIZED | Authentication required |
| 403 | SC_FORBIDDEN | Access denied |
| 404 | SC_NOT_FOUND | Resource not found |
| 500 | SC_INTERNAL_SERVER_ERROR | Server error |
@WebServlet("/redirect-demo")
public class RedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String action = req.getParameter("action");
if ("redirect".equals(action)) {
// sendRedirect: client-side redirect (new HTTP request)
// URL changes in browser, request attributes are lost
// Can redirect to external URLs
resp.sendRedirect("https://example.com");
// OR: resp.sendRedirect(req.getContextPath() + "/home");
} else if ("forward".equals(action)) {
// RequestDispatcher.forward: server-side forward (same request)
// URL does NOT change in browser, request attributes are preserved
// Can only forward within the same web application
req.setAttribute("message", "Forwarded from RedirectServlet");
RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/views/result.jsp");
rd.forward(req, resp);
} else if ("include".equals(action)) {
// RequestDispatcher.include: includes another resource's output
// Both servlets contribute to the response
RequestDispatcher rd = req.getRequestDispatcher("/header.jsp");
rd.include(req, resp);
resp.getWriter().println("<p>Main content here</p>");
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown action");
}
}
}
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.