SlideShare a Scribd company logo
1 of 30
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

What's hot

Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIPRIYADARSINISK
 
Realtime web application with java
Realtime web application with javaRealtime web application with java
Realtime web application with javaJeongHun Byeon
 
Server-Side Programming Primer
Server-Side Programming PrimerServer-Side Programming Primer
Server-Side Programming PrimerIvano Malavolta
 
Server Side Programming
Server Side ProgrammingServer Side Programming
Server Side ProgrammingMilan Thapa
 
HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 ConferenceRoger Kitain
 
Server side programming
Server side programming Server side programming
Server side programming javed ahmed
 
Web services - A Practical Approach
Web services - A Practical ApproachWeb services - A Practical Approach
Web services - A Practical ApproachMadhaiyan Muthu
 
Java web application development
Java web application developmentJava web application development
Java web application developmentRitikRathaur
 
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a CometDDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a CometRichard Banks
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsi krishna
 
Getting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent EventsGetting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent EventsArun Gupta
 
Web Server-Side Programming Techniques
Web Server-Side Programming TechniquesWeb Server-Side Programming Techniques
Web Server-Side Programming Techniquesguest8899ec02
 

What's hot (20)

Servlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, APIServlet in java , java servlet , servlet servlet and CGI, API
Servlet in java , java servlet , servlet servlet and CGI, API
 
Realtime web application with java
Realtime web application with javaRealtime web application with java
Realtime web application with java
 
Server-Side Programming Primer
Server-Side Programming PrimerServer-Side Programming Primer
Server-Side Programming Primer
 
Servlet session 1
Servlet   session 1Servlet   session 1
Servlet session 1
 
Server Side Programming
Server Side ProgrammingServer Side Programming
Server Side Programming
 
HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 Conference
 
Server side programming
Server side programming Server side programming
Server side programming
 
Reverse proxy
Reverse proxyReverse proxy
Reverse proxy
 
Web services - A Practical Approach
Web services - A Practical ApproachWeb services - A Practical Approach
Web services - A Practical Approach
 
Web Servers (ppt)
Web Servers (ppt)Web Servers (ppt)
Web Servers (ppt)
 
Java web application development
Java web application developmentJava web application development
Java web application development
 
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a CometDDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
DDD Sydney 2011 - Getting out of Sync with IIS and Riding a Comet
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Getting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent EventsGetting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent Events
 
Overview of java web services
Overview of java web servicesOverview of java web services
Overview of java web services
 
zigbee
zigbeezigbee
zigbee
 
Web servers
Web serversWeb servers
Web servers
 
Web Server-Side Programming Techniques
Web Server-Side Programming TechniquesWeb Server-Side Programming Techniques
Web Server-Side Programming Techniques
 
4 Basic PHP
4 Basic PHP4 Basic PHP
4 Basic PHP
 
Web server
Web serverWeb server
Web server
 

Similar to Presentation on java servlets (20)

Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
 
Unit5 servlets
Unit5 servletsUnit5 servlets
Unit5 servlets
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
 
JAVA
JAVAJAVA
JAVA
 
Java Servlet
Java ServletJava Servlet
Java Servlet
 
Advance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.ServletAdvance Java Programming (CM5I) 6.Servlet
Advance Java Programming (CM5I) 6.Servlet
 
Servlets
ServletsServlets
Servlets
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Servlet.pptx
Servlet.pptxServlet.pptx
Servlet.pptx
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Servlets
ServletsServlets
Servlets
 
Wt unit 3
Wt unit 3 Wt unit 3
Wt unit 3
 
UNIT-3 Servlet
UNIT-3 ServletUNIT-3 Servlet
UNIT-3 Servlet
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
Major project report
Major project reportMajor project report
Major project report
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Servlets & jdbc
Servlets & jdbcServlets & jdbc
Servlets & jdbc
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
Java servlets
Java servletsJava servlets
Java servlets
 

More from Aamir Sohail

Presentation on html, css
Presentation on html, cssPresentation on html, css
Presentation on html, cssAamir Sohail
 
Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)Aamir Sohail
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithmAamir Sohail
 
Infromation securiity
Infromation securiityInfromation securiity
Infromation securiityAamir Sohail
 
Network Security Policies
Network Security PoliciesNetwork Security Policies
Network Security PoliciesAamir Sohail
 
Scheduling and scheduling personnel
Scheduling and scheduling personnelScheduling and scheduling personnel
Scheduling and scheduling personnelAamir Sohail
 

More from Aamir Sohail (7)

Presentation on html, css
Presentation on html, cssPresentation on html, css
Presentation on html, css
 
Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)Line clipping algorithm (Detailed)
Line clipping algorithm (Detailed)
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Vb script
Vb scriptVb script
Vb script
 
Infromation securiity
Infromation securiityInfromation securiity
Infromation securiity
 
Network Security Policies
Network Security PoliciesNetwork Security Policies
Network Security Policies
 
Scheduling and scheduling personnel
Scheduling and scheduling personnelScheduling and scheduling personnel
Scheduling and scheduling personnel
 

Recently uploaded

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

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.