Servlet HTTP Methods GET, POST, PUT, DELETE is an important Servlet topic because it appears in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.
For this page, focus on what problem Servlet HTTP Methods GET, POST, PUT, DELETE solves, where developers usually make mistakes, and how to verify the result. The audit note for this lesson was: under 650 content words; limited checklist/practice/mistake/FAQ notes .
A strong understanding of Servlet HTTP Methods GET, POST, PUT, DELETE should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work with tools or output.
Servlet HTTP Methods GET POST PUT DELETE should be studied as a practical Servlet lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the servlet > http-methods page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
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>");
}
}
| 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");
}
}
}
When studying Servlet HTTP Methods GET, POST, PUT, DELETE, separate three things: the concept, the syntax, and the situation where it is useful. This prevents the lesson from becoming a list of commands with no practical meaning.
In Servlet, Servlet HTTP Methods GET, POST, PUT, DELETE becomes easier when you build a tiny example first, then increase complexity. Add one realistic input, one invalid or boundary input, and one explanation of why the result changes.
class ServletHTTPMethodsGETPOSTPUTDELETEReview {
public static void main(String[] args) {
String state = "ready";
System.out.println("Servlet HTTP Methods GET POST PUT DELETE: " + state);
}
}
String value = null;
if (value == null) {
System.out.println("Servlet HTTP Methods GET POST PUT DELETE: handle the missing value before continuing");
}
Memorizing Servlet HTTP Methods GET POST PUT DELETE without the situation where it is useful.
Connect Servlet HTTP Methods GET POST PUT DELETE to a concrete Servlet task.
Testing Servlet HTTP Methods GET POST PUT DELETE only with the perfect input.
Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Changing code before reading the visible symptom or error message.
Inspect the output, state, configuration, or stack trace connected to Servlet HTTP Methods GET POST PUT DELETE.
Memorizing Servlet HTTP Methods GET POST PUT DELETE without the situation where it is useful.
Connect Servlet HTTP Methods GET POST PUT DELETE to a concrete Servlet task.
The common mistake is memorizing syntax without understanding when the behavior changes or fails.
Remember the problem it solves in Servlet, then attach the syntax or steps to that problem.
You can predict the result of a small example, explain a failure case, and choose it over a nearby alternative for a clear reason.
They often copy the syntax but skip the state, input, dependency, selector, route, type, or configuration that controls the behavior.
Explore 500+ free tutorials across 20+ languages and frameworks.