SlideShare a Scribd company logo
1 of 19
Download to read offline
CST8288
OOP with Design Patterns
Fall 2022
Week 09 – Servlets – part1
© 2022 - Copyright Algonquin College and other authors as indicated.
Overview
o Servlets Part1 – this week
• N-Tier applications
o Servlets Part2 – Week 9
2
o "client - server"
• tiers communicate via network protocols
• e.g. using JDBC
• where client implements:
presentation,
business rules/logic
and data access logic
2 Tiered Applications
Multi-tiered Applications
4
Client PC
Browser
Database Server
RDBMS
SQL over TCP
HTML over HTTP
Application
Server
Servlet Container
JDBC
Presentation Tier Application Tier Data Tier
o in N-tier model
• presentation tier
• implemented on client device
• using content (e.g. HTML) provided by "application" server
• business tier
• creates presentation content
• implements business rules/logic on an "application" server
• data tier
• implements data access logic on the "application" server
• data stored on "database" server
• Notes:
• servers may be replicated for redundancy and load balancing
Multi-tiered - continued
Multi-tiered - continued
6
Application Server
Servlet Container
JDBC
HttpServletRequest
HttpServletResponse
void doGet( __ , __)
void doPost(__ , __)
HTML over HTTP
SQL over TCP
Servlet - lifecycle
o init() and destroy() from class HttpServlet
• init() method called when servlet first created
• not called again as long as the servlet is not destroyed.
• destroy() method is invoked once:
• all in service() method have exited
• or after timeout period
• releases container resources that were allocated.
o service()
• invoked each time server receives request for servlet.
• server spawns a new thread and invokes service().
7
Servlet lifecycle - continued
o more on service()
• based on request type, call appropriate method
• GET, POST requests go to: doGet(), doPost()
are the most common ones
• other request map the same way
• also provides default handlers
• so do not override service()
• override specific doXXX() method instead
8
Servlet lifecycle - continued
9
from Liang, Introduction to Java Programming, 12th ed
classes: HttpServletRequest &
HttpServletResponse
protected void doXXX(HttpServletRequest req,
HttpServletResponse resp)
• servlet obtains request from HTTPServletRequest
and creates response using HTTPServletResponse
• these 2 classes encapsulated request & response
• doXXX() methods throws following:
ServletException, java.io.IOException
10
HTML - document overview
<!DOCTYPE html>
• first line of HTML doc.
• Info about specific version can be added
o then 3 pairs of tags:
• first 3 tag-pairs: html, head & body
<html>
<head>
non-visible elements here
images, scripts & other items can be pre-loaded
</head>
<body>
(visible elements here)
</body>
</html>
11
Servlet – overview of coding
o create servlet by:
public class ___ extends HttpServlet
o then override specific doXXX() methods
• if using same code for doGet() and doPost()
• then have one call the other or
• have both call a common method
e.g. in NetBeans skeleton code both doGet() and
doPost() call processRequest()
12
Servlet coding - continued
13
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
/* out.println() statements for head section */
out.println("</head>");
out.println("<body>");
/* more out.println() statements for body section */
out.println("</body>");
out.println("</html>");
}
}
Servlet - troubleshooting
o remember the browser caches results
• browser feature: reload without cache
o examine HTML source
• browser features: show source
o display details of response and/or request
• both classes have methods that get this info
• echo to Web page or to log file
14
Servlet troubleshooting – cont'd
o logging
void log(String msg)
void log(String msg, Throwable t)
• will write to the application server's log
• log location depends on implementation
• NetBeans shows server's log in window at bottom
• if date & time needed use:
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
log(dtf.format(LocalDateTime.now()) + " some_msg");
• or use logging framework like Apache Log4j
15
Servlet – class hierarchy
16
from Liang, Introduction to Java Programming, 12th ed
17
from Liang, Introduction to Java Programming, 12th ed
18
from Liang, Introduction to Java Programming, 12th ed
Credits:
o George Kriger
19

More Related Content

Similar to Week 09 -Servlets-part1.pdf

1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jspAnkit Minocha
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0megrhi haikel
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handsonPrashant Kumar
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government DevelopersFrank La Vigne
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsitricks
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSPGary Yeh
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...WebStackAcademy
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsi krishna
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: ServletsFahad Golra
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsitricks
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing Techglyphs
 
SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2Ben Abdallah Helmi
 
Integrating Servlets and JSP (The MVC Architecture)
Integrating Servlets and JSP  (The MVC Architecture)Integrating Servlets and JSP  (The MVC Architecture)
Integrating Servlets and JSP (The MVC Architecture)Amit Ranjan
 
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...Edward Burns
 
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 Connectivitypkaviya
 

