0% found this document useful (0 votes)
3 views29 pages

Serve LT

Java Servlet technology is used to create dynamic web applications on the server side, utilizing various interfaces and classes from the Servlet API. The servlet lifecycle includes methods for initialization, request handling, and cleanup, and the process of creating a servlet involves setting up a directory structure, compiling the servlet, and creating a deployment descriptor. Apache Tomcat is commonly used as the server for deploying servlets, requiring configuration of environment variables like JAVA_HOME.

Uploaded by

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

Serve LT

Java Servlet technology is used to create dynamic web applications on the server side, utilizing various interfaces and classes from the Servlet API. The servlet lifecycle includes methods for initialization, request handling, and cleanup, and the process of creating a servlet involves setting up a directory structure, compiling the servlet, and creating a deployment descriptor. Apache Tomcat is commonly used as the server for deploying servlets, requiring configuration of environment variables like JAVA_HOME.

Uploaded by

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

Java Servlet

Java Servlet
• Java Servlet technology is used to create a web application (resides at
server side and generates a dynamic web page).
• The Java Servlet technology is robust and scalable because of java
language.
• There are many interfaces and classes in the Servlet API such as
Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse,
etc.
Java Servlet
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.
Advantages of Servlet
• The web container creates threads for
handling the multiple requests to the
Servlet.
• Threads have many benefits such as
cost of communication between the
threads are low.
Web Terminology
Servlet Interface
• Servlet interface defines methods that all servlets must implement.
• Servlet interface needs to be implemented for creating any servlet.
• It provides 3 life cycle methods that are used to initialize the servlet,
to service the requests, and to destroy the servlet and 2 non-life cycle
methods.
GenericServlet class
• GenericServlet class implements Servlet.
• It provides the implementation of all the methods of the interface
except the service method.
HttpServlet class
• The HttpServlet class extends the GenericServlet class
• It provides http specific methods such as doGet, doPost, doHead,
doTrace etc.
Life Cycle of a Servlet (Servlet Life Cycle)
• Servlet class is loaded.
• Servlet instance is created.
• init method is invoked.
• service method is invoked.
• destroy method is invoked
Ex 1
• Which method is invoked by the servlet container to allow a servlet
to release resources before it is removed from service?
• destroy()
• doDelete()
• doCleanup()
• finalize()
• During the initialization phase of a servlet, which method is called
by the servlet container to read the servlet configuration
information?
• init()
• service()
• config()
• initialize()
• In which phase of the servlet lifecycle is the service() method
repeatedly called?
• Initialization phase
• Instantiation phase
• Request handling phase
• Destruction phase
• Which method is called by the servlet container when it decides to
remove a servlet instance from the service, allowing the servlet to
perform final cleanup?
• service()
• doGet()
• init()
• destroy()
Steps to create a servlet example
• we are going to use apache tomcat server in this example.
• The steps are as follows:
• Create a directory structure
• Create a Servlet
• Compile the Servlet
• Create a deployment descriptor
• Start the server and deploy the project
• Access the servlet
1-Create a directory structures
• The directory structure defines that
where to put the different types of files so
that web container may get the
information and respond to the client.
• As you can see that the servlet class file
must be in the classes folder.
• The web.xml file must be under the WEB-
INF folder.
2-Create a Servlet
• The servlet example can be created by three ways:

• By implementing Servlet interface,


• By inheriting GenericServlet class, (or)
• By inheriting HttpServlet class

• The mostly used approach is by extending HttpServlet


In this example, we are inheriting the HttpServlet
class and providing the implementation of the
doGet() method.
3)Compile the servlet
• For compiling the Servlet, jar file is required to be loaded.
• Two ways to load the jar file
• set classpath
• paste the jar file in JRE/lib/ext folder
• Put the java file in any folder. After compiling the java file, paste the
class file of servlet in WEB-INF/classes directory.
4)Create the deployment descriptor (web.xml
file)
• The deployment descriptor is an xml
file, from which Web Container gets the
information about the servet to be
invoked.
Description of the elements of web.xml file
<web-app> represents the whole application.

<servlet> is sub element of <web-app> and represents the servlet.

<servlet-name> is sub element of <servlet> represents the name of the servlet.

<servlet-class> is sub element of <servlet> represents the class of the servlet.

<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.

<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to


invoke the servlet.
5)Start the Server and deploy the project
• double click on the startup.bat file under apache-tomcat/bin
directory.
One Time Configuration for Apache Tomcat
Server
• You need to perform 2 tasks:
• set JAVA_HOME or JRE_HOME in environment variable (It is required
to start server).
• Change the port number of tomcat (optional). It is required if another
server is running on same port (8080).
1) How to set JAVA_HOME in environment
variable?
• To start Apache Tomcat server JAVA_HOME and JRE_HOME must be
set in Environment variables.

• After setting the JAVA_HOME double click on the startup.bat file
in apache tomcat/bin.
5) How to deploy the servlet project
How to access the servlet
Open broser and write
http://hostname:portno/contextroot/urlpatternof
servlet. For example:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy