Why servlet is abstract
Collectives on Stack Overflow. Learn more. Abstract servlet - how to prevent instantiation Ask Question. Asked 5 years, 10 months ago. Active 5 years, 10 months ago. Viewed times. Improve this question. BalusC 1. Rames Rames 10 10 silver badges 26 26 bronze badges. Add a comment. Active Oldest Votes. It can process client requests and respond to data. We have always written servlets by implementing the Servlet interface. However, using this method, we must implement all the methods defined in the Servlet interface, even if there is nothing in some methods, and we also need to manually maintain the reference of the object ServletConfig.
Therefore, it is troublesome to implement servlets in this way. HttpServlet abstract class is inherited from GenericServlet abstract class. When using the HttpServlet abstract class, you also need to use the HttpServletRequest and HttpServletResponse objects representing the Servlet request and Servlet response respectively.
The HttpServletRequest interface is extended to the javax. ServletRequest interface, and the HttpServletResponse interface is extended to the javax. We can use the init method to perform the corresponding initialization.
When this method is called, the servlet container will pass in a ServletConfig object to initialize the servlet object. Just like people, you need to constantly accept the boss's instructions and "work". So defining the required functionalities upfront is a good approach. Is there any specific reason you think it should have been an abstract class rather than Servlet?
As far as I understand, it is how they designed the framework. They wanted to define the functionalities first and then come up with implementation at a later point. So interface first and then implementation.
Sean Clark. Class Summary Class Description Cookie Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. Provides a convenient implementation of the HttpServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet.
Events of this type are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session.
0コメント