java 我应该覆盖 service() 还是 doPost()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6822006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I override service() or doPost()?
提问by Arvind
I was reading a book on servlets, in that book a brief explanation is given about the servlet class, as well as the HttpServlet
class.
我正在阅读一本关于 servlet 的书,在那本书中给出了关于 servlet 类以及该类的简要说明HttpServlet
。
There is one example for filling in a form- for that form, the servlet's doPost()
method is overridden by the class. But for another example of a login form, the service()
method is overridden instead.
有一个填写表单的示例——对于该表单,servlet 的doPost()
方法被类覆盖。但是对于登录表单的另一个示例,该service()
方法被覆盖。
I want to know why the 2 different approaches- I thought that usually we put our custom code into doPost()
(or doGet()
) and let service()
remain as it is. Is there any reason behind using either one of the 2 approaches, or can I use both approaches in any situation?
我想知道为什么有两种不同的方法 - 我认为通常我们将自定义代码放入doPost()
(or doGet()
) 并service()
保持原样。使用这两种方法之一是否有任何原因,或者我可以在任何情况下使用这两种方法?
回答by Amir Raminfar
Do not override service()
method. The preferred approach is using doPost()
for post and doGet()
for get. Here is an excellent post on what each does. http://www.jguru.com/faq/view.jsp?EID=47730
不要覆盖service()
方法。首选方法是使用doPost()
for post 和doGet()
for get。这是一篇关于每个人做什么的优秀帖子。http://www.jguru.com/faq/view.jsp?EID=47730
If you must respond to requests made by a client that is not using the HTTP protocol, you must use service().
如果必须响应不使用 HTTP 协议的客户端发出的请求,则必须使用 service()。
回答by Costi Ciudatu
I think you need to understand the flow in order to decide for yourself. The default implementation of service()
for an HttpServlet
simply calls the appropriate handler for the request method (GET, POST, whatever).
我认为您需要了解流程才能自己做出决定。service()
for an的默认实现HttpServlet
只是为请求方法(GET、POST 等)调用适当的处理程序。
You need to override service()
when you want the same method to handle all incoming methods (no matter if it's a GET, PUT or POST request, you'll answer the same to all). If you're happy with treating each method separately, go with the default service() implementation and override the specific handlers.
service()
当您想要相同的方法来处理所有传入的方法时,您需要覆盖(无论是 GET、PUT 还是 POST 请求,您都会对所有方法做出相同的回答)。如果您对分别处理每个方法感到满意,请使用默认的 service() 实现并覆盖特定的处理程序。
回答by asgs
You most probably override the doXXX() method where XXX stands for the HTTP Methods like GET, POST, and so on. service()
method invoked by the container will decide which of the doXXX() to be called.
您很可能会覆盖 doXXX() 方法,其中 XXX 代表 HTTP 方法,例如 GET、POST 等。service()
容器调用的方法将决定调用哪个 doXXX()。
回答by sky
The service() method belongs to Genericservletand can be overloaded to support any type of protocol such as Http,Ftp etc.
service() 方法属于Genericservlet,可以重载以支持任何类型的协议,例如 Http、Ftp 等。
Then you have specialized servlet for handling HttpProtocol, we call it HttpServlet. The HttpServlet also provides default implementation for service() and doGet() and doPost() methods.
然后你有专门的 servlet 来处理 HttpProtocol,我们称之为HttpServlet。HttpServlet 还为 service() 和 doGet() 以及 doPost() 方法提供了默认实现。
Why we should not override the service() method?
为什么我们不应该覆盖 service() 方法?
Since It's not a good practice to override the service method. If we call any of the doxxx method then internally it will call the service method of the HttpServlet. So there's no need for you to call it explicitly.
因为覆盖服务方法不是一个好习惯。如果我们调用任何 doxxx 方法,那么它会在内部调用 HttpServlet 的服务方法。因此,您无需显式调用它。
Order of Execution of service():
service() 的执行顺序:
service(ServletRequest,ServletResponse)-->
服务(ServletRequest,ServletResponse)-->
-->calls
--> 通话
-->service(HttpServletRequest req,HttpServletResponse res)
--> 服务(HttpServletRequest req,HttpServletResponse res)
-->calls
--> 通话
-->doGet/doPost(HttpServletRequest req,HttpServletResponse res)
-->doGet/doPost(HttpServletRequest req,HttpServletResponse res)
This is how you can override the service in case you want to:
这是您可以覆盖服务的方式,以防万一:
protected void service(HttpServletRequest req, HttpServletResponse resp) {
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
doGet(req, resp);
} else if (method.equals(METHOD_HEAD)) {
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 {
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}}
Implementation code given by Tomasz Nurkiewiczfrom SO community only Overriding Service Method
由来自 SO 社区的Tomasz Nurkiewicz给出的实现代码仅 覆盖服务方法
回答by user2768783
If you must respond to GET or POST requests made by a HTTP protocol client (usually a browser) don't hesitate to extend HttpServlet and use its convenience methods. If you must respond to requests made by a client that is not using the HTTP protocol, you must use service()
如果您必须响应 HTTP 协议客户端(通常是浏览器)发出的 GET 或 POST 请求,请不要犹豫扩展 HttpServlet 并使用其便捷方法。如果必须响应不使用 HTTP 协议的客户端发出的请求,则必须使用 service()