SlideShare a Scribd company logo
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 jsp
Ankit Minocha
 
servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0servlet 2.5 & JSP 2.0
servlet 2.5 & JSP 2.0
megrhi haikel
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handson
Prashant Kumar
 
Intro to .NET for Government Developers
Intro to .NET for Government DevelopersIntro to .NET for Government Developers
Intro to .NET for Government Developers
Frank 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 JSP
Gary Yeh
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
deepak kumar
 
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 technology
Tanmoy 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: Servlets
Fahad 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
 
Web-Socket
Web-SocketWeb-Socket
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
ramya marichamy
 
SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2SCWCD : The servlet model CHAP : 2
SCWCD : The servlet model CHAP : 2
Ben Abdallah Helmi
 
Servlet programming
Servlet programmingServlet programming
Servlet programming
Mallikarjuna G D
 
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 Connectivity
pkaviya
 

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

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 

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