Should I override service() or doPost()?

有些话、适合烂在心里 提交于 2019-12-30 08:05:08

问题


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.

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.

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?


回答1:


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

If you must respond to requests made by a client that is not using the HTTP protocol, you must use service().




回答2:


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).

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.




回答3:


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.




回答4:


The service() method belongs to Genericservlet and can be overloaded to support any type of protocol such as Http,Ftp etc.

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.

Why we should not override the service() method?

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.

Order of Execution of service():

service(ServletRequest,ServletResponse)-->

-->calls

-->service(HttpServletRequest req,HttpServletResponse res)

-->calls

-->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 Nurkiewicz from SO community only Overriding Service Method




回答5:


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()



来源:https://stackoverflow.com/questions/6822006/should-i-override-service-or-dopost

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!