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.
Folder structure
webapps
|_ dopostindoget
|_ WEB-INF
|_ classes
| |_ doPostIndoGet.java
| |_ doPostIndoGet.class
|_ web.xml
|_ index.html
index.html
<html>
<head>
<title>doPost in doGet</title>
</head>
<body>
<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>
</body>
</html>
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.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>testservlet</servlet-name>
<servlet-class>doPostIndoGet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testservlet</servlet-name>
<url-pattern>/dpdg</url-pattern>
</servlet-mapping>
</web-app>
Cant understand web.xml? See Understanding web.xml in Java Servlets
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class doPostIndoGet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
}
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
, HttpServletResponse
classes (used here).
javax.servlet.*
: Contains ServletException
(used here).
java.io.*
: 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 java.io
package.
req.getParameter("uname")
: Get the value (user input value) in the input field named uname
.
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 within h2
tag.
Note: Do not forget to compile the class and re/start tomcat! ;)