Servlet API Tutorial

The javax.servlet and javax.servlet.http packages represent interfaces and classes for servlet api.

  • javax.servlet.*;
  • javax.servlet.http.*;

The javax.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.

The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.

Let's see what are the interfaces of javax.servlet package.

Classes in javax.servlet package

There are many classes in javax.servlet package.

  • GenericServlet
  • ServletInputStream
  • ServletOutputStream
  • ServletRequestWrapper
  • ServletResponseWrapper
  • ServletRequestEvent
  • ServletContextEvent
  • ServletRequestAttributeEvent
  • ServletContextAttributeEvent
  • ServletException
  • UnavailableException

Interfaces in javax.servlet package

There are many interfaces in javax.servlet package.

  • Servlet
  • ServletRequest
  • ServletResponse
  • RequestDispatcher
  • ServletConfig
  • ServletContext
  • SingleThreadModel
  • Filter
  • FilterConfig
  • FilterChain
  • ServletRequestListener
  • ServletRequestAttributeListener
  • ServletContextListener
  • ServletContextAttributeListener

Classes in javax.servlet.http package

There are many classes in javax.servlet.http package.

  • HttpServlet
  • Cookie
  • HttpServletRequestWrapper
  • HttpServletResponseWrapper
  • HttpSessionEvent
  • HttpSessionBindingEvent

Interfaces in javax.servlet.http package

There are many interfaces in javax.servlet.http package.

  • HttpServletRequest
  • HttpServletResponse
  • HttpSession
  • HttpSessionListener
  • HttpSessionAttributeListener
  • HttpSessionBindingListener
  • HttpSessionActivationListener

File: First.java
import java.io.*;
 import javax.servlet.*;

 public class First implements Servlet {
     ServletConfig config = null;
     public void init(ServletConfig config)
    {
     this.config = config;
 System.out.println("servlet is initialized");
     }
     public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException
     {
       res.setContentType("text/html");
       PrintWriter out = res.getWriter();
         out.print("<html><body>");
         out.print("hello simple servlet");
        out.print("</body> </html>");

     }
     public void destroy()
     {
         System.out.println("servlet is destroyed");
     }
     public ServletConfig getServletConfig()
     {
       return config;
     }
     public String getServletInfo()
     {
        return "copyright 2014-2015";

     }

 }
 
In this post, you will learn important line
  • What is a servlet API?