SlideShare a Scribd company logo
1 of 113
CSI 3140
WWW Structures, Techniques and Standards

Server-side Programming:
Java Servlets
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)
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

2
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 class instantiated by the server to
produce a dynamic response
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

3
Servlet Overview

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

4
Servlet Overview
1.When server starts it instantiates servlets
2.Server receives HTTP request, determines need
for dynamic response
3.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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

5
Hello World! Servlet

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

6
Hello World! Servlet
All servlets we will write
are subclasses of
HttpServlet

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

7
Hello World! Servlet

Server calls doGet() in response to GET request

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

8
Hello World! Servlet

Interfaces implemented by request/response objects

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

9
Hello World! Servlet

Production servlet should
catch these exceptions

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

10
Hello World! Servlet
JWSDP Tomcat server exception handling:




Stack trace appended to
logs/jwsdp_log.*.txt
HTML document returned to client may (or may
not) contain partial stack trace

Servlet output to
System.out.print(),
printStackTrace(), etc. is appended to
logs/launcher.server.log
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

11
Hello World! Servlet

First two
things done
by typical servlet;
must be in this
order

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

12
Hello World! Servlet

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

13
Hello World! Servlet

HTML generated by calling print() or
println() on the servlet’s
PrintWriter object

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

14
Hello World! Servlet

Good practice to explicitly close
the PrintWriter when done

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

15
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

16
Running Servlets
 Simple way to run a servlet (better later):
1.

2.

3.
4.

Compile servlet (make sure that JWSDP
libraries are on path)
Copy .class file to shared/classes
directory
(Re)start the Tomcat web server
If the class is named ServletHello, browse
to
http://localhost:8080/servlet/ServletHello
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

17
Dynamic Content

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

18
Dynamic Content

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

19
Dynamic Content

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

20
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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

21
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

22
Servlet Life Cycle
Example life cycle method:
attempt to initialize visits variable
from file

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

23
Servlet Life Cycle

Exception to be thrown
if initialization fails and servlet
should not be instantiated

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

24
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 with
one parameter
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

25
Parameter Data
Parameter data is the Web analog of
arguments in a method call:

Query string syntax and semantics

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

26
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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

27
Parameter Data
Parameter names and values can be any 8bit characters
URL encoding is used to represent nonalphanumeric characters:
Value of arg is
‘a String’

URL decoding applied by server to retrieve
intended name or value
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

28
Parameter Data
URL encoding algorithm

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

29
Parameter Data

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

30
Parameter Data

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

31
Parameter Data

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

32
Parameter Data
Must escape XML special characters in
all user-supplied data before adding to HTML
to avoid cross-site scripting attacks

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

33
Parameter Data
Cross-site scripting
Attacker

Comment containing
<script> element
Blogging Web
site

Victim

Document containing
attacker’s comment (and script)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

34
Parameter Data

Also need to escape quotes within
attribute values.

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

35
Parameter Data

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

36
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 checkbox
specified by value attribute
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

37
Parameter Data

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

38
Parameter Data
username

lifestory
doit

boxgroup1 (values same as labels)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

39
Parameter Data
Query string produced by browser (all one
line):

Checkbox parameters have same name values;
only checked boxes have corresponding parameters

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

40
Parameter Data
GET vs. POST method for forms:


GET:
 Query

string is part of URL
 Length of query string may be limited
 Recommended when parameter data is not stored 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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

41
Parameter Data

Browser content copyright 2004 Google, Inc. Used by permission.

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

42
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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

43
Parameter Data

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

44
Parameter Data
GET vs. POST in a Web application:
According to the HTTP 1.1 specification (RFC 2616):
In particular, the convention has been established that the
GET and HEAD methods SHOULD NOT have the
significance of taking an action other than retrieval
A consequence of this is that “web accelerators” might prefetch your GET-based URL, with possible disastrous
consequences if you actually use GET for processing.
(note that because of this, accelerators such as Google’s
won’t pre-fetch GET-based URLs with parameters)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

45
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

46
Sessions

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

47
Sessions

Server sends back
new unique
session ID when
the request has
none

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

48
Sessions

Client that supports
session stores the
ID and sends it
back to the server
in subsequent
requests

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

49
Sessions

Server knows
that all of these
requests are
from the same
client. The
set of requests
is known as a
session.

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

