提问人:overexchange 提问时间:5/31/2015 更新时间:3/15/2021 访问量:26782
HttpServlet 类中的“service”方法是什么?
What is 'service' method in HttpServlet class?
问:
下面是一个为学习而编写的简单 servlet。
package com.example.tutorial;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello Java!");
}
}
当浏览器命中此 URI 时: ,http://localhost:8081/ServletsJSPExample/servletexample
通过分析http数据包的请求头,显示浏览器发送的请求。但是,在我的servlet中,我没有要处理的请求。GET
GET
所以
何时调用方法?service
为什么方法会收到此请求?service
GET
答:
25赞
Sotirios Delimanolis
5/31/2015
#1
HttpServlet
实现其服务
方法 javadoc 状态Servlet
由 servlet 容器调用,以允许 servlet 响应 请求。
这是所有 Servlet 处理的入口点。Servlet 容器实例化您的类,并在生成的实例上调用此方法,如果它确定您应该处理请求。Servlet
Servlet
HttpServlet
是一个类,它通过委托给相应的 、 、 方法来实现此方法,具体取决于请求中使用的 HTTP 方法。abstract
doGet
doPost
doXyz
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
if (!(req instanceof HttpServletRequest &&
res instanceof HttpServletResponse)) {
throw new ServletException("non-HTTP request or response");
}
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
service(request, response);
}
[...]
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);
}
}
如果覆盖 中的方法,则会丢失该行为并恢复到对所有 Servlet 请求的单个处理。service
HttpServlet
评论
This is an HTTP-specific version of the Servlet.service method, which accepts HTTP specific parameters. This method is rarely overridden. Standard HTTP requests are supported by dispatching to Java methods specialized to implement them.
这是来自javadoc的模糊答案