SlideShare a Scribd company logo
1 of 34
Java Servlets
-by Ayush Thakur
&
Shivanshu Bansal
• Servlets are programs that run on a Web server and build
Web pages. Building Web pages on the fly is useful (and
commonly done) for a number of reasons:
• The Web page is based on data submitted by the
user. For example the results pages from search engines
are generated this way, and programs that process
orders for e-commerce sites do this as well.
• The data changes frequently. For example, a weather-
report or news headlines page might build the page
dynamically, perhaps returning a previously built page if it
is still up to date.
• The Web page uses information from corporate
databases or other such sources. For example, you
would use this for making a Web page at an on-line store
that lists current prices and number of items in stock.
• Java Servlets technology provides an HTTP-
based request and response paradigm on web
servers.
• Java Servlets can handle generic service
requests and respond to the client’s requests.
• Java Servlets are used in embedded systems,
wireless communication, and any other generic
request/response application.
• The Common Gateway Interface (CGI) was a
popular technology used to generate dynamic
HTTP web contents in the mid-1990s.
Efficient :With traditional CGI, a new process is started
for each HTTP request. If the CGI program does a
relatively fast operation, the overhead of starting the
process can dominate the execution time. With servlets,
the Java Virtual Machine stays up, and each request is
handled by a lightweight Java thread, not a heavyweight
operating system process. Similarly, in traditional CGI, if
there are N simultaneous request to the same CGI
program, then the code for the CGI program is loaded
into memory N times. With servlets, however, there
are N threads but only a single copy of the servlet class.
• Convenient. Hey, you already know Java. Why learn
Perl too? Besides the convenience of being able to use a
familiar language, servlets have an extensive
infrastructure for automatically parsing and decoding
HTML form data, reading and setting HTTP headers,
handling cookies, tracking sessions, and many other
such utilities.
• Powerful. Java servlets let you easily do several things
that are difficult or impossible with regular CGI. For one
thing, servlets can talk directly to the Web server (regular
CGI programs can't). This simplifies operations that need
to look up images and other data stored in standard
places. Servlets can also share data among each other,
making useful things like database connection pools easy
to implement.
• Portable. Servlets are written in Java and follow a well-
standardized API. Consequently, servlets written for, say I-
Planet Enterprise Server can run virtually unchanged on
Apache, Microsoft IIS, or WebStar. Servlets are supported
directly or via a plugin on almost every major Web server.
• Inexpensive. There are number of inexpensive Web
servers available that are good for "personal" use or low-
volume Web sites.
• A Servlet component can delegate the requests to its back-end
tier such as a database management system, RMI, EAI, or other
Enterprise Information System (EIS).
• A Servlet is deployed as a middle tier just like other web
components such as JSP components.
• The Servlet components are building block components, which
always work together with other components such as JSP
components, JavaBean components, Enterprise Java Bean (EJB)
components, and web service components.
• A Servlet component is also a distributed component,
which can provide services to remote clients and also
access remote resources.
• A Java Servlet application is supported by its Servlet
container.
• The Apache Tomcat web server is the official reference
implementation of Servlet containers, supporting Servlets
and JSP.
• The Java Servlet is a server-side web
component that takes a HTTP request from a
client, handles it, talks to a database, talks to a
JavaBean component, and responds with a
HTTP response or dispatches the request to
other Servlets or JSP components.
• Servlets can dynamically produce text-based
HTML markup contents and binary contents as
well contents based on the client’s request.
• A Java Servlet is a typical Java class that extends the
abstract class HttpServlet.
• The HttpServlet class extends another abstract class
called GenericServlet.
• The GenericServlet class implements three interfaces:
javax.servlet.Servlet, javax.servlet.ServletConfig, and
java.io.Serializable.
• A Servlet has a lifecycle just like a Java applet. The
lifecycle is managed by the Servlet container.
• Three methods are central to the life cycle of a servlet.
These are init(), service() and destroy().
• They are implemented by every servlet and are invoked
at specific times by te server.
• Firstly, let us assume that a user enters a URL to a web
browser. The browser will then generate an HTTP request
for this URL. This request is sent to the appropriate server.
• Second, this HTTP request is received by the Web Server.
The server maps this request to a particular servlet. The
servlet is dynamically retrieved and loaded into the address
space of the server.
• Third, the server now invokes the init() method of the
servlet. This method is invoked only when the servlet is
first loaded into the memory. It is possible to pass initial
parameters to the servlet so it may configure itself.
• Fourth, the server invokes the service() method of the
servlet. This method is called to process the HTTP
request. The servlet to read the data that has been
provided in the HTTP request. It may also formulate an
HTTP response for the client. The servlet remains in the
server’s address space and is available to process any
other HTTP request received from the client. It is called
for each HTTP request.
• Finally, the server may decise to unload the servlet from
the memory. The server calls the destroy() method to
relinquish any resources such as file handles that are
allocated for the servlet. The memory allocated for the
servlet and its objects can then be garbage collected.
To become familiar with the key servlet components. We
will begin by building and testing a simple servlet.
The basic steps are as follows:
1. Create and compile the servlet source code.
2. Start Tomcat.
3. Start the web browser and request the servlet.
import java.io.*;
import javax.servlet.*;
public class HelloServlet extends GenericServlet {
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
response.setContentType(“text/html”);
PrintWriter pw= response.getWriter();
pw.println(“<B>Hello!”);
pw.close();
}
}
• You can download Tomcat through the Sun
Microsystems Web site at java.sun.com. The
current version is 7.0. . The default location for
Tomcat 7.0 is
C:Program FilesApache Tomcat 7.0
• To start Tomcat, select Start Tomcat in the Start |
Programs menu, or run startup.bat from the
C:Program FilesApache Tomcat 7.0bin
directory. When you are done testing servlets, you
can stop Tomcat by selecting Stop Tomcat in the
Start | Programs menu, or run shutdown.bat.
• Your first step is to download software that implements
the Java Servlet 2.1 or 2.2 and Java Server Pages 1.0 or
1.1 specifications. You can get a free version from Sun,
known as the JavaServer Web Development Kit
(JSWDK), at http://java.sun.com/products/servlet/.
• Next, you need to tell javac where to find the servlet and
JSP classes when you compile a servlet file. The JSWDK
installation instructions explain this, but it basically
amounts to putting servlet.jar and jsp.jar(which come with
the JSWDK) on your CLASSPATH. If you've never dealt
with the CLASSPATH before, it is the variable that
specifies where Java looks for classes
• If it is unspecified, Java looks in the current directory and
the standard system libraries. If you set it yourself, you
need to be sure to include ".", signifying the current
directory.
• Start a web browser and enter the URL shown here:
• http://localhost:8080/examples/servlets/HelloServlets
OR
• http://127.0.0.1:8080/examples/servlets/HelloServlets
• This can be done because 127.0.0.1 is defined as the IP
address of the local machine.
• You will observe the output of the servlet in the browser
display area. And it will contain the string Hello! In bold
type.
• Two packages contain the classes and interfaces that are
required to build servlets. These are javax.servlet and
javax.servlet.http.
• The javax.servlet package contains a number of classes
and interfaces that describe and define the contracts
between a servlet class and the runtime environment
provided for an instance of such a class by a conforming
servlet container.
• The javax.servlet.http package contains a number of
classes and interfaces that describe and define the
contracts between a servlet class running under the
HTTP protocol and the runtime environment provided for
an instance of such a class by a conforming servlet
container
Thank You. 

