gpt4 book ai didi

servlets - doGet() 或 doPost 方法如何在内部调用 service() 方法

转载 作者:行者123 更新时间:2023-12-02 19:43:39 25 4
gpt4 key购买 nike

在 servlet 程序中,我们调用 doGet()doPost() 方法,但在 servlet 生命周期中,所有请求都将重定向到 service () 方法。但在 HTTP servlet 中,我们没有在程序中编写 service() 方法。那么它是如何调用service()的呢?请简单说明一下。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

.......// It is working fine but how internally it is calling service()
}

最佳答案

but how internally it is calling service()

事实并非如此。 The Javadoc of HttpServlet

Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:

  • doGet, if the servlet supports HTTP GET requests
  • doPost, for HTTP POST requests
  • doPut, for HTTP PUT requests
  • doDelete, for HTTP DELETE requests
  • init and destroy, to manage resources that are held for the life of the servlet getServletInfo, which the servlet uses to provide information about itself

There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above).

您的 HttpServlet 的自定义子类型继承 service() 方法。当 Servlet 容器决定应该使用您的 Servlet 时,它会调用继承的方法,该方法的实现如下

protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();

if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < lastModified) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}

} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);

} else if (method.equals(METHOD_POST)) {
doPost(req, resp);

} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);

} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);

} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);

} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);

} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//

String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);

resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}

通过继承,如果您重写了上述任何方法,则将使用您的实现。

关于servlets - doGet() 或 doPost 方法如何在内部调用 service() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com