dr. thomas tran – csi3140 lecture notes (based on dr. jeffrey jackson’s slides) chapter 6...
TRANSCRIPT
![Page 1: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/1.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Chapter 6Server-side Programming:
Java Servlets
CSI3140WWW Structures, Techniques, and Standards
![Page 2: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/2.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Server-side Programming
• The combination of – HTML– JavaScript– DOM
is sometimes referred to as Dynamic HTML (DHTML)
• Web pages that include scripting are often called dynamic pages (vs. static)
![Page 3: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/3.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Server-side Programming
• Similarly, web server response can be static or dynamic– Static: HTML document is retrieved from the
file system and returned to the client– Dynamic: HTML document is generated by a
program in response to an HTTP request
• Java servlets are one technology for producing dynamic server responses– Servlet is a Java class instantiated by the
server to produce a dynamic response
![Page 4: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/4.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Servlet Overview
![Page 5: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/5.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Servlet Overview
1. When server starts, it instantiates servlets2. Server receives HTTP request, determines
need for dynamic response3. Server selects the appropriate servlet to
generate the response, creates request/response objects, and passes them to a method on the servlet instance
4. Servlet adds information to response object via method calls
5. Server generates HTTP response based on information stored in response object
![Page 6: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/6.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
![Page 7: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/7.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! ServletAll servlets we will writeare subclasses ofHttpServlet
![Page 8: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/8.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
Server calls doGet() in response to GET request
![Page 9: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/9.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
Interfaces implemented by request/response objects
![Page 10: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/10.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
Production servlet shouldcatch these exceptions
![Page 11: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/11.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
• JWSDP Tomcat server exception handling:– Writing a trace for the exception in logs/jwsdp_log.*.txt log file
– Returning HTML page to client that may (or may not) contain partial exception trace
• If servlet prints a stack trace itself by calling printStackTrace(), or if it writes debugging output to System.out or System.err, this output will be appended to the file logs/launcher.server.log
![Page 12: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/12.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
First twothings doneby typical servlet;must be in thisorder
![Page 13: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/13.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
![Page 14: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/14.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
HTML generated by calling print() orprintln() on the servlet’s PrintWriter object
![Page 15: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/15.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Hello World! Servlet
Good practice to explicitly closethe PrintWriter when done
![Page 16: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/16.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Servlets vs. Java Applications
• Servlets do not have a main()– The main() is in the server– Entry point to servlet code is via call to a
method (doGet() in the example)
• Servlet interaction with end user is indirect via request/response object APIs– Actual HTTP request/response processing is
handled by the server
• Primary servlet output is typically HTML
![Page 17: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/17.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Running Servlets
• Simple way to run a servlet (better later):1. Compile servlet (make sure that JWSDP
libraries are on path)
2. Copy .class file to shared/classes directory
3. (Re)start the Tomcat web server
4. If the class is named ServletHello, browse tohttp://localhost:8080/servlet/ServletHello
![Page 18: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/18.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Dynamic Content
![Page 19: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/19.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Dynamic Content
![Page 20: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/20.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Dynamic Content
![Page 21: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/21.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Dynamic Content
• Potential problems:– Assuming one instance of servlet on one
server, but• Many Web sites are distributed over multiple
servers• Even a single server can (not default) create
multiple instances of a single servlet
– Even if the assumption is correct, this servlet does not handle concurrent accesses properly
• We’ll deal with this later in the chapter
![Page 22: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/22.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Servlet Life Cycle
• Servlet API life cycle methods– init(): called when servlet is instantiated;
must return before any other methods will be called
– service(): method called directly by server when an HTTP request is received; default service() method calls doGet() (or related methods covered later)
– destroy(): called when server shuts down
![Page 23: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/23.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Servlet Life CycleExample life cycle method:attempt to initialize visits variablefrom file
![Page 24: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/24.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Servlet Life Cycle
Exception to be thrownif initialization fails and servletshould not be instantiated
![Page 25: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/25.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• The request object (which implements HttpServletRequest) provides information from the HTTP request to the servlet
• One type of information is parameter data, which is information from the query string portion of the HTTP request
Query string withone parameter
![Page 26: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/26.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• Parameter data is the Web analog of arguments in a method call:
• Query string syntax and semantics
![Page 27: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/27.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• Query string syntax and semantics– Multiple parameters separated by &
– Order of parameters does not matter
– All parameter values are strings
Value of arg is empty string
![Page 28: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/28.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• A parameter name or value can be any sequence of 8-bit characters
• URL encoding is used to represent non-alphanumeric characters:
• URL decoding applied by server to retrieve intended name or value
Value of arg is‘a String’
![Page 29: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/29.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• URL encoding algorithm
![Page 30: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/30.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
![Page 31: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/31.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
![Page 32: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/32.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
![Page 33: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/33.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter DataMust escape XML special characters inall user-supplied data before adding to HTMLto avoid cross-site scripting attacks
![Page 34: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/34.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• Cross-site scripting
Attacker
Blogging Website
Comment containing<script> element
Document containingattacker’s comment (and script)Victim
![Page 35: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/35.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
Also need to escape quotes withinattribute values.
![Page 36: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/36.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
![Page 37: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/37.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• A form automatically generates a query string when submitted– Parameter name specified by value of name
attributes of form controls
– Parameter value depends on control type
Value for checkboxspecified by value attribute
![Page 38: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/38.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
![Page 39: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/39.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
username
lifestory
boxgroup1 (values same as labels)doit
![Page 40: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/40.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• Query string produced by browser (all one line):
Checkbox parameters have same name values;only checked boxes have corresponding parameters
![Page 41: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/41.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• GET vs. POST for the method attribute of forms:– GET:
• Query string is part of URL• Length of query string may be limited• Recommended when parameter data is not stored
or updated on the server, but used only to request information (e.g., search engine query)
– The URL can be bookmarked or emailed and the same data will be passed to the server when the URL is revisited
![Page 42: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/42.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
Browser content copyright 2004 Google, Inc. Used by permission.
![Page 43: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/43.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
• GET vs. POST method for forms:– POST:
• Query string is sent as body of HTTP request• Length of query string is unlimited• Recommended if parameter data is intended to
cause the server to update stored data• Most browsers will warn you if they are about to
resubmit POST data to avoid duplicate updates
![Page 44: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/44.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Parameter Data
![Page 45: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/45.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
• Many interactive Web sites spread user data entry out over several pages:– Ex: add items to cart, enter shipping
information, enter billing information
• Problem: how does the server know which users generated which HTTP requests?– Cannot rely on standard HTTP headers to
identify a user
![Page 46: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/46.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
![Page 47: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/47.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Server sends backnew unique session ID when the request hasnone
![Page 48: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/48.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Client that supportssession stores theID and sends itback to the serverin subsequentrequests
![Page 49: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/49.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Server knowsthat all of theserequests arefrom the sameclient. Theset of requestsis known as asession.
![Page 50: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/50.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
And the serverknows that allof theserequests arefrom a differentclient.
![Page 51: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/51.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
HttpSession object is created by server when a servlet calls getSession() method on its HttpServletRequest parameter.
getSession() method returns HttpSession object associated with this HTTP request.• Creates new HttpSession object if no valid session ID in HTTP request • Otherwise, returns previously created HttpSession object containing the session ID
![Page 52: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/52.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Boolean indicating whether returnedobject was newly created or alreadyexisted.
![Page 53: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/53.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Incremented once per session
![Page 54: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/54.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Three webpages producedby a single servlet
![Page 55: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/55.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
![Page 56: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/56.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
![Page 57: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/57.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
![Page 58: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/58.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
![Page 59: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/59.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
,,,
![Page 60: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/60.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
,,, Session attribute is aname/value pair
![Page 61: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/61.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
,,,
Session attribute willhave null value untila value is assigned
![Page 62: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/62.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
,,,
Generatesign-in formif session isnew orsignIn attribute has no value,generate weclome-back pageotherwise.
![Page 63: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/63.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Sign-in form
Welcome-backpage
![Page 64: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/64.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Second argument (“Greeting”) used as action attribute value(relative URL)
![Page 65: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/65.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Form will be sent using POST HTTPMethod, so doPost() method will be called
![Page 66: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/66.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Text field containinguser name is namedsignIn
![Page 67: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/67.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
…
![Page 68: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/68.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
…RetrievesignInparameter value
![Page 69: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/69.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
…
Normalprocessing:signInparameteris present inHTTP request
![Page 70: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/70.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
…
GenerateHTML forresponse
![Page 71: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/71.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
Thank-you page Must escape XML specialcharacters inuser input
![Page 72: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/72.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
…
Assign avalue to thesignIn sessionattribute
![Page 73: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/73.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
• Session attribute methods:– setAttribute(String name, Object value): creates a session attribute with the given name and value
– Object getAttribute(String name): returns the value of the session attribute named name, or returns null if this session does not have an attribute with this name
![Page 74: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/74.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
…
Errorprocessing(return userto sign-in form)
![Page 75: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/75.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Sessions
• By default, each session expires if a server-determined length of time elapses between a session’s HTTP requests– Server destroys the corresponding session
object
• Servlet code can:– Terminate a session by calling invalidate() method on session object
– Set the expiration time-out duration (secs) by calling setMaxInactiveInterval(int)
![Page 76: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/76.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
• A cookie is a name/value pair in the Set-Cookie header field of an HTTP response
• Most (not all) clients will:– Store each cookie received in its file system– Send each cookie back to the server that sent
it as part of the Cookie header field of subsequent HTTP requests
![Page 77: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/77.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
Tomcat sendssession ID as valueof cookie namedJSESSIONID
![Page 78: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/78.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
Cookie-enabledbrowser returnssession ID as valueof cookie namedJSESSIONID
![Page 79: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/79.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
• Servlets can set cookies explicitly– Cookie class used to represent cookies– request.getCookies() returns an array of
Cookie instances representing cookie data in HTTP request
– response.addCookie(Cookie) adds a cookie to the HTTP response
![Page 80: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/80.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
Cookies are expired byclient (server can requestexpiration date)
![Page 81: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/81.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
![Page 82: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/82.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
Return array of cookiescontained in HTTP request
![Page 83: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/83.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
Search forcookienamedCOUNT andextract valueas an int
![Page 84: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/84.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
![Page 85: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/85.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
Sendreplacementcookie valueto client(overwritesexisting cookie)
![Page 86: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/86.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies
Should calladdCookie()before writingHTML
![Page 87: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/87.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies Privacy issues
Client
Web siteprovidingrequested
content
HTTP request tointended site
HTTP response:HTML documentincluding ad <img>
Web siteprovidingbanner
ads
HTTP request forad image
Imageplus Set-Cookiein response:third-party cookie
![Page 88: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/88.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Web siteprovidingrequested
content
Cookies Privacy issues
Client
SecondWeb siteprovidingrequested
content
HTTP request to 2ndintended site
HTTP response:HTML documentincluding ad <img>
Web siteprovidingbanner
ads
HTTP request forad image plus Cookie (identifies user)
Image Based onReferer, I know twoWeb sites thatthis user hasvisited
![Page 89: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/89.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Cookies Privacy issues
• Due to privacy concerns, many users block cookies– Blocking may be fine-tuned. Ex: Mozilla
allows • Blocking of third-party cookies• Blocking based on on-line privacy policy
• Alternative to cookies for maintaining session: URL rewriting
![Page 90: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/90.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
Tomcat addssession ID withinHTML documentto all URL’s referring to the servlet Session ID = 4235
![Page 91: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/91.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
Subsequentrequest will containsession ID in theURL of the request
Session ID = 4235
![Page 92: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/92.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
Next response mustagain add session IDto all URL’s Session ID = 4235
![Page 93: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/93.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
• Original (relative) URL: href=“URLEncodedGreeting”
• URL containing session ID: href=“URLEncodedGreeting;jsessionid=0157B9E85”
• Path parameter is treated differently than query string parameter– Ex: invisible to getParameter()
Path parameter
![Page 94: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/94.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
• HttpServletResponse method encodeURL() will add session id path parameter to argument URL
Relative URL of servlet
Originalservlet
Servletusing URLrewriting
![Page 95: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/95.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
• Must rewrite every servlet URL in every document
• Security issuesWeb site usingURL rewriting
User A
URL withsession ID7152
![Page 96: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/96.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
• Must rewrite every servlet URL in every document
• Security issuesWeb site usingURL rewriting
User A User BEmail URL
URL withsession ID7152
![Page 97: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/97.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
URL Rewriting
• Must rewrite every servlet URL in every document
• Security issuesWeb site usingURL rewriting
User A
URL withsession ID7152
User BEmail URL
Visit Web site withsession ID 7152
![Page 98: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/98.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
More Servlet Methods
![Page 99: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/99.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
More Servlet Methods
![Page 100: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/100.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
More Servlet Methods
![Page 101: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/101.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
More Servlet Methods
• Response buffer– All data sent to the PrintWriter object is
stored in a buffer– When the buffer is full, it is automatically
flushed: • Contents are sent to the client (preceded by
header fields, if this is the first flush)• Buffer becomes empty
– Note that all header fields must be defined before the first buffer flush
![Page 102: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/102.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
More Servlet Methods
![Page 103: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/103.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
More Servlet Methods
• In addition to doGet() and doPost(), servlets have methods corresponding to other HTTP request methods– doHead(): automatically defined if doGet()
is overridden– doOptions(), doTrace(): useful default
methods provided– doDelete(), doPut(): override to support
these methods
![Page 104: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/104.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Data Storage
• Almost all web applications (servlets or related dynamic web server software) store and retrieve data– Typical web app uses a data base management
system (DBMS)– Another option is to use the file system– Not web technologies, so beyond our scope
• Some Java data storage details provided in Appendices B (file system) and C (DBMS)
• One common problem: concurrency
![Page 105: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/105.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
![Page 106: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/106.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
• Tomcat creates a separate thread for each HTTP request
• Java thread state saved:– Which statement to be executed next– The call stack: where the current method will
return to, where that method will return to, etc. plus parameter values for each method
– The values of local variables for all methods on the call stack
![Page 107: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/107.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
• Some examples of values that are not saved when a thread is suspended:– Values of instance variables (variables
declared outside of methods)– Values of class variables (variables declared
as static outside of methods)– Contents of files and other external resources
![Page 108: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/108.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
// Output HTML document
![Page 109: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/109.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
![Page 110: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/110.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
• Java support thread synchronization
– Only one synchronized method within a class can be called at any one time
Only one thread atat time can call doGet()
![Page 111: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/111.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
![Page 112: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/112.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
• Web application with multiple servlet classes and shared resource:
![Page 113: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/113.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Concurrency
• Solution: create a shared class with synchronized static methods called by both servlets
CounterFileCounterReader CounterWriterreadAndReset() incr()
File
![Page 114: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/114.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Common Gateway Interface
• CGI was the earliest standard technology used for dynamic server-side content
• CGI basics:– HTTP request information is stored in
environment variables (e.g., QUERY_STRING, REQUEST_METHOD, HTTP_USER_AGENT)
– Program is executed, output is returned in HTTP response
![Page 115: Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides) Chapter 6 Server-side Programming: Java Servlets CSI3140 WWW Structures,](https://reader035.vdocument.in/reader035/viewer/2022062217/56649daf5503460f94a9cf2c/html5/thumbnails/115.jpg)
Dr. Thomas Tran – CSI3140 Lecture Notes (based on Dr. Jeffrey Jackson’s slides)
Common Gateway Interface
• Advantage:– Program can be written in any programming
language (Perl frequently used)
• Disadvantages:– No standard for concepts such as session– May be slower (programs normally run in
separate processes, not server process)