Servlet API in Java | Interfaces and Classes in java.servlet Package and javax.servlet.http package

Servlet uses classes and interfaces from two packages: javax.servlet and javax.servlet.http.They constitute servlet API. Keep in mind that these packages are not part of the Core java packages. Instead, they are a standard extension. Therefore, they are not included in the Java software development kit. you must download tomcat to obtain their functionality.

Every Servlets must implement the javax.servlet.Servlet interface. Most servlet implements this interface by extending one of two special classes javax.servlet.GenericServlet or javax.servlet.http.HttpServlet.

A GenericServlet class provides the implementation of the basic life cycle method of the servlet. The service() method of GenericServlet class accepts two parameters: a request object and a response object. The request object is used to read the data from a client request and the response object is used to write data to a client request.

In contrast, an HTTP servlet usually does not override the service() method. Instead, it overrides the doGet() method to handle the GET request and the doPost() method to handle the post requests. An HTTP servlet can override either or both of these methods, depending on the type of requests it needs to handle. An HTTP servlet can override doPut() and delete() methods to handle PUT and DELETE requests respectively. However,HTTP servlet generally don’t touch doTrace() or doOption().

java.servlet Package

java.Servlet Package has several interfaces and classes that provide the framework in which servlet operates. Classes and interfaces within the package are protocol-independent.

The ServletRequest and ServletResponse classes in javax.servlet is used to read the data from a client request and write the data to client response, While HttpServletRequest and HttpServletResponse in javax.Servlet.http package also contains an HttpSession class that provides built-in session tracking functionality and a cookie class that allows you to quickly set up and process HTTP cookies.

javax.servlet Package

Interfaces in javax.servlet package

There are various interfaces in javax.servlet package and they are listed in the following fashion

  1. servlet
  2. servletRequest
  3. servletResponse
  4. servletConfig
  5. servletContent
  6. servletThreadModel
  7. RequestDispatcher
  8. Filter
  9. Filterchain
  10. FilterConfig

Also, Refer: What is Servlet in Java?

Classes in javax.servlet package

There are many classes in javax.servlet package and they are listed as follows

  1. GenericServlet
  2. ServletInputStream
  3. ServletOutputStream
  4. servletRequestWrapper
  5. servletResponseWrapper
  6. ServletException
  7. unavailabale Exception
  8. ServletRequestEvent
  9. servletContentEvent
  10. servletRequestAttributeEvent

Interfaces in javax.servlet.http package

There are several interfaces in javax.servelt.http package and they are in the below fashion

  1. HttpServletRequest
  2. HttpServletResponse
  3. HttpSession
  4. HttpSessionContext
  5. HttpSessionAttributeLIstner
  6. HttpSessionActivationListner
  7. HttpSessionBindingListner

Classes in javax.servlet.http package

There are many classes in javax.servlet.http package and they are in the following way.

  1. HttpServlet
  2. cookie
  3. HttpServletRequestWrapper HttpServletResponseWrapper
  4. HttpSessionEvent
  5. HttpSessionBindingEvent
  6. Httputils