0% found this document useful (0 votes)
18 views18 pages

Module - 5 - Servlet, Architecture, Lifecycle of Servlet

The document provides an overview of Java Servlets, including their architecture, lifecycle, and types, specifically focusing on Generic and HTTP Servlets. It explains how Servlets handle requests and responses on a web server, detailing the Servlet API and the components involved in Servlet architecture. Additionally, it includes an example of a registration form implemented using a Servlet and HTML.

Uploaded by

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

Module - 5 - Servlet, Architecture, Lifecycle of Servlet

The document provides an overview of Java Servlets, including their architecture, lifecycle, and types, specifically focusing on Generic and HTTP Servlets. It explains how Servlets handle requests and responses on a web server, detailing the Servlet API and the components involved in Servlet architecture. Additionally, it includes an example of a registration form implemented using a Servlet and HTML.

Uploaded by

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

Course Title: Java Programming

Course Code: E1UA307C


Java Programming
Module 5
Introduction to Servlet, Servlet Architecture, Lifecycle of
a SERVLET
Important Link: https://www.studytonight.com/servlet/servlet-api.php
Introduction To Java Servlets
• Java Servlets are the Java programs that run on the Java-enabled web server or application server.
• They are used to handle the request obtained from the web server, process the request, produce the
response, and then send a response back to the web server.
• Java Servlets are used to create dynamic web pages i.e. the ones that can change the site contents
according to the time or can generate the content according to the request received from the client.

The properties of Servlets are as follows:


• Servlets work on the server side.
• Servlets are capable of handling complex requests obtained from the web server.

• There are three potential ways in which we can employ to create a servlet:
1. Implementing Servlet Interface
2. Extending Generic Servlet
3. Extending HTTP Servlet

2
Servlet API
• Servlets are built from two packages:
1. javax.servlet(Basic)
2. javax.servlet.http(Advance)

• The javax.servlet package contains many interfaces and classes that are used by the servlet or web
container. These are not specific to any protocol.
• The javax.servlet.http package contains interfaces and classes that are responsible for http requests
only.
Types of Servlet
• Generic Servlets: These servlets provide functionality for implementing a servlet. It is a generic class
from which all the customizable servlets are derived. It is protocol-independent and provides support
for HTTP, FTP, and SMTP protocols. The class used is ‘javax.servlet.Servlet’ and it only has 2
methods – init() to initialize & allocate memory to the servlet and destroy() to deallocate the servlet.

• HTTP Servlets: These are protocol dependent servlets, that provides support for HTTP request and
response. It is typically used to create web apps. And has two of the most used methods – doGET() and
doPOST() each serving their own purpose.
3
javax.servlet package
• Interfaces in javax.servlet package: Classes in javax.servlet package:
1. Servlet 1.GenericServlet
2. ServletRequest 2.ServletInputStream
3. ServletResponse 3.ServletOutputStream
4. RequestDispatcher
4.ServletRequestWrapper
5. ServletConfig
5.ServletResponseWrapper
6. ServletContext
6.ServletRequestEvent
7. SingleThreadModel
8. Filter 7.ServletContextEvent
9. FilterConfig 8.ServletRequestAttributeEvent
10.FilterChain 9.ServletContextAttributeEvent
11.ServletRequestListener 10.ServletException
12.ServletRequestAttributeListener 11.UnavailableException
13.ServletContextListener
14.ServletContextAttributeListener

4
javax.servlet.http package
Interfaces in javax.servlet.http package: Classes in javax.servlet.http package:
1.HttpServletRequest 1.HttpServlet
2.HttpServletResponse 2.Cookie
3.HttpSession 3.HttpServletRequestWrapper
4.HttpSessionListener 4.HttpServletResponseWrapper
5.HttpSessionAttributeListener 5.HttpSessionEvent
6.HttpSessionBindingListener 6.HttpSessionBindingEvent
7.HttpSessionActivationListener 7.HttpUtils (deprecated now)
8.HttpSessionContext (deprecated now)

