Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Servlets and Java Server pages

Rating
-
Sold
-
Pages
33
Uploaded on
14-03-2024
Written in
2023/2024

Servlets and Java Server

Institution
Course

Content preview

Unit 7: Servlets and Java Server pages (8 Hrs.)
Introduction to Servlets:
Servlets are small programs that execute on the server side of a Web connection and dynamically
extend the functionality of a Web server just as applets.

 Servlet is a technology which is used to create a web application.
 Servlet is an API that provides many interfaces and classes including documentation.
 Servlet is an interface that must be implemented for creating any Servlet.
 Servlet is a class that extends the capabilities of the servers and responds to the incoming
requests. It can respond to any requests.
 Servlet is a web component that is deployed on the server to create a dynamic web page.


Servlet Architecture
The architecture, is the communication interface, protocol used, requirements of client and
server, the programming with the languages and software involved. Basically, it performs the
below-mentioned tasks.

1. The clients send the request to the webserver.
2. The web server receives the request.
3. The web server passes the request to the corresponding servlet.
4. The servlet processes the request and generates the response in the form of output.
5. The servlet sends the response back to the webserver.
6. The web server sends the response back to the client and the client browser displays it on
the screen.




Notes by Santosh Bhatt Page 1

,Servlets more closely resemble Common Gateway Interface (CGI) scripts or programs than
applets in terms of functionality. As in CGI programs, servlets can respond to user events from
an HTML request, and then dynamically construct an HTML response that is sent back to the
client.



Servlet Lifecycle:

The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the
servlet:

1. Servlet class is loaded.

2. Servlet instance is created.

3. init method is invoked.

4. service method is invoked.

5. destroy method is invoked.




As displayed in the above diagram, there are three states of a servlet: new, ready and end. The
servlet is in new state if servlet instance is created. After invoking the init() method, Servlet
comes in the ready state. In the ready state, servlet performs all the tasks. When the web
container invokes the destroy() method, it shifts to the end state.


Notes by Santosh Bhatt Page 2

,1) Servlet class is loaded

The classloader is responsible to load the servlet class. The servlet class is loaded when the first
request for the servlet is received by the web container.

2) Servlet instance is created

The web container creates the instance of a servlet after loading the servlet class. The servlet
instance is created only once in the servlet life cycle.

3) init method is invoked
The web container calls the init method only once after creating the servlet instance. The init method is us
servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given belo

public void init(ServletConfig config) throws ServletException


4) service method is invoked

The web container calls the service method each time when request for the servlet is received. If
servlet is not initialized, it follows the first three steps as described above then calls the service
method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only
once. The syntax of the service method of the Servlet interface is given below:

public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException


5) destroy method is invoked

The web container calls the destroy method before removing the servlet instance from the
service. It gives the servlet an opportunity to clean up any resource for example memory, thread
etc. The syntax of the destroy method of the Servlet interface is given below:

public void destroy()


The servlet APIs:
Servlets are Java-based server-side components that extend the capabilities of a server. They
provide a way to generate dynamic content, handle HTTP requests, and interact with web clients.
Servlets are part of the Java Platform, Enterprise Edition (Java EE) and are commonly used for
developing web applications.

Notes by Santosh Bhatt Page 3

, The Servlet API in Java is a set of classes and interfaces that define the contract between a
servlet class and the runtime environment provided by the web container. Here's an overview of
some key classes and interfaces in the Servlet API:

1. javax.servlet.Servlet interface:
 All servlets must implement this interface.
 It defines the service() method, which handles requests from clients.
2. javax.servlet.http.HttpServlet class:
 This is an abstract class that extends Servlet.
 It provides HTTP-specific functionality and simplifies the development of web
applications.
 Developers usually extend this class to create their servlets.
3. javax.servlet.ServletRequest interface:
 Represents the request made by the client to the servlet.
 Provides methods to access information such as parameters, headers, and input
streams.
4. javax.servlet.ServletResponse interface:
 Represents the response sent by the servlet to the client.
 Provides methods to set response headers and write data to the response stream.
5. javax.servlet.http.HttpServletRequest interface:
 Extends ServletRequest and provides additional methods for HTTP-specific
functionality.
 Provides methods to get information about the request, such as the HTTP method,
headers, and parameters.
6. javax.servlet.http.HttpServletResponse interface:
 Extends ServletResponse and provides additional methods for HTTP-specific
functionality.
 Allows setting HTTP response status codes and headers.

Now, let's look at a simple example of a servlet that responds to a client request:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


Notes by Santosh Bhatt Page 4

Written for

Institution
Course

Document information

Uploaded on
March 14, 2024
Number of pages
33
Written in
2023/2024
Type
Class notes
Professor(s)
Santosh bhatt
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
santoshbhatt

Get to know the seller

Seller avatar
santoshbhatt SNSC
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions