如果我连续刷新页面,HTTP GET 方法是否每次刷新都会调用 servlet 的 doGet() 方法?
If I refresh a page continuously, does HTTP GET method invokes doGet() method of a servlet every time for every refresh?
我正在尝试 post 使用 HTTP GET 方法获取一些数据。当然,我应该选择POST方法来post数据。
这里的问题是,如果我连续刷新页面,每次刷新,HTTP GET方法是否每次都会调用servlet的doGet()方法?
我的理解是,在 POST 方法的情况下,每次刷新都会调用 servlet 的 doPOst() 方法。
我的 html 页面如下所示:
<!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>
如果您点击刷新按钮(F5 或 Ctrl+R),那么它只会通过 GET 方法访问应用程序,即使您在表单中提到 POST 但您正在点击应用程序页面仅 URL。
我认为 spec 没有具体说明这一点,但为什么不在您的代码中加入调试语句并找出答案呢?我认为这是找出特定设置中发生的情况的最快方法
答案是肯定的。在 GET 方法的情况下,每次页面刷新都会调用 doGet() 方法!
我们可以通过在 init() 方法中初始化全局变量并在每次调用 doGet() 或 doPost() 方法时递增全局变量来调试此场景。
以下代码演示了相同的内容:
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>");
}
}
您可以观察到对于页面刷新,hitCount 正在增加!!!
我正在尝试 post 使用 HTTP GET 方法获取一些数据。当然,我应该选择POST方法来post数据。
这里的问题是,如果我连续刷新页面,每次刷新,HTTP GET方法是否每次都会调用servlet的doGet()方法?
我的理解是,在 POST 方法的情况下,每次刷新都会调用 servlet 的 doPOst() 方法。
我的 html 页面如下所示:
<!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>
如果您点击刷新按钮(F5 或 Ctrl+R),那么它只会通过 GET 方法访问应用程序,即使您在表单中提到 POST 但您正在点击应用程序页面仅 URL。
我认为 spec 没有具体说明这一点,但为什么不在您的代码中加入调试语句并找出答案呢?我认为这是找出特定设置中发生的情况的最快方法
答案是肯定的。在 GET 方法的情况下,每次页面刷新都会调用 doGet() 方法!
我们可以通过在 init() 方法中初始化全局变量并在每次调用 doGet() 或 doPost() 方法时递增全局变量来调试此场景。
以下代码演示了相同的内容:
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>");
}
}