0% found this document useful (0 votes)
24 views14 pages

Servelets

Uploaded by

Pallavi Uday
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)
24 views14 pages

Servelets

Uploaded by

Pallavi Uday
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/ 14

Servelets

Java Programming Lab


Introduction to servlets
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
webserver, process the request, produce the response,
then send a response back to the webserver.

Properties of Servlets are as follows:


Servlets work on the server-side.
Servlets are capable of handling complex requests
obtained from the webserver.
What is Servlets?
• 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 is can be depicted from the image
itself as provided below as follows:
Execution of Servlets basically involves six basic steps:

• The clients send the request to the webserver.


• The web server receives the request.
• The web server passes the request to the corresponding
servlet.
• The servlet processes the request and generates the
response in the form of output.
• The servlet sends the response back to the webserver.
• The web server sends the response back to the client and
the client browser displays it on the screen.
What is a web application?

A web application is an application accessible from


the web. A web application is composed of web
components like Servlet, JSP, Filter, etc. and other
elements such as HTML, CSS, and JavaScript. The
web components typically execute in Web Server
and respond to the HTTP request.
Life Cycle of a Servlet (Servlet Life Cycle)

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.
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 used to initialize the
servlet. It is the life cycle method of the javax.servlet.Servlet
interface. Syntax of the init method is given below:
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 respo
nse)
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()
JSP-Java Server Page
JSP technology is used to create web application just like Servlet
technology. It can be thought of as an extension to Servlet because it
provides more functionality than servlet such as expression
language, JSTL, etc.

A JSP page consists of HTML tags and JSP tags. The JSP pages are
easier to maintain than Servlet because we can separate designing
and development. It provides some additional features such as
Expression Language, Custom Tags, etc.
Advantages of JSP over Servlet

1)Extension to Servlet
2)Easy to maintain
3)Fast Development: No
need to recompile and
redeploy
4)Less code than Servle
The Lifecycle of a JSP Page

The JSP pages follow these phases:

•Translation of JSP Page


•Compilation of JSP Page
•Classloading (the classloader loads class file)
•Instantiation (Object of the Generated Servlet is
created).
•Initialization ( the container invokes jspInit() method).
•Request processing ( the container invokes _jspService()
method).
•Destroy ( the container invokes jspDestroy() method).

You might also like