GenericServlet class
As the name suggest, it's a generic servlet and doesn't depend upon a particular protocol. It implements Servlet and ServletConfig interfaces and contains all the base code required by a servlet to maintain its life-cycle. As per the specification, Servlets are not restricted to a particular protocol and we can extend this class for any protocol, but we don't normally do that. Instead we use the already defined subclass (HTTPServlet) of this class specific to the HTTP protocol.
This class contains one abstract method called 'service(ServletRequest request ServletResponse response)'. We just need to define that methods in case we use this class to create servlets. This method is called by the servlet container on receipt of the request for that particular servlet and after that this method takes care of preparing the response for the incoming request.
ServletConfig interface
This interface has methods which are used by the servlet container to pass information to a servlet during the initialization of the servlet. The methods of this interface are:-
ServletContext interface
This interface has methods which are used for communicating with the servlet container. A Web APplication will have one context per JVM. The ServletContext object is contained within the ServletConfig object and hence every servlet gets it by the Web Server during its initialization. Few of the methods of this interface are:-
HTTPServlet class
This class extends the GenericServlet class to enable itself with not only the basic capabilities of a servlet, but also the HTTP protocol specific other capabilities like Cookies, Sessions, etc.
Similar to the GenericServlet class this class is also abstract and we extend it make the SubClass work like a servlet. The SubClass must override one of the following methods:-
service() method of HTTPServlet class is not abstract as was the case in the GenericServlet class and there is hardly any need to override that method as by default it contains the capability of deciding the type of the HTTP request it receives and based on that it calls the specific method to do the needful. For example, if an HTTP GET request calls the servlet then the service nethod of the HTTPServlet class determines the request type (it'll find it to be GET) and subsequently calls doGet() method to serve the request.
Difference between GenericServlet and HTTPServlet
HTTPServlet is a sub class of GenericServlet and it contains specific capabilities to handle the requests and responses of a particular protocol called 'HTTP' whereas the GenericServlet is protocol independent and as the name suggests it contains only the generic features which a servlet should have. Currently we use Servlets to handle HTTP requests only, but GenericServlet is capable enough to be extended for any other protocol as well.
Tuesday, June 3, 2008
GenericServlet, ServletConfig, ServletContext and HTTPServlet
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment