Servlet API
Servlet API
Servlet API consists of two important packages that encapsulates 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
SingleThreadModel ServletContextAttributeEvent
Filter ServletException
FilterConfig UnavailableException
FilterChain GenericServlet
ServletRequestListener
Some Important Classes and Interface of javax.servlet.http
HttpServlet HttpServletRequest
HttpServletResponse HttpSessionAttributeListener
HttpSession HttpSessionListener
Cookie HttpSessionEvent
Servlet Interface
In Java, An interface is used for the development of servlet. This interface is known as the servlet inter-
face. This interface is implemented by all the interfaces. The servlet interface is used for the declaration
of init(), service(), and destroy() method. These methods are called by the server during the life cycle of
a servlet. The getServletConfig() method is called by the servlet to initialize the parameters. And the
getServletInfo() method is used for providing important information.
Servlet Interface provides only five methods. Out of these five methods, three methods are of Servlet
life cycle methods and rest two are non-life cycle methods.
Declaration :
************************************<br><br>
</body>
</html>
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://
xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_4_0.xsd"id="WebApp_ID"version="4.0">
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>
import java.io.*;
import javax.servlet.*;
public class DemoServlet implements Servlet{
ServletConfig config=null;
public void init(ServletConfig config){
this.config=config;
}
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>Hello Welcome to Here. This example is of servlet interface. </h1>");
pwriter.print("</body>");
pwriter.print("</html>");
}
public void destroy(){
System.out.println("servlet destroy");
}
public ServletConfig getServletConfig(){
return config;
}
public String getServletInfo(){
return "studytonight.com";
}
}
HttpServlet class
HttpServlet is also an abstract class. This class gives implementation of various service() methods
of Servlet interface.
To create a servlet, we should create a class that extends HttpServlet abstract class. The Servlet class
that we will create, must not override service() method. Our servlet class will override only
the doGet() and/or doPost() methods.
The service() method of HttpServlet class listens to the Http methods (GET, POST etc) from request
stream and invokes doGet() or doPost() methods based on Http Method type.
Methods of HttpServlet interface
S.No. Method Description
It is used for securing the service
public void service(ServletRequest
1 method by creating objects of re-
req,ServletResponse res)
quest and response.
protected void service
It is used for receiving a service
2 (HttpServletRequest req, HttpS-
method.
ervletResponse res)
protected void doGet It is invoked by the web container
3 (HttpServletRequest req, HttpS- and it is used for handling the GET
ervletResponse res) request.
protected void doPost
It is invoked by the web container
4 (HttpServletRequest req, HttpS-
and it handles the POST request.
ervletResponse res)
protected void doHead
It is invoked by the web container
5 (HttpServletRequest req, HttpS-
and it handles the HEAD request.
ervletResponse res)
protected void doOptions It is invoked by the web container
6 (HttpServletRequest req, HttpS- and it handles the OPTIONS re-
ervletResponse res) quest.
protected void doPut It is invoked by the web container
7 (HttpServletRequest req, HttpS- and it handles the OPTIONS re-
ervletResponse res) quest.
protected void doTrace
It is invoked by the web container
8 (HttpServletRequest req, HttpS-
and it handles the TRACE request
ervletResponse res)
protected void doDelete
It is invoked by the web container
9 (HttpServletRequest req, HttpS-
and it handles the DELETE request.
ervletResponse res)
protected long getLastModified It is used for getting the time of
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="mar" align="center">
<h3 align="center">Navneet Sheoran</h3>
<h3 align="center">--------------------------------------------------------</h3>
Enter marks of the following subjects<br><br><br>
Maths : <input type="text" name="num1"><br><br>
English : <input type="text" name="num2"><br><br>
Hindi : <input type="text" name="num3"><br><br>
Science : <input type="text" name="num4"><br><br>
Social Science : <input type="text" name="num5"><br><br>
IT : <input type="text" name="num6"><br><br>
<input type="submit">
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://
xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://
xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
< <servlet>
<servlet-name>abc2</servlet-name>
<servlet-class>marks</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc2</servlet-name>
<url-pattern>/mar</url-pattern>
</servlet-mapping>
</web-app>
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
}
GenericServlet class
In Servlet, GenericServlet is an abstract class. This class implements the servlet, ServletConfig and Se-
rializable interface. This class provides the implementation of most of the basic servlet methods. The
protocol of this class is independent as it can handle any type of request.
Class:
Implemented Interfaces:
GenericServlet() : this constructor does nothing. Everything is initialized by the init method.
S.NO. Method Desciption
public void init(ServletConfig con- It is used for initialization of a
1
fig) servlet.
It is used for providing all the ser-
public abstract void service
vices for the incoming request.
2 (ServletRequest request,
When a user request then only it
ServletResponse response)
invokes.
It is used for destroying the
3 public void destroy()
servlet. It is invoked only once in a
public ServletConfig get- It is used to get the object of
4
ServletConfig() ServletConfig
It is used to get information about
5 public String getServletInfo()
writer, copyright etc of a servlet.
It is a very easy and convenient
6 public void init()
method for programmers.
public ServletContext get- It is used for getting object of a
7
ServletContext() servlet
public String getInitParameter It is used for getting all the pa-
8
(String name) rameter values from the given pa-
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;