Dr. N.G.P.
Institute of Technology
Course Code and Name: 22UAD502 & Full Stack Development
Class: III - B AI-DS
Faculty Handling: Ms R. Madhuramya , Assistant Professor/ AI-DS
UNIT III
SERVER SIDE PROGRAMMING
Servlets: Java Servlet Architecture- Servlet Life Cycle- Form GET and
POST actions- Session Handling- Understanding Cookies-
DATABASE CONNECTIVITY: JDBC
UNIT II - SERVER SIDE PROGRAMMING
3.1 An Introduction to Server Side Programming
3.2 Java Servlet Architecture
3.3 Servlet Life Cycle
3.4
3.5
Introduction to Server Side Programming
Server-Side Programming means that writing scripts that are executed on the
server and are then translated into HyperText Markup Language (HTML)
which can be viewed by all web browsers.
Servlets – An Overview
Servlets are Java programs that run on a web server, acting as a bridge
between client requests (e.g., browser) and server-side resources (like
databases).
Key Uses:
● Handle form input from users
● Generate dynamic web pages
● Interact with databases and other applications
● Enable secure access and session tracking
Types of web server responses
1. Static Response: HTML document retrieved from the file system and returned
to the client is called static response.
2. Dynamic Response: HTML document is generated by a program in response to
an HTTP request
Java Servlet Architecture
● Servlet architecture comes under a java programming language used
to create dynamic web applications.
● Mainly servlets are used to develop server-side applications.
● Servlets are very robust and scalable.
There are two types of Servlets-
1.Generic Servlets
2.HTTPServlets.
Java Servlet Architecture
Servlets can be created in three possible ways
1) Implementing Servlet Interface
2) Extending Generic Servlet.
3) Extending HTTPServlet.
Three life cycle methods available with servlets are init(), service()
and destroy(). Every servlet should override these methods.
Components of Servlet Architecture
Components of Servlet Architecture
1. Client
The web browser acts as the Client, sending HTTP requests to the web server and
processing the responses it receives.
2. Web Server
A web server handles client requests using the HTTP protocol. It locates the requested file
and sends it back via an HTTP response.
Types:
Static Server – Sends files as-is
Dynamic Server – Processes and updates files before sending
Components of Servlet Architecture
3. Web Container
The Web Container manages Servlets and JSP on the server. It handles their lifecycle,
maps URLs to servlets, and processes client requests and responses.
Key Roles:
● Load/unload servlets
● Manage request/response objects
● Handle servlet execution
Servlet Request Flow
Every servlet should override the following 3 methods namely:
1. init()
2. service()
3. destroy()
Servlet Request-Response Flow
● Client sends a request
● Web Server forwards it to the Web Container
● Web Container checks web.xml for servlet mapping
● If needed, servlet is instantiated and init() is called
● public service() is called with request/response objects
● Objects are typecast to HttpServletRequest/Response
● protected service() determines HTTP method (GET, POST, etc.)
● Corresponding doXXX() method (e.g., doGet()) is called
● Response is sent back to the client
Uses of Servlet Architecture
● Handle form data and generate dynamic HTML
● Support server load balancing
● Connect to SQL databases in enterprise apps
● Integrate with applets for interactive web content
● Act as middleware to share data between components
● Enable file servers, chat apps via protocol support (HTTP, FTP,
etc.)
Servlet Life Cycle
1) The servlet is initialized by calling the init () method.
2) The servlet calls service() method to process a client's
request.
3) The servlet is terminated by calling the destroy() method.
4) Finally, servlet is garbage collected by the garbage collector
of the JVM.
Life cycle methods of a Servlet -The init() method
● Called once when the servlet is first created
● Used for one-time initialization (e.g., loading resources or config)
● Runs before handling any user request
● Servlet instance is shared; each request gets a new thread
Example:
public void init() throws ServletException {
// Initialization code
}
Life cycle methods of a Servlet -The service() Method
● Called by the Web Container to handle client requests
● Checks request type (GET, POST, etc.) and calls corresponding method (doGet(),
doPost(), etc.)
● A new thread is created for each request
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// Called automatically by container
}
● Typically, developers override doGet() or doPost() instead of service() directly.
doGet() Method
● Handles GET requests (via URL or forms without method)
● Used for fetching or reading data
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Handle GET request
}
doPost() Method
● Handles POST requests (via forms with method="POST")
● Used for sending or submitting data (e.g., form data)
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Handle POST request
}
destroy() Method
● Called once when the servlet is being taken out of service
● Used for cleanup tasks like closing DB connections or stopping threads
● After this, the servlet is eligible for garbage collection
public void destroy() {
// Cleanup code
}
destroy() Method
Inside destroy(), you write code to clean up:
● Closing database connections
● Stopping background threads
● Releasing file or network resources
● Logging shutdown events