Servlet is a server-side technology used to send the data through  servlets classes from the client.

There are three ways to create servlets,

  • Servlet Interface
  • Generic Servlet class
  • HttpServlet class

In general, HttpServlet classes are recomended to use in java program's

Servlet Interface

This interface provides common behaviour to all the servlets and servlet will be the root to access all servlet classes.

Methods for Servlet Interface

  • public void init(ServletConfig config)

This method used to initialize the process in the cycle.

  • public void service(ServletRequest request, ServletResponse response)

This method is used to send the request from the client browser and send the response back to the client browser

  • public string getStringConfig()

This method is used to return the information like version, details of the servlet

  • public ServletConfig getServletConfig()

 This method is used to get the information regarding the web file.

  • public void destroy()

This method is used to destroy the process after execution.

 

Generic Servlet Class

Generic Servlet doesn't depend protocol and can handle any type of requests.

It is a abstract class and it has abstract method service().

 

HttpServlet Class

HttpServlet is an abstract class which is provided by API.

It depends on protocol and only HTTP requests are accepted.

For requests and responses, this API provides the interfaces like HttpServletRequest and HttpServletResponse

Methods for HttpServlet Class

  1. protected void doGet(HttpServletRequest req, HttpServletResponse res)
  2. protected void doPost(HttpServletRequest req, HttpServletResponse res)
  3. protected void doHead(HttpServletRequest req, HttpServletResponse res)
  4. protected void doOptions(HttpServletRequest req, HttpServletResponse res)
  5. protected void doPut(HttpServletRequest req, HttpServletResponse res)
  6. protected void doTrace(HttpServletRequest req, HttpServletResponse res)
  7. protected void doDelete(HttpServletRequest req, HttpServletResponse res)
  8. public void service(ServletRequest req,ServletResponse res)
  9. protected void service(HttpServletRequest req, HttpServletResponse res)
blog comments powered by Disqus