
Servlet API consists of two packages which contain all the important classes and interfaces that we use to create servlets.
Java Servlet is just a java class which runs on the web server that contains an interpreter which supports the Java servlet specification.
In this article, we will discuss about servlet API, the packages of servlet API, Servlet Interface, GenericServlet class and HttpServlet class in details. You will have an example of servlet by using those main classes.
First look at the packages of the servlet API, it consists of two packages which are as follows:
- javax.servlet
- javax.servlet.http
Table of Contents
javax.servlet package of Servlet API
The javax.servlet package contains a list of interfaces and classes that the servlet or web container uses.
Classes of javax.servlet package
There are several classes in the javax.servlet package which are as follows:
- GenericServlet
- ServletContextAttributeEvent
- ServletContextEvent
- ServletException
- ServletInputStream
- ServletOutputStream
- ServletRequestAttributeEvent
- ServletRequestEvent
- ServletRequestWrapper
- ServletResponseWrapper
- UnavailableException
Interfaces of javax.servlet package
There are several interfaces in the javax.servlet package which are as follows:
- Filter
- FilterChain
- FilterConfig
- RequestDispatcher
- Servlet
- ServletConfig
- ServletContext
- ServletContextAttributeListener
- ServletContextListener
- ServletRequest
- ServletRequestAttributeListener
- ServletRequestListener
- ServletResponse
- SingleThreadModel
javax.servlet.http package of Servlet API
We use the classes and interfaces of the javax.servlet.http package to interact with the browser using the HTTP protocol. It is only for HTTP requests.
Classes of javax.servlet.http package
There are several classes in the javax.servlet.http package which are as follows:
- Filter
- Cookie
- HttpServlet
- HttpServletRequestWrapper
- HttpServletResponseWrapper
- HttpSessionBindingEvent
- HttpSessionEvent
- HttpUtils (deprecated now)
Interfaces of javax.servlet.http package
There are several interfaces in the javax.servlet.http package which are as follows:
- HttpServletRequest
- HttpServletResponse
- HttpSession
- HttpSessionActivationListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
- HttpSessionContext (deprecated now)
- HttpSessionListener
Servlet Interface
The Servlet interface defines some methods that all servlets implement if they use Servlet interface.
Methods of Servlet interface
There are mainly 5 methods in Servlet interface. The init(), service() and destroy() are the methods of servlet life cycle.
# | Methods | Description |
1 | public void init(ServletConfig config) | Servlet container calls this method to indicate that the servlet is being placed into service. |
2 | public void service(ServletRequest req, ServletResponse res) | This method is called by the servlet container to allow the servlet to respond to a request. |
3 | public void destroy() | This method is called by the servlet container to indicate to a servlet that the servlet is being taken out of service. |
4 | public ServletConfig getServletConfig() | This method returns a ServletConfig object, which contains initialization and startup parameters for this servlet. |
5 | public String getServletInfo() | This method returns information about the servlet, such as author, version, and copyright. |
Example of servlet by implementing the Servlet interface.
This program shows you how you can create a servlet by implementing the Servlet interface.
ServletProgram.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletProgram implements Servlet{
ServletConfig config = null;
public void init(ServletConfig config) {
this.config = config;
System.out.println("Initialization Complete");
}
public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter pwriter = res.getWriter();
pwriter.print("<html>");
pwriter.print("<body>");
pwriter.print("<h1>Welcome To GeekCer!</h1>");
pwriter.print("</body>");
pwriter.print("</html>");
}
public void destroy() {
System.out.println("Servlet life cycle finished");
}
public ServletConfig getServletConfig() {
return config;
}
public String getServletInfo() {
return "Copyright ©geekcer.com 2021 All Rights reserved";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ServletTutorials</display-name>
<servlet>
<servlet-name>ServletProgram</servlet-name>
<servlet-class>com.geekcer.ServletProgram</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletProgram</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
GenericServlet Class
GenericServlet defines a generic, protocol-independent servlet. It is an abstract class which implements the Servlet and ServletConfig interfaces.
By using GenericServlet class it is easy to write a servlet. It also provides lifecycle method init() and destroy() which is available in ServletConfig interface.
Example of servlet by extending the GenericServlet class.
ServletProgram.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ServletProgram extends GenericServlet{
public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Welcome to GeekCer!");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ServletTutorials</display-name>
<servlet>
<servlet-name>ServletProgram</servlet-name>
<servlet-class>com.geekcer.ServletProgram</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletProgram</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
HttpServlet Class
HttpServlet class does not override the service() method, it is more advance class than the GenericServlet class.
We use HttpServlet class to read the HTTP request and then it determines whether the request is HTTP GET, POST, PUT, DELETE, HEAD and calls one of the related method.
Example of servlet by extending the HttpServlet class.
ServletProgram.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletProgram extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Welcome to GeekCer!");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ServletTutorials</display-name>
<servlet>
<servlet-name>ServletProgram</servlet-name>
<servlet-class>com.geekcer.ServletProgram</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletProgram</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
To compare the HttpServlet class and GenericServlet class you can visit the following link:
What is the difference between HttpServlet and GenericServlet?