Generic Servlet and Http Servlet

SHARE & COMMENT :

Generic Servlet

GenericServlet is an abstract class defined in the Servlet API. For implementing class we need to implement all the methods in javax.servlet.Servlet Interface. Most of the Servlet objects do not need initializing and destroying operations but still we need to implement all the methods in javax.servlet.Servlet interface. To solve this problem, Servlet API provides javax.servlet.GenericServlet which implements ServletConfig interface and provides the implementation for the methods in this interface except the service method.

Methods of GenericServlet class

  • public void init (): This method is invoked by init (ServletConfig) method of GenericServlet. It is used to provide custom initializations without disturbing the initializations performed by GenericServlet in the init (ServletConfig) method.
  • public void init (ServletConfig) : It is used to initialize the servlet. It indicates Servlet instance in being placed into the service.
  • public abstract void service (ServletRequest request, ServletResponse response) : It is used to process user request. It is called by servlet container to intimate servlet about client request. It carries out single request from the client. ServletRequest object is used to collect data which is available with client requested data. ServletResponse object is used to generate the output content.
  • public void destroy (): It indicates servlet instance is being taken out of service. It is used to clean up any resources that servlet might have initialized.
  • public ServletConfig getServletConfig (): It returns ServletConfig interface reference. It is used to get configuration information from web.xml file.
  • public ServletContext getServletContext (): It returns ServletContext object reference. It is used to get configuration information from web.xml file. It is also used to set, get or remove attribute from web.xml file. If information is changed then there is no need to modify the servlet. So it is easy to maintain.
  • public String getInitParameter (String name): It returns the Servlet initialization parameter of the given name and if requested parameter is not available then it returns null.
  • public Enumeration String getInitParameternames (): It returns names of all Servlet initialization parameters defined in web.xml file.
  • public String getServletInfo (): It returns information about the respective servlet. For e.g. version, copyright.
  • public String getServletName (): It returns the Servlet instance name defined in Deployment Descriptor (web.xml).
  • public void log (String msg): It writes class name of servlet and specified message to a servlet log file.
  • public void log (String msg, Throwable t): It writes explanatory message and stack trace for a given Throwable exception to the servlet log file.

Http Servlet

HttpServlet is an abstract class defined in the Servlet API. It is abstract class with no abstract methods. Servlet container provides support for the Http protocol. It creates HttpServletRequest and HttpServletResponse objects if HTTP protocol is used for the sending request.
HttpServletRequest extends ServletRequest interface to provide request information for servlets. HttpServletResponse extends ServletResponse interface which provides functionality in sending response. HttpServlet is subtype of GenericServlet and does not override init, destroy and other methods. However, it implements the service () method which is abstract method in GenericServlet.

Methods of HttpServlet class

  • public void service (ServletRequest request, ServletResponse response): This method is used to process the user request. For each request the web container will issue unique request and response to the service method.
  • public void service (HttpServletRequest request, HttpServletResponse response): This method receives requests from the service () method. It dispatches requests depending on the request type.
  • protected void doGet (HttpServletRequest request, HttpServletResponse response): By using doGet () method we can send specific amount of data. If we use doGet () method data is shown in address bar. We must override doGet () method depending on type of request.
  • protected void doPost (HttpServletRequest request, HttpServletResponse response): We can send large amount of data by using doPost () method. By using this method data is not viewable in address bar. When we want to send secure data like passwords and other things doPost () method is used. We must override doPost () method depending on type of request.
  • protected void doDelete (HttpServletRequest request, HttpServletResponse response): It is used to delete files, web pages or documents from the server. If requests are formatted incorrectly then it will return HTTP “Bad Request” error.
  • protected void doPut (HttpServletRequest request, HttpServletResponse response): This method is used to put files, web pages or documents in the server means for uploading files on the server. If requests are formatted incorrectly then it will return HTTP “Bad Request” error.
  • protected void doTrace (HttpServletRequest request, HttpServletResponse response): This method is used for logging and debugging purpose. It can be used for testing the requested message. There is no need to override this method.
  • protected void doOptions (HttpServletRequest request, HttpServletResponse response): This method handles OPTIONS request. There is no need to override this method. It determines which HTTP method supported by server and returns correct header.
  • protected long getLastModified (HttpServletRequest request, HttpServletResponse response): It returns the time when request was last modified. This method should override GET request to return modification time of object.
  • protected void doHead (HttpServletRequest request, HttpServletResponse response): This method request header part of the GET request without the GET response body. It receives request from service method and handles the request. If HEAD requests are formatted incorrectly then it will return HTTP “Bad Request” error.

Previous Tutorial : Servlet LifeCycle  || Next Tutorial : Servlet Example

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better