50
Sessions
And the server
knows that all
of these
requests are
from a different
client.

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

51
Sessions

Returns HttpSession object associated
with this HTTP request.
• Creates new HttpSession object if no
session ID in request or no object with
this ID exists
• Otherwise, returns previously created
object

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

52
Sessions

Boolean indicating whether returned
object was newly created or already
existed.

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

53
Sessions

Incremented once per session

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

54
Sessions

Three web
pages produced
by a single servlet

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

55
Sessions

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

56
Sessions
,,,

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

57
Sessions
,,,

Session attribute is a
name/value pair

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

58
Sessions
,,,

Session attribute will
have null value until
a value is assigned

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

59
Sessions
,,,

Generate
sign-in form
if session is
new or
signIn
attribute has no value,
weclome-back page
otherwise.

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

60
Sessions

Sign-in form

Welcome-back
page

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

61
Sessions
Second argument
(“Greeting”) used as
action attribute value
(relative URL)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

62
Sessions

Form will be sent using POST HTTP
method (doPost() method will be called)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

63
Sessions

Text field containing
user name is named
signIn

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

64
Sessions
…

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

65
Sessions
…
Retrieve
signIn
parameter value

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

66
Sessions
…

Normal
processing:
signIn
parameter
is present in
HTTP request

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

67
Sessions
…

Generate
HTML for
response

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

68
Sessions

Thank-you page

Must escape
XML special
characters in
user input

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

69
Sessions
…

Assign a
value to the
signIn session
attribute

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

70
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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

71
Sessions
…

Error
processing
(return user
to sign-in form)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

72
Sessions
By default, each session expires if a serverdetermined 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)
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

73
Cookies
A cookie is a name/value pair in the SetCookie 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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

74
Cookies

Tomcat sends
session ID as value
of cookie named
JSESSIONID

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

75
Cookies

Cookie-enabled
browser returns
session ID as value
of cookie named
JSESSIONID

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

76
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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

77
Cookies

Cookies are expired by
client (server can request
expiration date)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

78
Cookies

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

79
Cookies

Return array of cookies
contained in HTTP request

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

80
Cookies

Search for
cookie
named
COUNT and
extract value
as an int

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

81
Cookies

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

82
Cookies
Send
replacement
cookie value
to client
(overwrites
existing cookie)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

83
Cookies

Should call
addCookie()
before writing
HTML

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

84
Cookies
Privacy issues
HTTP request to
intended site

Client

HTTP response:
HTML document
including ad <img>

Web site
providing
requested
content

HTTP request for
ad image
Image
plus Set-Cookie
in response:
third-party cookie

Web site
providing
banner
ads

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

85
Cookies
Privacy issues
HTTP request to 2nd
intended site

Client

HTTP response:
HTML document
including ad <img>

Web site
providing
requested
content

Second
Web site
providing
requested
content

HTTP request for
ad image plus Cookie (identifies user)
Image

Web site
providing
banner
ads

Based on
Referer, I know two
Web sites that
this user has
visited

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

86
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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

87
URL Rewriting

Tomcat adds
session ID within
HTML document
to all URL’s
referring to the
servlet

Session ID = 4235

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

88
URL Rewriting

Subsequent
request will contain
session ID in the
URL of the request

Session ID = 4235

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

89
URL Rewriting

Next response must
again add session ID
to all URL’s

Session ID = 4235

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

90
URL Rewriting
Original (relative) URL:
href=“URLEncodedGreeting”

URL containing session ID:
href=“URLEncodedGreeting;jsessionid=0157B9E85”
Path parameter

Path parameter is treated differently than
query string parameter


Ex: invisible to getParameter()
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

91
URL Rewriting

Original
servlet

HttpServletResponse method
encodeURL() will add session id path
parameter to argument URL
Relative URL of servlet

Servlet
using URL
rewriting

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

92
URL Rewriting
Must rewrite every servlet URL in every
document
Security issues
URL with
session ID
7152

Web site using
URL rewriting

User A

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

93
URL Rewriting
Must rewrite every servlet URL in every
document
Security issues
URL with
session ID
7152

User A

Web site using
URL rewriting

Email URL

User B

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

94
URL Rewriting
Must rewrite every servlet URL in every
document
Security issues
URL with
session ID
7152

User A

Web site using
URL rewriting

Email URL

Visit Web site with
session ID 7152

User B

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

95
More Servlet Methods

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

96
More Servlet Methods

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

97
More Servlet Methods

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

98
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

99
More Servlet Methods

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

100
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

101
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

102
Concurrency

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

103
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

104
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

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

105
Concurrency

// Output HTML document

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

106
Concurrency

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

107
Concurrency
Java support thread synchronization
Only one thread at
at time can call doGet()



Only one synchronized method within a class
can be called at any one time

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

108
Concurrency

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

109
Concurrency
Web application with multiple servlet
classes and shared resource:

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

110
Concurrency
Solution: create a shared class with
synchronized static methods called by both
servlets
CounterReader

readAndReset()

CounterFile

incr()

CounterWriter

File
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

111
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
Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

112
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)

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides

113

More Related Content

What's hot

Don't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadDon't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadWASdev Community
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletFahmi Jafar
 
Realtime web application with java
Realtime web application with javaRealtime web application with java
Realtime web application with javaJeongHun Byeon
 
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!Masoud Kalali
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaJames Falkner
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsSarvesh Kushwaha
 
Faster Java EE Builds with Gradle
Faster Java EE Builds with GradleFaster Java EE Builds with Gradle
Faster Java EE Builds with GradleRyan Cuprak
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkFabio Tiriticco
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessionsvantinhkhuc
 
Atlassian meets Kerberos
Atlassian meets KerberosAtlassian meets Kerberos
Atlassian meets KerberosNils Hofmeister
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: ServletsFahad Golra
 
Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Ryan Cuprak
 
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
 

What's hot (19)

Don't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 InsteadDon't Wait! Develop Responsive Applications with Java EE7 Instead
Don't Wait! Develop Responsive Applications with Java EE7 Instead
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
 
Realtime web application with java
Realtime web application with javaRealtime web application with java
Realtime web application with java
 
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
Server Sent Events, Async Servlet, Web Sockets and JSON; born to work together!
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and Java
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC Applications
 
Faster Java EE Builds with Gradle
Faster Java EE Builds with GradleFaster Java EE Builds with Gradle
Faster Java EE Builds with Gradle
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! Framework
 
Servlet sessions
Servlet sessionsServlet sessions
Servlet sessions
 
ASP.NET WEB API Training
ASP.NET WEB API TrainingASP.NET WEB API Training
ASP.NET WEB API Training
 
Atlassian meets Kerberos
Atlassian meets KerberosAtlassian meets Kerberos
Atlassian meets Kerberos
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 2: Servlets
Lecture 2:  ServletsLecture 2:  Servlets
Lecture 2: Servlets
 
JEE Programming - 04 Java Servlets
JEE Programming - 04 Java ServletsJEE Programming - 04 Java Servlets
JEE Programming - 04 Java Servlets
 
Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)
 
Windows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside worldWindows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside world
 
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...
 
Web API Basics
Web API BasicsWeb API Basics
Web API Basics
 

Viewers also liked

Java web programming
Java web programmingJava web programming
Java web programmingjkumaranc
 
Java web programming
Java web programmingJava web programming
Java web programmingChing Yi Chan
 
Application Of Software Design Pattern
Application Of Software Design PatternApplication Of Software Design Pattern
Application Of Software Design Patternguest46da5428
 
use web template by Bootstrap
use web template by Bootstrapuse web template by Bootstrap
use web template by BootstrapCaesar Chi
 
Java EE 02-First Servlet
Java EE 02-First ServletJava EE 02-First Servlet
Java EE 02-First ServletFernando Gil
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationIMC Institute
 
Java Web Start - How Zhara POS Works
Java Web Start - How Zhara POS WorksJava Web Start - How Zhara POS Works
Java Web Start - How Zhara POS WorksYohan Liyanage
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slidesSasidhar Kothuru
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsAnton Keks
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedIMC Institute
 
Observer Software Design Pattern
Observer Software Design Pattern Observer Software Design Pattern
Observer Software Design Pattern Nirthika Rajendran
 
Web Application Introduction
Web Application  IntroductionWeb Application  Introduction
Web Application Introductionshaojung
 
Programming paradigm and web programming
Programming paradigm and web programmingProgramming paradigm and web programming
Programming paradigm and web programmingMohammad Kamrul Hasan
 
