更改服务方法中的 doGet() 和 doPost() 方法?
谁能告诉我服务方法中的doGet()
和doPost()
方法如何变化?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
谁能告诉我服务方法中的doGet()
和doPost()
方法如何变化?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
如果客户端浏览器发送 GET 请求,它将由 doGet() 方法。
如果客户端浏览器发送 POST 请求,它将由 doPost() 方法。
这就是你想知道的吗? :O
更新:
在服务(HttpServletRequest req,
HttpServletResponse resp)方法,服务器使用HttpServletRequest.html#getMethod()。然后,它将该值与可用的 HTTP 方法类型进行比较,并调用相关的 doGet、doPost 或其他匹配方法。
请参阅 service(HttpServletRequest req, HttpServletResponse resp) 方法实现rel="nofollow">http://www.docjar.com/html/api/javax/servlet/http/HttpServlet.java.html
除了请求参数如何发送到服务器以及用法含义之外用于使用GET 和 POST 方法,据我所知没有其他变化。
if the client browser sends a GET request, it will handle by doGet() method.
if the client browser sends a POST request, it will handle by doPost() method.
Is that what you wanted to know? :O
UPDATE:
In side the service(HttpServletRequest req,
HttpServletResponse resp) method, server get the request method using HttpServletRequest.html#getMethod(). Then it compare that value with available HTTP method types and call the relevant doGet, doPost or other matching method.
See service(HttpServletRequest req, HttpServletResponse resp) method implementation in http://www.docjar.com/html/api/javax/servlet/http/HttpServlet.java.html
Other than how the request parameters are sent to the server and the usage meant for using GET and POST methods, there is no other change AFAIK.
因为,在 HttpServlet 类中实现了服务方法,并且所有用户定义的 servlet 类都必须扩展 Httpservlet 类。它是一个库类。在该类中实现了 2 个服务方法。一个是 public void 服务方法,它基本上是 Servlet 接口方法。
另一个是带有 2 个参数的重载 protected void 服务方法 - HttpServletRequest 和 HttpServletResponse。
公共服务方法调用受保护服务方法和受保护服务方法,其中有一些逻辑。它检查客户端请求是哪种类型。如果是 GET 类型,则调用 doGet() 方法,如果是 POST 类型,则调用 doGet() 方法调用 doPost() 方法。
这两个方法也在 protected service() 方法中实现,但是我们在用户定义的 servlet 类中重写这些方法来执行我们的任务。
Because,In HttpServlet class service method is implemented and all user defined servlet class must extends Httpservlet class.It is a library class.Inside that class 2 service methods are implemented .one is public void service method ,which is basically Servlet interface method.
and another is overloaded protected void service method with 2 parameter-HttpServletRequest and HttpServletResponse.
public service method calls protected service method and proteced service method ,which has inside some logic.It checks which type of client request has come.if it is GET type then it will call doGet() method and if it is of type POST than it calls doPost() method.
these 2 methods are also implemented in protected service() method but we override these methods in user defined servlet class to perform our task.
您可以使用
HttpServletRequest#getMethod()
(发出此请求)验证请求方法类型。You may verify the request method type using
HttpServletRequest#getMethod()
(which this request was made).