doPost() in doGet() in Java Servlets - What's the difference?

doPost() in doGet() in Servlets

Till now we have seen working with doPost() and doGet(). Now let us see how to write doPost() in doGet() before that lets us talk about them for some time. doPost() does invisible data submission whereas doGet() does visible data submission, through url. While doGet() is not recommended for everything, doPost() is used to carry passwords. But some tricky people, tries to pass data through url, though doPost() was to be done. However, this doesn't happen unless they use some extension like Tamper Data. To give them (only the people who thinks of doing so), you can write doPost() within doGet(). Let us look at the example for a better understand.

doPost() in doGet() - Folder Structure

     |_ dopostindoget
                  |_ WEB-INF
                  |            |_ classes
                  |            |        |_
                  |            |        |_ doPostIndoGet.class
                  |            |_ web.xml
                  |_ index.html

doPost() in doGet() - index.html

<title>doPost in doGet</title>

<form action="/dopostindoget/dpdg" method="post">

Name: <input type="text" name="uname" width="20" />
Password: <input type="password" name="pass" width="20" />
<input type="submit" name="submit" value="Login" />



form: User input is taken in a form containing input fields.

action="/postservlet/ps"/ps is the url pattern in web.xml and the postservlet is the name of the project (folder name). The project folder name must be specified here for sure, else the statement means to search in the webapps directory but not within the project.

method="post": Invisible submission, at the background.

name="uname": Used to get the value in this field in Servlet class.

name="pass": Used to get the value in this field in Servlet class.

doPost() in doGet() - web.xml









Cant understand web.xml? See Understanding web.xml in Java Servlets

doPostIndoGet Result

doPostIndoGet Tricked. See Address Bar.

import javax.servlet.http.*;
import javax.servlet.*;
public class doPostIndoGet extends HttpServlet
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException

String user=req.getParameter("uname");
String password=req.getParameter("pass");

PrintWriter pw=res.getWriter();

pw.println("<h1> Welcome "+user+"</h1> <br/>");
pw.println("<h2> Your Password is "+password+"</h2>");



javax.servlet.http.*: Contains HttpServlet, HttpServletRequest, HttpServletResponseclasses (used here).

javax.servlet.*: Contains ServletException (used here).*: Contains PrintWriter, IOException (used here).

doGet(): To let the tricky people, submit through address bar also. Passing, req,res objects to doPost(), if doPost() was just written without doGet(), then tricky people couldn't pass it through address bar. Observing the screenshot gives a better understand.

doPost(): For url type submission, submission through url, observe method="post" in index.html

PrintWriter: Part of the package.

req.getParameter("uname"): Get the value (user input value) in the input field nameduname.

req.getParameter("pass"): Get the value (user input value) in the input field named pass.

pw.println("<h1>Welcome "+user+"</h1> <br/>"): Present in the PrintWriter class, write Welcome along with user within <h1> with a line break. (here).

pw.println("<h2> Your Password is "+password+"</h2>"): Display password withinh2 tag.

Note: Do not forget to compile the class and re/start tomcat! ;)

Posted by at on

Tags: Servlets,