J2ee (java ee) tutorial for beginners
J2ee (java ee) tutorial for beginnersJ2ee (java ee) tutorial for beginners
J2ee (java ee) tutorial for beginnersinTwentyEight Minutes
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)Talha Ocakçı
 

Viewers also liked (20)

Java web programming
Java web programmingJava web programming
Java web programming
 
Java web programming
Java web programmingJava web programming
Java web programming
 
Java web programming
Java web programmingJava web programming
Java web programming
 
Application Of Software Design Pattern
Application Of Software Design PatternApplication Of Software Design Pattern
Application Of Software Design Pattern
 
use web template by Bootstrap
use web template by Bootstrapuse web template by Bootstrap
use web template by Bootstrap
 
Java and the Web
Java and the WebJava and the Web
Java and the Web
 
Java EE 02-First Servlet
Java EE 02-First ServletJava EE 02-First Servlet
Java EE 02-First Servlet
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web Application
 
Java Web Start - How Zhara POS Works
Java Web Start - How Zhara POS WorksJava Web Start - How Zhara POS Works
Java Web Start - How Zhara POS Works
 
Web Technologies -- Servlets 4 unit slides
Web Technologies -- Servlets   4 unit slidesWeb Technologies -- Servlets   4 unit slides
Web Technologies -- Servlets 4 unit slides
 
Java Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & ServletsJava Course 12: XML & XSL, Web & Servlets
Java Course 12: XML & XSL, Web & Servlets
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
Servlet and JSP Lifecycle
Servlet and JSP LifecycleServlet and JSP Lifecycle
Servlet and JSP Lifecycle
 
Observer Software Design Pattern
Observer Software Design Pattern Observer Software Design Pattern
Observer Software Design Pattern
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 
Web Application Introduction
Web Application  IntroductionWeb Application  Introduction
Web Application Introduction
 
Programming paradigm and web programming
Programming paradigm and web programmingProgramming paradigm and web programming
Programming paradigm and web programming
 
J2ee (java ee) tutorial for beginners
J2ee (java ee) tutorial for beginnersJ2ee (java ee) tutorial for beginners
J2ee (java ee) tutorial for beginners
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
Web programming
Web programmingWeb programming
Web programming
 

Similar to Server side

Sitecore - Deep drive into the Sitecore Client pipelines
Sitecore - Deep drive into the Sitecore Client pipelinesSitecore - Deep drive into the Sitecore Client pipelines
Sitecore - Deep drive into the Sitecore Client pipelinesBenjamin Vangansewinkel
 
Google Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with ZabbixGoogle Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with ZabbixMax Kuzkin
 
St0 029 question answers
St0 029 question answersSt0 029 question answers
St0 029 question answersMarcoMCervantes
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desaijinaldesailive
 
Unit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxUnit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxAbhijayKulshrestha1
 
DEF CON 27 - BEN SADEGHIPOUR - owning the clout through ssrf and pdf generators
DEF CON 27 - BEN SADEGHIPOUR  - owning the clout through ssrf and pdf generatorsDEF CON 27 - BEN SADEGHIPOUR  - owning the clout through ssrf and pdf generators
DEF CON 27 - BEN SADEGHIPOUR - owning the clout through ssrf and pdf generatorsFelipe Prado
 
Http/2 - What's it all about?
Http/2  - What's it all about?Http/2  - What's it all about?
Http/2 - What's it all about?Andy Davies
 
Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...
Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...
Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...Amaaira Johns
 
z/Ware 2.0 Features Overview
z/Ware 2.0 Features Overviewz/Ware 2.0 Features Overview
z/Ware 2.0 Features Overviewillustrosystems
 
Experienced Selenium Interview questions
Experienced Selenium Interview questionsExperienced Selenium Interview questions
Experienced Selenium Interview questionsarchana singh
 
SwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupSwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupErnest Jumbe
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentationwebhostingguy
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentationwebhostingguy
 
Adobe analytics implementation secret hacks
Adobe analytics implementation secret hacksAdobe analytics implementation secret hacks
Adobe analytics implementation secret hacksAlban Gérôme
 

Similar to Server side (20)

Sitecore - Deep drive into the Sitecore Client pipelines
Sitecore - Deep drive into the Sitecore Client pipelinesSitecore - Deep drive into the Sitecore Client pipelines
Sitecore - Deep drive into the Sitecore Client pipelines
 
