Servlet Tutorial 3 : GET, POST, doGet, doPost, Java Servlet, Request Response by Thakur Arjun Singh



For complete list of videos please visit
GET vs. Post
Browser can request a resource using GET or POST methods (HTTP protocol).
doGet and doPost are specific to Java and handle GET and POST respectively.
My blog

GET ………………………….. POST

Data is sent as part of URL….

, , , , , , , , , , ,

16 thoughts on “Servlet Tutorial 3 : GET, POST, doGet, doPost, Java Servlet, Request Response by Thakur Arjun Singh

  1. welcomearjunServed at: /2GETandPOST i get this when i click ok ? and my code looks different even i follow you why?
    @WebServlet("/getpost")
    public class GETPOSTServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter printWriter=response.getWriter();
    printWriter.print("welcome");
    String firstName=request.getParameter("fname");
    printWriter.print(firstName);

    response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }

    }

  2. It's very helpful, but there is one thing i'm still confusing. I'm writing a basic codes in javaEE, when I added these codes to doGet()

    PrintWriter out = response.getWriter();
    out.println("<html><body><h1>The square root of 4.0 is: " + Math.sqrt(4.0) + "</h1></body></html>");

    Eclipse will print out something, like "The square root of 4.0 is 2.0", but when I add these to doPost(), Eclipse print out nothing.
    Please tell me why, and in what case we use doGet() or doPost()? As you said doGet() and doPost() is similar, but doGet() is not safe(password…) and very limited while doPost() is more powerful. But those codes above won't work the same. Thank you very much! Thakur

Leave a Reply

Your email address will not be published. Required fields are marked *