More Related Content

Similar to 192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt

Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00
Rajesh Moorjani
 
Web Application Deployment
Web Application DeploymentWeb Application Deployment
Web Application Deployment
elliando dias
 

Similar to 192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt (20)

BITM3730Week12.pptx
BITM3730Week12.pptxBITM3730Week12.pptx
BITM3730Week12.pptx
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Liit tyit sem 5 enterprise java unit 1 notes 2018
Liit tyit sem 5 enterprise java  unit 1 notes 2018 Liit tyit sem 5 enterprise java  unit 1 notes 2018
Liit tyit sem 5 enterprise java unit 1 notes 2018
 
Weblogic
WeblogicWeblogic
Weblogic
 
Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00
 
Coursejspservlets00
Coursejspservlets00Coursejspservlets00
Coursejspservlets00
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Servlets
ServletsServlets
Servlets
 
Java part 3
Java part  3Java part  3
Java part 3
 
Advance java1.1
Advance java1.1Advance java1.1
Advance java1.1
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
JEE Course - The Web Tier
JEE Course - The Web TierJEE Course - The Web Tier
JEE Course - The Web Tier
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B KuteJava Servlet Programming under Ubuntu Linux by Tushar B Kute
Java Servlet Programming under Ubuntu Linux by Tushar B Kute
 