Google Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with ZabbixGoogle Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with Zabbix
 
St0 029 question answers
St0 029 question answersSt0 029 question answers
St0 029 question answers
 
PPT
PPTPPT
PPT
 
ql.io at NodePDX
ql.io at NodePDXql.io at NodePDX
ql.io at NodePDX
 
Mvc interview questions – deep dive jinal desai
Mvc interview questions – deep dive   jinal desaiMvc interview questions – deep dive   jinal desai
Mvc interview questions – deep dive jinal desai
 
Unit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptxUnit 38 - Spring MVC Introduction.pptx
Unit 38 - Spring MVC Introduction.pptx
 
DEF CON 27 - BEN SADEGHIPOUR - owning the clout through ssrf and pdf generators
DEF CON 27 - BEN SADEGHIPOUR  - owning the clout through ssrf and pdf generatorsDEF CON 27 - BEN SADEGHIPOUR  - owning the clout through ssrf and pdf generators
DEF CON 27 - BEN SADEGHIPOUR - owning the clout through ssrf and pdf generators
 
Hacking Web Performance
Hacking Web Performance Hacking Web Performance
Hacking Web Performance
 
Codeigniter
CodeigniterCodeigniter
Codeigniter
 
Http/2 - What's it all about?
Http/2  - What's it all about?Http/2  - What's it all about?
Http/2 - What's it all about?
 
Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...
Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...
Study Guide for Preparing Citrix Certified Professional - Networking (1Y0-341...
 
Servlets lecture1
Servlets lecture1Servlets lecture1
Servlets lecture1
 
z/Ware 2.0 Features Overview
z/Ware 2.0 Features Overviewz/Ware 2.0 Features Overview
z/Ware 2.0 Features Overview
 
Experienced Selenium Interview questions
Experienced Selenium Interview questionsExperienced Selenium Interview questions
Experienced Selenium Interview questions
 
SwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup GroupSwampDragon presentation: The Copenhagen Django Meetup Group
SwampDragon presentation: The Copenhagen Django Meetup Group
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentation
 
PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentation
 
000 252
000 252000 252
000 252
 
Adobe analytics implementation secret hacks
Adobe analytics implementation secret hacksAdobe analytics implementation secret hacks
Adobe analytics implementation secret hacks
 

More from philipsinter

More from philipsinter (10)

Fundamentals of Database system
Fundamentals of Database systemFundamentals of Database system
Fundamentals of Database system
 
MULTIMEDIA Cocomo forum version5
MULTIMEDIA Cocomo forum version5 MULTIMEDIA Cocomo forum version5
MULTIMEDIA Cocomo forum version5
 
Multimedia
Multimedia Multimedia
Multimedia
 
multimedia 01
multimedia 01multimedia 01
multimedia 01
 
Xml 215-presentation
Xml 215-presentationXml 215-presentation
Xml 215-presentation
 
Xml
XmlXml
Xml
 
Java servlet
Java servletJava servlet
Java servlet
 
Dbms
DbmsDbms
Dbms
 
Lecture2
Lecture2Lecture2
Lecture2
 
Final vlsi projectreport
Final vlsi projectreportFinal vlsi projectreport
Final vlsi projectreport
 

Server side

  • 1. CSI 3140 WWW Structures, Techniques and Standards Server-side Programming: Java Servlets
  • 2. 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) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 2
  • 3. 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 class instantiated by the server to produce a dynamic response Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 3
  • 4. Servlet Overview Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 4
  • 5. Servlet Overview 1.When server starts it instantiates servlets 2.Server receives HTTP request, determines need for dynamic response 3.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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 5
  • 6. Hello World! Servlet Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 6
  • 7. Hello World! Servlet All servlets we will write are subclasses of HttpServlet Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 7
  • 8. Hello World! Servlet Server calls doGet() in response to GET request Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 8
  • 9. Hello World! Servlet Interfaces implemented by request/response objects Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 9
  • 10. Hello World! Servlet Production servlet should catch these exceptions Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 10
  • 11. Hello World! Servlet JWSDP Tomcat server exception handling:   Stack trace appended to logs/jwsdp_log.*.txt HTML document returned to client may (or may not) contain partial stack trace Servlet output to System.out.print(), printStackTrace(), etc. is appended to logs/launcher.server.log Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 11
  • 12. Hello World! Servlet First two things done by typical servlet; must be in this order Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 12
  • 13. Hello World! Servlet Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 13
  • 14. Hello World! Servlet HTML generated by calling print() or println() on the servlet’s PrintWriter object Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 14
  • 15. Hello World! Servlet Good practice to explicitly close the PrintWriter when done Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 15
  • 16. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 16
  • 17. Running Servlets  Simple way to run a servlet (better later): 1. 2. 3. 4. Compile servlet (make sure that JWSDP libraries are on path) Copy .class file to shared/classes directory (Re)start the Tomcat web server If the class is named ServletHello, browse to http://localhost:8080/servlet/ServletHello Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 17
  • 18. Dynamic Content Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 18
  • 19. Dynamic Content Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 19
  • 20. Dynamic Content Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 20
  • 21. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 21
  • 22. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 22
  • 23. Servlet Life Cycle Example life cycle method: attempt to initialize visits variable from file Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 23
  • 24. Servlet Life Cycle Exception to be thrown if initialization fails and servlet should not be instantiated Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 24
  • 25. 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 with one parameter Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 25
  • 26. Parameter Data Parameter data is the Web analog of arguments in a method call: Query string syntax and semantics Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 26
  • 27. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 27
  • 28. Parameter Data Parameter names and values can be any 8bit characters URL encoding is used to represent nonalphanumeric characters: Value of arg is ‘a String’ URL decoding applied by server to retrieve intended name or value Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 28
  • 29. Parameter Data URL encoding algorithm Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 29
  • 30. Parameter Data Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 30
  • 31. Parameter Data Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 31
  • 32. Parameter Data Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 32
  • 33. Parameter Data Must escape XML special characters in all user-supplied data before adding to HTML to avoid cross-site scripting attacks Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 33
  • 34. Parameter Data Cross-site scripting Attacker Comment containing <script> element Blogging Web site Victim Document containing attacker’s comment (and script) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 34
  • 35. Parameter Data Also need to escape quotes within attribute values. Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 35
  • 36. Parameter Data Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 36
  • 37. 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 checkbox specified by value attribute Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 37
  • 38. Parameter Data Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 38
  • 39. Parameter Data username lifestory doit boxgroup1 (values same as labels) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 39
  • 40. Parameter Data Query string produced by browser (all one line): Checkbox parameters have same name values; only checked boxes have corresponding parameters Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 40
  • 41. Parameter Data GET vs. POST method for forms:  GET:  Query string is part of URL  Length of query string may be limited  Recommended when parameter data is not stored 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 41
  • 42. Parameter Data Browser content copyright 2004 Google, Inc. Used by permission. Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 42
  • 43. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 43
  • 44. Parameter Data Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 44
  • 45. Parameter Data GET vs. POST in a Web application: According to the HTTP 1.1 specification (RFC 2616): In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval A consequence of this is that “web accelerators” might prefetch your GET-based URL, with possible disastrous consequences if you actually use GET for processing. (note that because of this, accelerators such as Google’s won’t pre-fetch GET-based URLs with parameters) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 45
  • 46. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 46
  • 47. Sessions Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 47
  • 48. Sessions Server sends back new unique session ID when the request has none Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 48
  • 49. Sessions Client that supports session stores the ID and sends it back to the server in subsequent requests Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 49
  • 50. Sessions Server knows that all of these requests are from the same client. The set of requests is known as a session. Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 50
  • 51. Sessions And the server knows that all of these requests are from a different client. Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 51
  • 52. Sessions Returns HttpSession object associated with this HTTP request. • Creates new HttpSession object if no session ID in request or no object with this ID exists • Otherwise, returns previously created object Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 52
  • 53. Sessions Boolean indicating whether returned object was newly created or already existed. Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 53
  • 54. Sessions Incremented once per session Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 54
  • 55. Sessions Three web pages produced by a single servlet Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 55
  • 56. Sessions Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 56
  • 57. Sessions ,,, Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 57
  • 58. Sessions ,,, Session attribute is a name/value pair Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 58
  • 59. Sessions ,,, Session attribute will have null value until a value is assigned Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 59
  • 60. Sessions ,,, Generate sign-in form if session is new or signIn attribute has no value, weclome-back page otherwise. Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 60
  • 61. Sessions Sign-in form Welcome-back page Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 61
  • 62. Sessions Second argument (“Greeting”) used as action attribute value (relative URL) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 62
  • 63. Sessions Form will be sent using POST HTTP method (doPost() method will be called) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 63
  • 64. Sessions Text field containing user name is named signIn Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 64
  • 65. Sessions … Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 65
  • 66. Sessions … Retrieve signIn parameter value Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 66
  • 67. Sessions … Normal processing: signIn parameter is present in HTTP request Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 67
  • 68. Sessions … Generate HTML for response Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 68
  • 69. Sessions Thank-you page Must escape XML special characters in user input Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 69
  • 70. Sessions … Assign a value to the signIn session attribute Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 70
  • 71. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 71
  • 72. Sessions … Error processing (return user to sign-in form) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 72
  • 73. Sessions By default, each session expires if a serverdetermined 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) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 73
  • 74. Cookies A cookie is a name/value pair in the SetCookie 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 74
  • 75. Cookies Tomcat sends session ID as value of cookie named JSESSIONID Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 75
  • 76. Cookies Cookie-enabled browser returns session ID as value of cookie named JSESSIONID Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 76
  • 77. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 77
  • 78. Cookies Cookies are expired by client (server can request expiration date) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 78
  • 79. Cookies Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 79
  • 80. Cookies Return array of cookies contained in HTTP request Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 80
  • 81. Cookies Search for cookie named COUNT and extract value as an int Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 81
  • 82. Cookies Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 82
  • 83. Cookies Send replacement cookie value to client (overwrites existing cookie) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 83
  • 84. Cookies Should call addCookie() before writing HTML Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 84
  • 85. Cookies Privacy issues HTTP request to intended site Client HTTP response: HTML document including ad <img> Web site providing requested content HTTP request for ad image Image plus Set-Cookie in response: third-party cookie Web site providing banner ads Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 85
  • 86. Cookies Privacy issues HTTP request to 2nd intended site Client HTTP response: HTML document including ad <img> Web site providing requested content Second Web site providing requested content HTTP request for ad image plus Cookie (identifies user) Image Web site providing banner ads Based on Referer, I know two Web sites that this user has visited Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 86
  • 87. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 87
  • 88. URL Rewriting Tomcat adds session ID within HTML document to all URL’s referring to the servlet Session ID = 4235 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 88
  • 89. URL Rewriting Subsequent request will contain session ID in the URL of the request Session ID = 4235 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 89
  • 90. URL Rewriting Next response must again add session ID to all URL’s Session ID = 4235 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 90
  • 91. URL Rewriting Original (relative) URL: href=“URLEncodedGreeting” URL containing session ID: href=“URLEncodedGreeting;jsessionid=0157B9E85” Path parameter Path parameter is treated differently than query string parameter  Ex: invisible to getParameter() Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 91
  • 92. URL Rewriting Original servlet HttpServletResponse method encodeURL() will add session id path parameter to argument URL Relative URL of servlet Servlet using URL rewriting Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 92
  • 93. URL Rewriting Must rewrite every servlet URL in every document Security issues URL with session ID 7152 Web site using URL rewriting User A Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 93
  • 94. URL Rewriting Must rewrite every servlet URL in every document Security issues URL with session ID 7152 User A Web site using URL rewriting Email URL User B Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 94
  • 95. URL Rewriting Must rewrite every servlet URL in every document Security issues URL with session ID 7152 User A Web site using URL rewriting Email URL Visit Web site with session ID 7152 User B Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 95
  • 96. More Servlet Methods Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 96
  • 97. More Servlet Methods Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 97
  • 98. More Servlet Methods Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 98
  • 99. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 99
  • 100. More Servlet Methods Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 100
  • 101. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 101
  • 102. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 102
  • 103. Concurrency Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 103
  • 104. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 104
  • 105. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 105
  • 106. Concurrency // Output HTML document Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 106
  • 107. Concurrency Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 107
  • 108. Concurrency Java support thread synchronization Only one thread at at time can call doGet()  Only one synchronized method within a class can be called at any one time Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 108
  • 109. Concurrency Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 109
  • 110. Concurrency Web application with multiple servlet classes and shared resource: Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 110
  • 111. Concurrency Solution: create a shared class with synchronized static methods called by both servlets CounterReader readAndReset() CounterFile incr() CounterWriter File Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 111
  • 112. 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 Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 112
  • 113. 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) Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 113