Hackforge Academy

Category: java

httpservlet and doget dopost

Published on 30 Sep 2026

Explanation

HttpServlet provides methods for handling HTTP requests such as GET, POST, PUT, and DELETE.

Code:

public class UserServlet extends HttpServlet {
    // HTTP request handling methods
}

Explanation

The doGet() method is normally used to retrieve data from the server.

Code:

@Override
protected void doGet(HttpServletRequest req,
                     HttpServletResponse resp)
                     throws IOException {
    resp.getWriter().println("User List");
}

Explanation

The doPost() method is commonly used to submit data to the server.

Code:

@Override
protected void doPost(HttpServletRequest req,
                      HttpServletResponse resp)
                      throws IOException {
    String name = req.getParameter("name");
    resp.getWriter().println("Hello " + name);
}

Explanation

A servlet can support both GET and POST operations using separate methods.

Code:

@WebServlet("/users")
public class UserServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req,
                         HttpServletResponse resp) {
        // Read users
    }

    protected void doPost(HttpServletRequest req,
                          HttpServletResponse resp) {
        // Create user
    }
}

Explanation

HTML forms can send data to a servlet using either GET or POST.

Code:

<form action="users" method="post">
    <input name="name">
    <input name="email">
    <button type="submit">Save</button>
</form>

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community