5
Servlet Architecture
• The most common used type of servlet is an HTTP servlet which extends
the javax.servlet.http.HttpServlet class which is a subclass
of javax.servlet.GenericServlet.
• When you create a servlet, that extends
the javax.servlet.http.HttpServlet class, you must override one or more
methods to respond to specific HTTP requests.

6
Cont..

1. When a client request is received by a web server, it will deliver the request to the servlet container
that will start and deliver the request to the servlet.
2. The Servlet receives the request and returns a response to its container which will deliver the response
to the web server that returns the response to the client.

7
Other Diagrams

8
Components of Servlet Architecture
1. Client: The client shown in the architecture above is the web browser and it primarily works as a
medium that sends out HTTP requests over to the web server and the web server generates a response
based on some processing in the servlet and the client further processes the response.
2. Web Server: Primary job of a web server is to process the requests and responses that a user sends
over time and maintain how a web user would be able to access the files that has been hosted over the
server. The server we are talking about here is a software which manages access to a centralized resource
or service in a network. There are precisely two types of webservers:
• Static web server
• Dynamic web server
3. Web Container:
Web container is another typical component in servlet architecture which is responsible for
communicating with the servlets. Two prime tasks of a web container are:
• Managing the servlet lifecycle
• URL mapping
Web container sits at the server-side managing and handling all the requests that are coming in either
from the servlets or from some JSP pages or potentially any other file system.
9
Servlet Life Cycle
1.Loading Servlet Class : A Servlet class is loaded when first request for the
servlet is received by the Web Container.
2.Servlet instance creation :After the Servlet class is loaded, Web Container
creates the instance of it. Servlet instance is created only once in the life cycle.
3.Call to the init() method : init() method is called by the Web Container on
servlet instance to initialize the servlet.
Signature of init() method :
public void init(ServletConfig config) throws ServletException
4. Call to the service() method : The containers call the service() method each
time the request for servlet is received. The service() method will then call
the doGet() or doPost() methos based ont eh type of the HTTP request, as explained
in previous lessons.
Signature of service() method :
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
5. Call to destroy() method: The Web Container call the destroy() method before
removing servlet instance, giving it a chance for cleanup activity. 10
Cont..

11
How a Servlet works
1. User sends request for a servlet by clicking a link that has URL to a servlet.

2. The container finds the servlet using deployment descriptor and creates two objects :
• HttpServletRequest
• HttpServletResponse

12
3. Then the container creates or allocates a thread for that request and calls the Servlet's service() method
and passes the request, response objects as arguments.

4. The service() method, then decides which servlet method, doGet() or doPost() to call, based on HTTP
Request Method(Get, Post etc) sent by the client. Suppose the client sent an HTTP GET request, so the
service() will call Servlet's doGet() method.

3 4
13
5. Then the Servlet uses response object to write the response back to the client.

6. After the service() method is completed the thread dies. And the request and response objects are
ready for garbage collection.

14
Example: Registration Form
web.xml:
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >
<servlet>
<servlet-name>MyRegisterServ</servlet-name>
<servlet-class>MyRegisterServ</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyRegisterServ</servlet-name>
<url-pattern>/MyRegisterServ</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
15
MyRegisterServ.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class MyRegisterServ extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String email = request.getParameter("email");
String pass = request.getParameter("pass");

16
try {
// loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
//creating connection with the database
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/Database_name","root","root");
PreparedStatement ps = con.prepareStatement
("insert into TableName values(?,?,?)");
ps.setString(1, name);
ps.setString(2, email);
ps.setString(3, pass);
int i = ps.executeUpdate();
if(i > 0) {
out.println("You are successfully registered");
}
}
catch(Exception se) {
se.printStackTrace();
}
}
}
17
index.html

<html>
<head>
<title>Students Registration form</title>
</head>
<body>
<center>
<form method="post" action="MyRegisterServ">
Name:<input type="text" name="name" /><br/><br/>
Email ID:<input type="text" name="email" /><br/><br/>
Password:<input type="text" name="pass" /><br/><br/>
<input type="submit" value="REGISTER" /><br/>
</form>
</center>
</body>
</html>

18

You might also like