Ultimate Guide to Java Servlet API

Java Servlet API

Java Servlet API

Java Servlet API

Servlet API consists of two important packages that encapsulate all the important classes and interface, namely :

  • Javax.servlet
  • Javax.servlet.http

Some Important classes and Interfaces of Javax.servlet.

INTERFACES CLASSES
Servlet ServletInputStream
ServletContext ServletOutputStream
ServletConfig ServletRequestWrapper
ServletRequest ServletResponseWrapper
ServletResponse ServletRequestEvent
ServletContextListener ServletContextEvent
RequestDispatcher ServletRequestAttributeEvent
Single ThreadModel ServletException
Filter UnavailableException
FilterConfig GenericServlet
FilterChain
ServletRequestListener

Some Important classes and Interfaces of javax.servlet.http

CLASSES INTERFACES
HttpServlet HttpServletRequest
HttpServletResponse HttpSessionAttributeListener
HttpSession HttpSessionListener
Cookie HttpSessionEvent

Servlet Interface

  • Javax.servlet.Servlet is an interface which is root for all the servlets you are developing.
  • Java Servlet interface contains life cycle methods of a Java servlet.
  • In Servlet interface service() method signature is public void service(ServiceRequest, ServletResponse);

Generic Servlet-Abstract class

  • GenericServlet class is the direct subclass of Servlet interface.
  •  Overriding all the methods of Servlet interface except service() method because of that only GenericServlet is declared as abstract.
  • When you develop the servlet by extending GenericServlet then you have to override the service() method as follows:
    • void service(ServletRequest req, ServletResponse res);
  • It is protocol-independent class and it can be used if the client will send the request with any protocol.

HttpServlet-Abstract Class

  • HttpServlet class is the direct subclass of GenericServlet.
  • HttpServlet is overriding all the methods of GenericServlet, but HttpServlet is declared as abstract to restrict the container to create the object of HttpServlet class.
  • HttpServlet method() implementation is available but it is not for the Customs requirement.
  • HttpServlet is a protocol dependent servlet class and it will be used only if the client sends the request using the HTTP protocol.
  • When you develop the servlet by extending HttpServlet then you can override any of the following methods.
(generic service)
      • void service(ServletRequest req, ServletResponse res)
(http service)
    • void service(HttpServletRequest req, HttpServletResponse res);
    • void doGet(HttpServletRequest req, HttpServletRespnse res);
    • void doPost(HttpServletRequest req, HttpServletResponse res);
    • void doPut(HttpServletRequest req, HttpServletResponse res);
    • void doDelete(HttpServletRequest req, HttpServletResponse res);
    • void doHead(HttpServletRequest req, HttpServletResponse res);
    • void doTrace(HttpServletRequest req, HttpServletResponse res);
    • void doOptions(HttpServletRequest req, HttpServletResponse res);

Whatever, the servlet class you are extending or whatever, the method you are overriding. Container always calls the generic service method with ServletRequest and ServletResponse parameter.

Reason:
Servlet s = new HelloServlet();
s.service(req, res);
To hold your class reference container is using Servlet class object only.

Different cases for calling service() method:

Case 1: I have written HelloServlet by extending HttpServlet and overridden generic service() method.
class HelloServlet extends HttpServlet
{
public void service( Servlet.req, Servlet.res)
{
// some code
}
}

Explanation:
Servlet s = new HelloServlet();
s.service(S.req, s.res);
Container calls generic service() method and generic service() method which is overriden in the HelloServlet class will be called.

Case 2: I have written HelloServlet by extending HttpServlet and overridden HTTP service() method.
Class HttpServlet extends GenericServlet
{
public void service(Servlet.req, Servlet.res)
{
//calls http service method.
}
protected void service( HttpServlet.req, HttpServlet.res)
{
// some code
}
}
class HelloServlet extends HttpServlet
{
public void service( HttpServlet.req, HttpServlet.res)
{
// some code
}
}

Explanation:

  • The container calls a generic service() method.
  • generic service() method which is inherited to HelloServlet will be called.
  • Generic service() method of HttpServlet invokes the http service() method.
  • Http service() method of HelloServlet will be called.

Case 3: I have written HelloServlet by extending GenericServlet and overridden generic service() method

