SlideShare a Scribd company logo
Department of Computer Science
Govt Post Graduate College Dargai Malakand
Presenting By: Aamir SohailPresenting By: Aamir Sohail
Subject: Advance OOP(Java)Subject: Advance OOP(Java)
Java ServletsJava Servlets
Page  2
What are the java servlets?
OR
Servlets are java-based server side programs
Java Servlets are programs that run on a Web server
and act as a middle layer between a requests coming
from a Web browser or other HTTP client and
databases or applications on the HTTP server.
??
Page  3
Server…Server…
A server is a computer that responds to
requests from a client.
Typical requests: provide a web page, upload or download a file,
send email.
A client could be the browser or other software
making these requests
Typically, My little computer is the client, and
someone else’s big computer is the server
Page  4
Servlets…
Servlets are java-based server side programs
Servlets are Java technology’s answer to Common
Gateway Interface (CGI) programming.
They are programs that run on a Web server, acting
as a middle layer between a request coming from a
Web browser or other HTTP client and databases or
applications on the HTTP server.
Page  5
Servlets…Servlets…
Using Servlets, you can collect input from users
through web page forms, present records from a
database or another source, and create web pages
dynamically.
Java Servlets often serve the same purpose as
programs implemented using the Common Gateway
Interface (CGI).
If we have this
Why we need
Servlets????
Page  6
let me explain
Page  7
CGI…
CGI stands for “Common Gateway Interface”
Client sends a request to server
Server starts a CGI script
Script computes a result for server
Another client sends a request
CGI Creates a new procces
Server returns response to client
Page  8
Servlets…Servlets…
Client sends a request to
server
Server starts a servlet
Servlet computes a result
for server and does not quit
Another client sends a
request
Server calls the servlet
again
Etc.
Server returns response to
client
Page  9
Servlets vs. CGI scripts
Advantages:
Running a servlet doesn’t require creating a separate
process each time
A servlet stays in memory, so it doesn’t have to be
reloaded each time
There is only one instance handling multiple requests,
not a separate instance for every request
Page  10
The Advantages of Servlets Over “Traditional” CGI
Java servlets are more efficient,
easier to use,
more powerful,
more portable,
safer,
and cheaper than traditional CGI and many alternative CGI-like
technologies
Page  11
With traditional CGI, a new process is started for each
HTTP request. If the CGI program itself is relatively
short, the overhead of starting the process can dominate
the execution time.
With servlets, the Java virtual machine stays running and
handles each request with a lightweight Java thread, not a
heavyweight operating system process
Page  12
Similarly, in traditional CGI, if there are N requests to
the same CGI program, the code for the CGI program
is loaded into memory N times.
With servlets, however, there would be N threads, but
only a single copy of the servlet class would be loaded.
This approach reduces server memory requirements and
saves time by instantiating fewer objects
Page  13
The Servlet Life Cycle
In Slide 10 and 11 (The Advantages of Servlets Over
“Traditional” CGI), I referred to the fact that only a
single instance of a servlet gets created, with each
user request resulting in a new thread that is handed
off to doGet or doPost as appropriate.
I’ll now explain how servlets are created and
destroyed, and how and when the various methods are
invoked. I give a quick summary here, then elaborate in
the following subsections.
Page  14
After this, each user request results in a thread that
calls the service method of the previously created
instance.
Multiple concurrent requests normally result in multiple
threads calling service
The service method then calls doGet, doPost, or
another method, depending on the type of HTTP
request it received.
When the servlet is first created, its init method is
invoked, so init is where you put one-time setup code.
Page  15
Finally, when the server decides to unload a
servlet, it first calls the servlet’s destroy
method.
Page  16
The init Method…
The init method is called when the servlet is first created;
it is not called again for each user request. So, it is used
for one-time initializations.
The servlet is normally created when a user first invokes a
URL corresponding to the servlet, but you can also specify
that the servlet be loaded when the server is first started
Page  17
The service Method
Each time the server receives a request for a servlet, the
server spawns a new thread and calls service.
The service method checks the HTTP request type (GET,
POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut,
doDelete, etc
Page  18
Destroy Method
Called when server deletes servletinstance.
Not called after each request
Page  19
how to create servletshow to create servlets (Basic Structure)(Basic Structure)
Page  20
In the above program a basic servlet that handles
GET requests. GET requests, for those unfamiliar
with HTTP, are the usual type of browser requests
for Web pages.
A browser generates this request when the user
enters a URL on the address line, follows a link from
a Web page, or submits an HTML form that either
does not specify a METHOD or specifies
METHOD="GET".
Page  21
Servlets can also easily handle POST requests, which
are generated when someone submits an HTML form
that specifies METHOD="POST".
Page  22
Advantages of Servlets over other server side
technologies (ASP, CGI etc)
A servlet can be imagined to be as an applet
running on the server side. Some of the other
server side technologies available are Common
Gateway Interface (CGI), server side
JavaScript and Active Server Pages (ASP).
Advantages of servlets over these server
side technologies are as follows:
Page  23
Persistent
Servlets remain in memory until explicitly destroyed. This helps in
serving several incoming requests.
Servlets establishes connection only once with the database and can
handle several requests on the same database.
This reduces the time and resources required to establish connection
again
and again with the same database.
Whereas, CGI programs are removed from the memory once the request is
processed and each time a new process is initiated whenever new request
arrives.
Page  24
Portable
Since servlets are written in Java, they are portable.
That is, servlets are compatible with almost all operating systems.
The programs written on one operating system can be executed on
other operating system
Page  25
Server-independent
Servlets are compatible with any web server available today.
Most of the software vendors today support servlets within their
web server products.
On the other hand, some of the server side technologies like
server side JavaSricpt and ASP can run on only selected web
servers.
The CGI is compatible with the web server that has features to
supports it.
Page  26
Fast
Since servlets are compiled into bytecodes, they can
execute more quickly as compared to other scripting
languages. The bytecode compilation feature helps
servlets to give much better performance. In
addition, it also provides advantage of strong error
and type checking.
Page  27
How to run servlet?
There are Six Steps to Running Your First Servlet
 Create a directory structure under Tomcat for
your application.
 Write the servlet source code. You need to import
the javax.servlet package and the
javax.servlet.http package in your source file.
 Compile your source code.
 Create a deployment descriptor.
 Run Tomcat.
 Call your servlet from a web browser.
Page  28
Create a Directory Structure under Tomcat
 When you install Tomcat, several subdirectories are
automatically created under the Tomcat home directory
(%TOMCAT_HOME%). One of the subdirectories is webapps.
 The webapps directory is where you store your web applications.
A web application is a collection of servlets and other contents
installed under a specific subset of the server's URL
namespace.
 A separate directory is dedicated for each servlet application.
Therefore, the first thing to do when you build a servlet
application is create an application directory
Page  29
Compile Your Source Code
For your servlet source code to compile, you need to include in
your CLASSPATH environment variable the path to the
servlet.jar file. The servlet.
jar is located in the commonlib subdirectory under
%CATALINA_HOME%.
30
Thank You !!!
References:
Wikipedi.org
Videos on youtube
Askme.com
And some PDF files about Java Servlets.

More Related Content

PPTX
Session And Cookies In Servlets - Java
PPTX
J servlets
PPTX
J2ee servlet
PPT
Java - Servlet - Mazenet Solution
PPT
PDF
Servlet classnotes
PPTX
Java servlets and CGI
PPTX
Creating a WebSocket-Chat-Application with Jetty Embedded - Techcamp 2014
Session And Cookies In Servlets - Java
J servlets
J2ee servlet
Java - Servlet - Mazenet Solution
Servlet classnotes
Java servlets and CGI
Creating a WebSocket-Chat-Application with Jetty Embedded - Techcamp 2014

What's hot (20)

PPTX
Servlet in java , java servlet , servlet servlet and CGI, API
PDF
Realtime web application with java
PDF
Server-Side Programming Primer
PPTX
Servlet session 1
PPTX
Server Side Programming
PDF
HTML5 Server Sent Events/JSF JAX 2011 Conference
PPTX
Server side programming
PPT
Reverse proxy
PPTX
Web services - A Practical Approach
PPT
Web Servers (ppt)
PPTX
Java web application development
PPTX
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
PPTX
Servletarchitecture,lifecycle,get,post
PDF
Getting Started with WebSockets and Server-Sent Events
PPTX
PPT
PPT
Web servers
ODP
Web Server-Side Programming Techniques
PDF
4 Basic PHP
PDF
Web server
Servlet in java , java servlet , servlet servlet and CGI, API
Realtime web application with java
Server-Side Programming Primer
Servlet session 1
Server Side Programming
HTML5 Server Sent Events/JSF JAX 2011 Conference
Server side programming
Reverse proxy
Web services - A Practical Approach
Web Servers (ppt)
Java web application development
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
Servletarchitecture,lifecycle,get,post
Getting Started with WebSockets and Server-Sent Events
Web servers
Web Server-Side Programming Techniques
4 Basic PHP
Web server
Ad

Similar to Presentation on java servlets (20)

PPT
Programming Server side with Sevlet
DOC
Unit5 servlets
PPTX
WEB TECHNOLOGY Unit-3.pptx
PPTX
PPTX
Java Servlet
PPTX
Advance Java Programming (CM5I) 6.Servlet
PPTX
PPTX
Servlet.pptx
PPTX
Servlet.pptx
PPTX
Chapter 3 servlet & jsp
PPTX
Servlets
PPTX
Wt unit 3
PPTX
UNIT-3 Servlet
PPTX
SERVLET in web technolgy engineering.pptx
PPT
Lecture 2
DOCX
Major project report
PPTX
java Servlet technology
PPTX
Servlets & jdbc
PPTX
Jsp and Servlets
Programming Server side with Sevlet
Unit5 servlets
WEB TECHNOLOGY Unit-3.pptx
Java Servlet
Advance Java Programming (CM5I) 6.Servlet
Servlet.pptx
Servlet.pptx
Chapter 3 servlet & jsp
Servlets
Wt unit 3
UNIT-3 Servlet
SERVLET in web technolgy engineering.pptx
Lecture 2
Major project report
java Servlet technology
Servlets & jdbc
Jsp and Servlets
Ad

More from Aamir Sohail (7)

PPT
Presentation on html, css
PPTX
Line clipping algorithm (Detailed)
PPTX
Hash table in data structure and algorithm
PPT
Vb script
PPTX
Infromation securiity
PPTX
Network Security Policies
PPTX
Scheduling and scheduling personnel
Presentation on html, css
Line clipping algorithm (Detailed)
Hash table in data structure and algorithm
Vb script
Infromation securiity
Network Security Policies
Scheduling and scheduling personnel

Recently uploaded (20)

PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
System and Network Administration Chapter 2
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Transform Your Business with a Software ERP System
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
System and Network Administraation Chapter 3
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Understanding Forklifts - TECH EHS Solution
PPT
Introduction Database Management System for Course Database
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Digital Systems & Binary Numbers (comprehensive )
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
System and Network Administration Chapter 2
PTS Company Brochure 2025 (1).pdf.......
Operating system designcfffgfgggggggvggggggggg
Designing Intelligence for the Shop Floor.pdf
Transform Your Business with a Software ERP System
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administraation Chapter 3
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo Companies in India – Driving Business Transformation.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Understanding Forklifts - TECH EHS Solution
Introduction Database Management System for Course Database
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
How to Migrate SBCGlobal Email to Yahoo Easily
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Which alternative to Crystal Reports is best for small or large businesses.pdf

Presentation on java servlets

  • 1. Department of Computer Science Govt Post Graduate College Dargai Malakand Presenting By: Aamir SohailPresenting By: Aamir Sohail Subject: Advance OOP(Java)Subject: Advance OOP(Java) Java ServletsJava Servlets
  • 2. Page  2 What are the java servlets? OR Servlets are java-based server side programs Java Servlets are programs that run on a Web server and act as a middle layer between a requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server. ??
  • 3. Page  3 Server…Server… A server is a computer that responds to requests from a client. Typical requests: provide a web page, upload or download a file, send email. A client could be the browser or other software making these requests Typically, My little computer is the client, and someone else’s big computer is the server
  • 4. Page  4 Servlets… Servlets are java-based server side programs Servlets are Java technology’s answer to Common Gateway Interface (CGI) programming. They are programs that run on a Web server, acting as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
  • 5. Page  5 Servlets…Servlets… Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically. Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). If we have this Why we need Servlets????
  • 6. Page  6 let me explain
  • 7. Page  7 CGI… CGI stands for “Common Gateway Interface” Client sends a request to server Server starts a CGI script Script computes a result for server Another client sends a request CGI Creates a new procces Server returns response to client
  • 8. Page  8 Servlets…Servlets… Client sends a request to server Server starts a servlet Servlet computes a result for server and does not quit Another client sends a request Server calls the servlet again Etc. Server returns response to client
  • 9. Page  9 Servlets vs. CGI scripts Advantages: Running a servlet doesn’t require creating a separate process each time A servlet stays in memory, so it doesn’t have to be reloaded each time There is only one instance handling multiple requests, not a separate instance for every request
  • 10. Page  10 The Advantages of Servlets Over “Traditional” CGI Java servlets are more efficient, easier to use, more powerful, more portable, safer, and cheaper than traditional CGI and many alternative CGI-like technologies
  • 11. Page  11 With traditional CGI, a new process is started for each HTTP request. If the CGI program itself is relatively short, the overhead of starting the process can dominate the execution time. With servlets, the Java virtual machine stays running and handles each request with a lightweight Java thread, not a heavyweight operating system process
  • 12. Page  12 Similarly, in traditional CGI, if there are N requests to the same CGI program, the code for the CGI program is loaded into memory N times. With servlets, however, there would be N threads, but only a single copy of the servlet class would be loaded. This approach reduces server memory requirements and saves time by instantiating fewer objects
  • 13. Page  13 The Servlet Life Cycle In Slide 10 and 11 (The Advantages of Servlets Over “Traditional” CGI), I referred to the fact that only a single instance of a servlet gets created, with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate. I’ll now explain how servlets are created and destroyed, and how and when the various methods are invoked. I give a quick summary here, then elaborate in the following subsections.
  • 14. Page  14 After this, each user request results in a thread that calls the service method of the previously created instance. Multiple concurrent requests normally result in multiple threads calling service The service method then calls doGet, doPost, or another method, depending on the type of HTTP request it received. When the servlet is first created, its init method is invoked, so init is where you put one-time setup code.
  • 15. Page  15 Finally, when the server decides to unload a servlet, it first calls the servlet’s destroy method.
  • 16. Page  16 The init Method… The init method is called when the servlet is first created; it is not called again for each user request. So, it is used for one-time initializations. The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started
  • 17. Page  17 The service Method Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc
  • 18. Page  18 Destroy Method Called when server deletes servletinstance. Not called after each request
  • 19. Page  19 how to create servletshow to create servlets (Basic Structure)(Basic Structure)
  • 20. Page  20 In the above program a basic servlet that handles GET requests. GET requests, for those unfamiliar with HTTP, are the usual type of browser requests for Web pages. A browser generates this request when the user enters a URL on the address line, follows a link from a Web page, or submits an HTML form that either does not specify a METHOD or specifies METHOD="GET".
  • 21. Page  21 Servlets can also easily handle POST requests, which are generated when someone submits an HTML form that specifies METHOD="POST".
  • 22. Page  22 Advantages of Servlets over other server side technologies (ASP, CGI etc) A servlet can be imagined to be as an applet running on the server side. Some of the other server side technologies available are Common Gateway Interface (CGI), server side JavaScript and Active Server Pages (ASP). Advantages of servlets over these server side technologies are as follows:
  • 23. Page  23 Persistent Servlets remain in memory until explicitly destroyed. This helps in serving several incoming requests. Servlets establishes connection only once with the database and can handle several requests on the same database. This reduces the time and resources required to establish connection again and again with the same database. Whereas, CGI programs are removed from the memory once the request is processed and each time a new process is initiated whenever new request arrives.
  • 24. Page  24 Portable Since servlets are written in Java, they are portable. That is, servlets are compatible with almost all operating systems. The programs written on one operating system can be executed on other operating system
  • 25. Page  25 Server-independent Servlets are compatible with any web server available today. Most of the software vendors today support servlets within their web server products. On the other hand, some of the server side technologies like server side JavaSricpt and ASP can run on only selected web servers. The CGI is compatible with the web server that has features to supports it.
  • 26. Page  26 Fast Since servlets are compiled into bytecodes, they can execute more quickly as compared to other scripting languages. The bytecode compilation feature helps servlets to give much better performance. In addition, it also provides advantage of strong error and type checking.
  • 27. Page  27 How to run servlet? There are Six Steps to Running Your First Servlet  Create a directory structure under Tomcat for your application.  Write the servlet source code. You need to import the javax.servlet package and the javax.servlet.http package in your source file.  Compile your source code.  Create a deployment descriptor.  Run Tomcat.  Call your servlet from a web browser.
  • 28. Page  28 Create a Directory Structure under Tomcat  When you install Tomcat, several subdirectories are automatically created under the Tomcat home directory (%TOMCAT_HOME%). One of the subdirectories is webapps.  The webapps directory is where you store your web applications. A web application is a collection of servlets and other contents installed under a specific subset of the server's URL namespace.  A separate directory is dedicated for each servlet application. Therefore, the first thing to do when you build a servlet application is create an application directory
  • 29. Page  29 Compile Your Source Code For your servlet source code to compile, you need to include in your CLASSPATH environment variable the path to the servlet.jar file. The servlet. jar is located in the commonlib subdirectory under %CATALINA_HOME%.
  • 30. 30 Thank You !!! References: Wikipedi.org Videos on youtube Askme.com And some PDF files about Java Servlets.