Web Application Deployment
Web Application DeploymentWeb Application Deployment
Web Application Deployment
 
Project First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be usedProject First presentation about introduction to technologies to be used
Project First presentation about introduction to technologies to be used
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهله
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

192563547-Servletsjhb,mnjhjhjm,nm,-Pres-ppt.ppt

  • 1. Java Servlets -by Ayush Thakur & Shivanshu Bansal
  • 2. • Servlets are programs that run on a Web server and build Web pages. Building Web pages on the fly is useful (and commonly done) for a number of reasons: • The Web page is based on data submitted by the user. For example the results pages from search engines are generated this way, and programs that process orders for e-commerce sites do this as well.
  • 3. • The data changes frequently. For example, a weather- report or news headlines page might build the page dynamically, perhaps returning a previously built page if it is still up to date. • The Web page uses information from corporate databases or other such sources. For example, you would use this for making a Web page at an on-line store that lists current prices and number of items in stock.
  • 4. • Java Servlets technology provides an HTTP- based request and response paradigm on web servers. • Java Servlets can handle generic service requests and respond to the client’s requests. • Java Servlets are used in embedded systems, wireless communication, and any other generic request/response application. • The Common Gateway Interface (CGI) was a popular technology used to generate dynamic HTTP web contents in the mid-1990s.
  • 5.
  • 6. Efficient :With traditional CGI, a new process is started for each HTTP request. If the CGI program does a relatively fast operation, the overhead of starting the process can dominate the execution time. With servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread, not a heavyweight operating system process. Similarly, in traditional CGI, if there are N simultaneous request to the same CGI program, then the code for the CGI program is loaded into memory N times. With servlets, however, there are N threads but only a single copy of the servlet class.
  • 7. • Convenient. Hey, you already know Java. Why learn Perl too? Besides the convenience of being able to use a familiar language, servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such utilities.
  • 8. • Powerful. Java servlets let you easily do several things that are difficult or impossible with regular CGI. For one thing, servlets can talk directly to the Web server (regular CGI programs can't). This simplifies operations that need to look up images and other data stored in standard places. Servlets can also share data among each other, making useful things like database connection pools easy to implement.
  • 9. • Portable. Servlets are written in Java and follow a well- standardized API. Consequently, servlets written for, say I- Planet Enterprise Server can run virtually unchanged on Apache, Microsoft IIS, or WebStar. Servlets are supported directly or via a plugin on almost every major Web server. • Inexpensive. There are number of inexpensive Web servers available that are good for "personal" use or low- volume Web sites.
  • 10.
  • 11.
  • 12. • A Servlet component can delegate the requests to its back-end tier such as a database management system, RMI, EAI, or other Enterprise Information System (EIS). • A Servlet is deployed as a middle tier just like other web components such as JSP components. • The Servlet components are building block components, which always work together with other components such as JSP components, JavaBean components, Enterprise Java Bean (EJB) components, and web service components. • A Servlet component is also a distributed component, which can provide services to remote clients and also access remote resources.
  • 13. • A Java Servlet application is supported by its Servlet container. • The Apache Tomcat web server is the official reference implementation of Servlet containers, supporting Servlets and JSP.
  • 14. • The Java Servlet is a server-side web component that takes a HTTP request from a client, handles it, talks to a database, talks to a JavaBean component, and responds with a HTTP response or dispatches the request to other Servlets or JSP components. • Servlets can dynamically produce text-based HTML markup contents and binary contents as well contents based on the client’s request.
  • 15. • A Java Servlet is a typical Java class that extends the abstract class HttpServlet. • The HttpServlet class extends another abstract class called GenericServlet. • The GenericServlet class implements three interfaces: javax.servlet.Servlet, javax.servlet.ServletConfig, and java.io.Serializable.
  • 16.
  • 17. • A Servlet has a lifecycle just like a Java applet. The lifecycle is managed by the Servlet container. • Three methods are central to the life cycle of a servlet. These are init(), service() and destroy(). • They are implemented by every servlet and are invoked at specific times by te server.
  • 18. • Firstly, let us assume that a user enters a URL to a web browser. The browser will then generate an HTTP request for this URL. This request is sent to the appropriate server. • Second, this HTTP request is received by the Web Server. The server maps this request to a particular servlet. The servlet is dynamically retrieved and loaded into the address space of the server.
  • 19. • Third, the server now invokes the init() method of the servlet. This method is invoked only when the servlet is first loaded into the memory. It is possible to pass initial parameters to the servlet so it may configure itself.
  • 20. • Fourth, the server invokes the service() method of the servlet. This method is called to process the HTTP request. The servlet to read the data that has been provided in the HTTP request. It may also formulate an HTTP response for the client. The servlet remains in the server’s address space and is available to process any other HTTP request received from the client. It is called for each HTTP request.
  • 21. • Finally, the server may decise to unload the servlet from the memory. The server calls the destroy() method to relinquish any resources such as file handles that are allocated for the servlet. The memory allocated for the servlet and its objects can then be garbage collected.
  • 22.
  • 23. To become familiar with the key servlet components. We will begin by building and testing a simple servlet. The basic steps are as follows: 1. Create and compile the servlet source code. 2. Start Tomcat. 3. Start the web browser and request the servlet.
  • 24. import java.io.*; import javax.servlet.*; public class HelloServlet extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException{ response.setContentType(“text/html”); PrintWriter pw= response.getWriter();
  • 26. • You can download Tomcat through the Sun Microsystems Web site at java.sun.com. The current version is 7.0. . The default location for Tomcat 7.0 is C:Program FilesApache Tomcat 7.0 • To start Tomcat, select Start Tomcat in the Start | Programs menu, or run startup.bat from the C:Program FilesApache Tomcat 7.0bin directory. When you are done testing servlets, you can stop Tomcat by selecting Stop Tomcat in the Start | Programs menu, or run shutdown.bat.
  • 27. • Your first step is to download software that implements the Java Servlet 2.1 or 2.2 and Java Server Pages 1.0 or 1.1 specifications. You can get a free version from Sun, known as the JavaServer Web Development Kit (JSWDK), at http://java.sun.com/products/servlet/. • Next, you need to tell javac where to find the servlet and JSP classes when you compile a servlet file. The JSWDK installation instructions explain this, but it basically amounts to putting servlet.jar and jsp.jar(which come with the JSWDK) on your CLASSPATH. If you've never dealt with the CLASSPATH before, it is the variable that specifies where Java looks for classes
  • 28. • If it is unspecified, Java looks in the current directory and the standard system libraries. If you set it yourself, you need to be sure to include ".", signifying the current directory.
  • 29. • Start a web browser and enter the URL shown here: • http://localhost:8080/examples/servlets/HelloServlets OR • http://127.0.0.1:8080/examples/servlets/HelloServlets
  • 30. • This can be done because 127.0.0.1 is defined as the IP address of the local machine. • You will observe the output of the servlet in the browser display area. And it will contain the string Hello! In bold type.
  • 31. • Two packages contain the classes and interfaces that are required to build servlets. These are javax.servlet and javax.servlet.http.
  • 32. • The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.
  • 33. • The javax.servlet.http package contains a number of classes and interfaces that describe and define the contracts between a servlet class running under the HTTP protocol and the runtime environment provided for an instance of such a class by a conforming servlet container