class HelloServlet extends GenericServlet
{
public void service (Servlet.req, Servlet.res)
{
//some code
}
}

Explanation:

  • The container calls a generic service() method.
  • Generic service() method which is overridden in HelloServlet will be called.
Case 4: I have written HelloServlet by extending GenericServlet and overridden HTTP service() method
class HelloServlet extends GenericServlet
{
public void service( HelloServlet.req, HelloServlet.res)
{
//some code
}
}

Explanation:

you will get compile time error because we have extended GenericServlet class which is an abstract class and we are not overriding their abstract method called service() with following signature service(Servlet.req. Servlet.res)

Case 5: I have written HelloServlet by extending HttpServlet and overridden Http service() method and generic service() method.
Class HelloServlet extends HttpServlet
{
public void service( HelloServlet.req, HelloServlet.res)
{
//some code
}
public void service(Servlet.req, Servlet.res)
// some code
}
}

Explanation:

  • The container calls the generic service method.
  • generic service() method which is overridden in HelloServlet will be called.
Case 6: I have written HelloServlet by extending HttpServlet and overridden doGet() method.
  • What will happen when I send the request to get() method.
  • What will happen when I send the request with a post() method.

// Default implementation of HttpServlet class methods.

Class HttpServlet extends GenericServlet
{
 public void service(Servlet.req, Servlet req)
  {
  // calls http service() method.
  }
 protected void service( HttpServlet.req, HttpServlet.res)
 {
 String m= req.getMethod();
 if(m.equals(“GET”))
  {
  doGet(res, req);
  }
else
 if(m.equals(“POST”))
 {
 doPost(req, res);
 }
...
 }
protected void doGet( HttpServlet.req, HttpServlet.res)
 {
 send the error with Http status code:405
 }
 protected void doPost( HttpServlet.req, HttpServlet.res)
 {
 send the error with Http status code:405
 }
 ..
}
class HelloServlet extends HttpServlet
 {
  public void doGet( HttpServlet.req, HttpServlet.res)
  {
  // some code
  }
 }

Explanation:

  • The container calls a generic service() method.
  • Generic service() method which is inherited to HelloServlet will be called.
  • Generic service() method of HttpServlet invokes the HTTP service() method.
  • Http service() method of HttpServlet will be called.
  • Http service() method of HttpServlet checks the incoming request HTTP method and invokes the corresponding doXX() method.

doGet() method will be called from HelloServlet.
doPost() method will be called from HttpServlet which gives the error with Status code:405.
Http method Post is not supported by this URL.

Q1. what will happen when I send the request to get() method.

Ans: Http service() method of HelloServlet will be called.

Q2. What will happen when I send the request with POST.

Ans: Http service() method of HelloServlet will be called.

Attribute

It is the element of name and value format when the name will be String type and value will be an Object type.
E.g
Customer cust= new Customer();
req.setAttribute(“CUST”, cust);

An attribute will be updatable for the developer. An attribute will be stored by the developer explicitly as well as if required it can be replaced from the corresponding object.

You can set the attribute in the following object.
  • ServletContext
  • HttpSession
  • ServletRequest
Depending on the object you are using to store the attribute following scope is available in Servlet
  • Context/ Application Scope
  • Session scope
  • Request scope

if the attribute stored in context object then it is known as context/Application scope.
These data can be accessed in any component of the application.

If the attribute stored in HttpSession object then it is known as Session scope
These data can be accessed by the client in multiple requests related to one session.

If the Attribute will be stored in ServletRequest object then it is known as Request scope
These data can be accessed by the client in one request only.

You can use the following method to manage attribute in multiple scopes:
  • public Object getAttribute(String name);
  • public void setAttribute (String name, Object obj);
  • public void removeAttribute(String name);

We can access all attribute name by;
public Enumeration getAttributeNames();

Conclusion:

In this article, we learned regarding the Java Servlet API,  different type of interfaces and classes present inside the servlet. we have seen that  In almost all cases, a servlet is created by extending HttpServlet class and implementing doGet() and doPost() method as these two are the most common Request types. Please feel free to drop a comment in the comment box below if you have any doubt regarding the topic or you want to share more knowledge regarding the topic. Happy Learning!

READ NOW :- Java servlet models

 

Leave a Reply

Your email address will not be published. Required fields are marked *