You need to use Servlet API to create servlets. There are two packages that you must remember while using API, the javax.servlet
package that contains the classes to support generic servlet (protocol-independent servlet) and the javax.servlet.http
package that contains classes to support http servlet. You may be wondering what is generic and http servlet, I have explained them later in this post.
Let’s see the hierarchy of packages:
java.lang.Object |_extended byjavax.servlet.GenericServlet |_extended byjavax.servlet.http.HttpServlet
Every Servlet must implement the java.servlet.Servlet
interface, you can do it by extending one of the following two classes: javax.servlet.GenericServlet
or javax.servlet.http.HttpServlet
. The first one is for protocol independent Servlet and the second one for http Servlet.
How servlet works?
Generic Servlet
As I mentioned above, if you are creating a Generic Servlet then you must extend javax.servlet.GenericServlet
class. GenericServlet class has an abstract service() method. Which means the subclass of GenericServlet should always override the service() method.
Signature of service() method:
public abstract void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException
The service() method accepts two arguments ServletRequest object and ServletResponse object. The request object tells the servlet about the request made by client while the response object is used to return a response back to the client.
HTTP Servlet
If you creating Http Servlet you must extend javax.servlet.http.HttpServlet
class, which is an abstract class. Unlike Generic Servlet, the HTTP Servlet doesn’t override the service() method. Instead it overrides one or more of the following methods. It must override at least one method from the list below:
- doGet() – This method is called by servlet service method to handle the HTTP GET request from client. The Get method is used for getting information from the server
- doPost() – Used for posting information to the Server
- doPut() – This method is similar to doPost method but unlike doPost method where we send information to the server, this method sends file to the server, this is similar to the FTP operation from client to server
- doDelete() – allows a client to delete a document, webpage or information from the server
- init() and destroy() – Used for managing resources that are held for the life of the servlet
- getServletInfo() – Returns information about the servlet, such as author, version, and copyright.
In Http Servlet there is no need to override the service() method as this method dispatches the Http Requests to the correct method handler, for example if it receives HTTP GET Request it dispatches the request to the doGet() method.
Interfaces in javax.servlet package
- Servlet
- ServletRequest
- ServletResponse
- ServletConfig
- ServletContext
- SingleThreadModel
- RequestDispatcher
- ServletRequestListener
- ServletRequestAttributeListener
- ServletContextListener
- ServletContextAttributeListener
- Filter
- FilterConfig
- FilterChain
Classes in javax.servlet package
- GenericServlet
- ServletInputStream
- ServletOutputStream
- ServletException
- ServletRequestWrapper
- ServletRequestEvent
- ServletResponseWrapper
- ServletContextEvent
- ServletRequestAttributeEvent
- ServletContextAttributeEvent
- UnavailableException
Interfaces in javax.servlet.http package
- HttpSession
- HttpServletRequest
- HttpServletResponse
- HttpSessionAttributeListener
- HttpSessionListener
- HttpSessionBindingListener
- HttpSessionActivationListener
- HttpSessionContext
Classes in javax.servlet.http package
- HttpServlet
- Cookie
- HttpSessionEvent
- HttpSessionBindingEvent
- HttpServletRequestWrapper
- HttpServletResponseWrapper
- HttpUtils
References:
Leave a Reply