0% found this document useful (0 votes)
38 views20 pages

Servlet API

Uploaded by

Navneet Sheoran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views20 pages

Servlet API

Uploaded by

Navneet Sheoran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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

CLASSES and INTERFACES

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 :

public interface Servlet


Methods of Servlet interface

S.No. Method Description

It is used for initializing the


public void init servlet. It is invoked only once by
1.
(ServletConfigconfig) the web container in a servlet life
cycle.
It is used for providing a re-
public void service sponse to all the incoming re-
2. (ServletRequestreq, quest. It is invoked every time by
ServletResponse res) the web container for each re-
quest.
It is used for destroying the
3. public void destroy() servlet. It is invoked only once in
a life cycle of a servlet.

public ServletConfigget- It is used to get the object of


4.
ServletConfig() ServletConfig.

It is used to get information


5. Public String getServletInfo() about writer, copyright etc of a
servlet.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Navneet Sheoran => servlet interface example</title>
</head>
<body>
<h1>Navneet sheoran</h1><br><br>
************************************<br><br>
<h3><a href="demo">Click here to proceed...</a></h3><br><br>

************************************<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;

public class marks extends HttpServlet{


public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException
{
int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k = Integer.parseInt(req.getParameter("num3"));
int l = Integer.parseInt(req.getParameter("num4"));
int m = Integer.parseInt(req.getParameter("num5"));
int n = Integer.parseInt(req.getParameter("num6"));
int total = i + j + k + l + m + n;
float avg = total / 6;

PrintWriter out = res.getWriter();


out.println("Maths : " + i );
out.println("English : " + j );
out.println("Hindi : " + k);
out.println("Science : " + l);
out.println("Social Science : " + m);
out.println("IT : " + n);
out.println("Total Marks : "+ total);
out.println("Average: "+avg);
}

}
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:

Methods of GenericServlet interface

Implemented Interfaces:

java.io.Serializable, Servlet, ServletConfig


Constructor:

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-

public Enumeration getInitParam- It is used for getting parameters


9
eterNames() which are defined in web.xml files
It is used for getting the name of
10 public String getServletName()
a servlet object.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Studytonight.com</title>
</head>
<body>
<form action="sal" align="center">
<h3 align="center">studytonight.com</h3>
<h3 align="center">--------------------------------------------------------</h3><br><br>
Enter Basic Salary <input type="text" name="num1"><br><br>
Enter Basic DA <input type="text" name="num2"><br><br>
Enter Basic HRA <input type="text" name="num3"><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">
<display-name>Generic_Servlet</display-name>
<servlet>
<servlet-name>abc1</servlet-name>
<servlet-class>salary</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc1</servlet-name>
<url-pattern>/sal</url-pattern>
</servlet-mapping>
</web-app>
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 salary extends GenericServlet


{
/**
*
*/
private static final long serialVersionUID = 1L;

public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException


{
int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k = Integer.parseInt(req.getParameter("num3"));
int da = (j * i) / 100;
int hra = (k * i) / 100;
int g = i + da + hra;
PrintWriter out = res.getWriter();
out.println("studytonight.com");
out.println("DA : "+da);
out.println("HRA : "+hra);
out.println("Gross Salary : "+g);
}
}

You might also like