J a v a a n d J 2EE 10I S 753
UNIT -6: SERVLETS
Background
Definition : Servlets are modules of Java code that run in a server application (hence
the name "Servlets", similar to "Applets" on the client side) to answer client requests.
• Servlets are not tied to a specific client-server protocol but they are most commonly
used with HTTP and the word "Servlet" is often used in the meaning of "HTTP
Servlet".
• Servlets make use of the Java standard extension classes in the packages
javax. servlet (the basic Servlet framework) and javax. servlet .http
• Typical uses for HTTP Servlets include:
o Processing and/or storing data submitted by an HTML form.
o Providing dynamic content, e.g. returning the results of a database query to the
client.
o Managing state information on top of the stateless HTTP, e.g. for an online
shopping cart system which manages shopping carts for many concurrent
customers and maps every request to the right customer.
1. Servlet Life Cycle
The life cycle of a servlet is controlled by the container in which the servlet has been
deployed. When a request is mapped to a servlet, the container performs the following
steps:
1. If an instance of the servlet does not exist, the web container:
a. Loads the servlet class
b. Creates an instance of the servlet class
c. Initializes the servlet instance by calling the init method. Initialization is covered
in Initializing a Servlet
Dept. of ISE, SJBIT Page 54
, J a v a a n d J 2EE 10I S 753
2. Invokes the service method, passing a request and response object.
3. If the container needs to remove the servlet, it finalizes the servlet by calling the
servlet’s destroy method.
1.1 A servlet example
import java.io.*; import javax.servlet.*; import javax.servlet.http. *;
public class HelloClientServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html"); PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>Hello Client !</TITLE>"+
"</HEAD><BODY>Hello Client !</BODY></HTML>");
out.close();
}
public String getServletInfo()
{
return "HelloClientServlet 1.0 by Stefan Zeiger";
}
}
1.2. Servlet API
• Two packages contain the classes and interfaces that are required to build servlets.
They are javax.servlet and javax.servlet.http.
• The javax.servlet and javax.servlet.http packages provide interfaces and classes for
writing servlets. All servlets must implement the Servlet interface, which defines
life cycle methods.
1.3 The servlet packages :
Dept. of ISE, SJBIT Page 55
UNIT -6: SERVLETS
Background
Definition : Servlets are modules of Java code that run in a server application (hence
the name "Servlets", similar to "Applets" on the client side) to answer client requests.
• Servlets are not tied to a specific client-server protocol but they are most commonly
used with HTTP and the word "Servlet" is often used in the meaning of "HTTP
Servlet".
• Servlets make use of the Java standard extension classes in the packages
javax. servlet (the basic Servlet framework) and javax. servlet .http
• Typical uses for HTTP Servlets include:
o Processing and/or storing data submitted by an HTML form.
o Providing dynamic content, e.g. returning the results of a database query to the
client.
o Managing state information on top of the stateless HTTP, e.g. for an online
shopping cart system which manages shopping carts for many concurrent
customers and maps every request to the right customer.
1. Servlet Life Cycle
The life cycle of a servlet is controlled by the container in which the servlet has been
deployed. When a request is mapped to a servlet, the container performs the following
steps:
1. If an instance of the servlet does not exist, the web container:
a. Loads the servlet class
b. Creates an instance of the servlet class
c. Initializes the servlet instance by calling the init method. Initialization is covered
in Initializing a Servlet
Dept. of ISE, SJBIT Page 54
, J a v a a n d J 2EE 10I S 753
2. Invokes the service method, passing a request and response object.
3. If the container needs to remove the servlet, it finalizes the servlet by calling the
servlet’s destroy method.
1.1 A servlet example
import java.io.*; import javax.servlet.*; import javax.servlet.http. *;
public class HelloClientServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html"); PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>Hello Client !</TITLE>"+
"</HEAD><BODY>Hello Client !</BODY></HTML>");
out.close();
}
public String getServletInfo()
{
return "HelloClientServlet 1.0 by Stefan Zeiger";
}
}
1.2. Servlet API
• Two packages contain the classes and interfaces that are required to build servlets.
They are javax.servlet and javax.servlet.http.
• The javax.servlet and javax.servlet.http packages provide interfaces and classes for
writing servlets. All servlets must implement the Servlet interface, which defines
life cycle methods.
1.3 The servlet packages :
Dept. of ISE, SJBIT Page 55