Can you override service method in servlet
It looks like you are using an AD Blocker! Disable Ad Blocker Learn More. Login GeekInterview Login. Create your GeekInterview account Signup. Continue Reading after Disabling Refresh. Since the routing takes place in service , there is no need to generally override service in an HTTP servlet.
Instead, override doGet , doPost , and so on, depending on the expected request type. The automatic routing in an HTTP servlet is based simply on a call to request. In a Sun Java System Web Server, request data is already preprocessed into a name-value list by the time the servlet sees the data, so simply overriding the service method in an HTTP servlet does not lose any functionality.
However, this does make the servlet less portable, since it is now dependent on preprocessed request data. Very often, these tasks collate the needed information in the request object or in a JDBC result set object , and then passing the newly generated content to a JSP for formatting and delivery back to the client.
Implement both methods to provide for both input types or pass the request object to a central processing method, as shown in the following example:. For this reason, be sure to implement routines for all possible input types. If you must respond to requests made by a client that is not using the HTTP protocol, you must use service. I think you need to understand the flow in order to decide for yourself.
If you're happy with treating each method separately, go with the default service implementation and override the specific handlers. 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. 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. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Should I override service or doPost?
0コメント