Issue
I'm trying to post some data using HTTP GET method. Of course, I should choose POST method to post the data.
Here the question is, if I refresh a page continuously, for every refresh, does HTTP GET method invokes doGet() method of a servlet every time?
My understanding is that in case of POST method, every time it invokes doPOst() method of a servlet for every refresh.
My html page looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>user registration</title>
</head>
<body>
<form action="hello" method="get">
User Name: <input type="text" name="userName" /> <br> <br> User Id: <input
type="text" name="userId" /> <br><br>
Select the profession:
<input type="radio" value="Developer" name="proffession">Developer
<input type="radio" value= "Architect" name="profession">Architect<br><br>
Select your location:
<select name="location" multiple size=3>
<option value="Bangalore">Bangalore</option>
<option value="Mangalore">Mangalore</option>
<option value="Udupi">Udupi</option>
<option value="Bhatkal">Bhatkal</option>
</select><br><br>
<input type="submit">
</form>
</body>
</html>
Solution
Answer is Yes. doGet() method will be invoked for every page refresh in case of GET method as well!
We can debug this scenario by initializing a global variable in init() method and increment the global variable every time either doGet() or doPost() method is called.
Following code demonstrate the same:
package org.shan.jspservlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class XmlServlet extends HttpServlet {
private int hitCount;
public void init()
{
// Reset hit counter.
hitCount = 0;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
String userId = request.getParameter("userId");
response.setContentType("text/html");
hitCount++;
response.getWriter().print(
"<html><body><h1> Hello from XML servlet to " + userName
+ "</h1></body></html>");
response.getWriter().print(
"<html><body><h1> Your id is: " + userId
+ "</h1></body></html>");
response.getWriter().print("hitcount: "+ hitCount);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String userName = request.getParameter("userName");
String userId = request.getParameter("userId");
String profession = request.getParameter("profession");
// String location = request.getParameter("location");
String location[] = request.getParameterValues("location");
response.setContentType("text/html");
response.getWriter().print(
"<html><body><h1> Hello from XML servlet to " + userName
+ "</h1></body></html>");
response.getWriter().print(
"<html><body><h1> Your id is: " + userId
+ "</h1></body></html>");
response.getWriter().print(
"<html><body><h1> Your profession is: " + profession
+ "</h1></body></html>");
response.getWriter().print(
"<html><body><h1> Your at following " + location.length
+ " palces!!!</h1></body></html>");
for (int i = 0; i < location.length; i++)
response.getWriter().print(
"<html><body><h1>" + location[i]
+ "</h1></body></html>");
}
}
Answered By - owgitt
Answer Checked By - Senaida (JavaFixing Volunteer)