Similar to Week 09 -Servlets-part1.pdf (20)

1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handson
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government Developers
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
Web-Socket
Web-SocketWeb-Socket
Web-Socket
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2
 
Servlet programming
Servlet programmingServlet programming
Servlet programming
 
Integrating Servlets and JSP (The MVC Architecture)
Integrating Servlets and JSP  (The MVC Architecture)Integrating Servlets and JSP  (The MVC Architecture)
Integrating Servlets and JSP (The MVC Architecture)
 
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
HTTP/2 Comes to Java: Servlet 4.0 and what it means for the Java/Jakarta EE e...
 
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
 

Recently uploaded

"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 ...Zilliz
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
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 EngineeringWSO2
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 

Recently uploaded (20)

"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 ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Week 09 -Servlets-part1.pdf

  • 1. CST8288 OOP with Design Patterns Fall 2022 Week 09 – Servlets – part1 © 2022 - Copyright Algonquin College and other authors as indicated.
  • 2. Overview o Servlets Part1 – this week • N-Tier applications o Servlets Part2 – Week 9 2
  • 3. o "client - server" • tiers communicate via network protocols • e.g. using JDBC • where client implements: presentation, business rules/logic and data access logic 2 Tiered Applications
  • 4. Multi-tiered Applications 4 Client PC Browser Database Server RDBMS SQL over TCP HTML over HTTP Application Server Servlet Container JDBC Presentation Tier Application Tier Data Tier
  • 5. o in N-tier model • presentation tier • implemented on client device • using content (e.g. HTML) provided by "application" server • business tier • creates presentation content • implements business rules/logic on an "application" server • data tier • implements data access logic on the "application" server • data stored on "database" server • Notes: • servers may be replicated for redundancy and load balancing Multi-tiered - continued
  • 6. Multi-tiered - continued 6 Application Server Servlet Container JDBC HttpServletRequest HttpServletResponse void doGet( __ , __) void doPost(__ , __) HTML over HTTP SQL over TCP
  • 7. Servlet - lifecycle o init() and destroy() from class HttpServlet • init() method called when servlet first created • not called again as long as the servlet is not destroyed. • destroy() method is invoked once: • all in service() method have exited • or after timeout period • releases container resources that were allocated. o service() • invoked each time server receives request for servlet. • server spawns a new thread and invokes service(). 7
  • 8. Servlet lifecycle - continued o more on service() • based on request type, call appropriate method • GET, POST requests go to: doGet(), doPost() are the most common ones • other request map the same way • also provides default handlers • so do not override service() • override specific doXXX() method instead 8
  • 9. Servlet lifecycle - continued 9 from Liang, Introduction to Java Programming, 12th ed
  • 10. classes: HttpServletRequest & HttpServletResponse protected void doXXX(HttpServletRequest req, HttpServletResponse resp) • servlet obtains request from HTTPServletRequest and creates response using HTTPServletResponse • these 2 classes encapsulated request & response • doXXX() methods throws following: ServletException, java.io.IOException 10
  • 11. HTML - document overview <!DOCTYPE html> • first line of HTML doc. • Info about specific version can be added o then 3 pairs of tags: • first 3 tag-pairs: html, head & body <html> <head> non-visible elements here images, scripts & other items can be pre-loaded </head> <body> (visible elements here) </body> </html> 11
  • 12. Servlet – overview of coding o create servlet by: public class ___ extends HttpServlet o then override specific doXXX() methods • if using same code for doGet() and doPost() • then have one call the other or • have both call a common method e.g. in NetBeans skeleton code both doGet() and doPost() call processRequest() 12
  • 13. Servlet coding - continued 13 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try ( PrintWriter out = response.getWriter()) { out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); /* out.println() statements for head section */ out.println("</head>"); out.println("<body>"); /* more out.println() statements for body section */ out.println("</body>"); out.println("</html>"); } }
  • 14. Servlet - troubleshooting o remember the browser caches results • browser feature: reload without cache o examine HTML source • browser features: show source o display details of response and/or request • both classes have methods that get this info • echo to Web page or to log file 14
  • 15. Servlet troubleshooting – cont'd o logging void log(String msg) void log(String msg, Throwable t) • will write to the application server's log • log location depends on implementation • NetBeans shows server's log in window at bottom • if date & time needed use: DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); log(dtf.format(LocalDateTime.now()) + " some_msg"); • or use logging framework like Apache Log4j 15
  • 16. Servlet – class hierarchy 16 from Liang, Introduction to Java Programming, 12th ed
  • 17. 17 from Liang, Introduction to Java Programming, 12th ed
  • 18. 18 from Liang, Introduction to Java Programming, 12th ed