113
Server-side Programming: Java Servlets CSI 3140 WWW Structures, Techniques and Standards

Server side

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Server side

Server-side Programming:Java Servlets

CSI 3140

WWW Structures, Techniques and Standards

Page 2: Server side

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

Page 3: Server side

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

Page 4: Server side

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

Servlet Overview

Page 5: Server side

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

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: Server side

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

Hello World! Servlet

Page 7: Server side

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

Hello World! ServletAll servlets we will writeare subclasses ofHttpServlet

Page 8: Server side

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

Hello World! Servlet

Server calls doGet() in response to GET request

Page 9: Server side

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

Hello World! Servlet

Interfaces implemented by request/response objects

Page 10: Server side

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

Hello World! Servlet

Production servlet shouldcatch these exceptions

Page 11: Server side

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

Page 12: Server side

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

Hello World! Servlet

First twothings doneby typical servlet;must be in thisorder

Page 13: Server side

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

Hello World! Servlet

Page 14: Server side

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

Hello World! Servlet

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

Page 15: Server side

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

Hello World! Servlet

Good practice to explicitly closethe PrintWriter when done

Page 16: Server side

Guy-Vincent Jourdan :: CSI 3140 :: based on Jeffrey C. Jackson’s slides 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 serverPrimary servlet output is typically HTML

Page 17: Server side

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

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: Server side

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

Dynamic Content

Page 19: Server side

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

Dynamic Content

Page 20: Server side

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

Dynamic Content

Page 21: Server side

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

Dynamic Content

Potential problems: Assuming one instance of servlet on one server,

butMany Web sites are distributed over multiple serversEven 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 properlyWe’ll deal with this later in the chapter

Page 22: Server side

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

Page 23: Server side

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

Servlet Life CycleExample life cycle method:attempt to initialize visits variablefrom file

Page 24: Server side

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

Servlet Life Cycle

Exception to be thrownif initialization fails and servletshould not be instantiated

Page 25: Server side

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

Parameter Data

The request object (which implements HttpServletRequest) provides information from the HTTP request to the servletOne type of information is parameter data,

which is information from the query string portion of the HTTP request

Query string withone parameter

Page 26: Server side

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

Parameter Data

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

Query string syntax and semantics

Page 27: Server side

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

Page 28: Server side

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

Parameter Data

Parameter names and values can be any 8-bit charactersURL 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: Server side

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

Parameter Data

URL encoding algorithm

Page 30: Server side

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

Parameter Data

Page 31: Server side

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

Parameter Data

Page 32: Server side

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

Parameter Data

Page 33: Server side

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

Parameter DataMust escape XML special characters inall user-supplied data before adding to HTMLto avoid cross-site scripting attacks

Page 34: Server side

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

Parameter Data

Cross-site scripting

Attacker

Blogging Website

Comment containing<script> element

Document containingattacker’s comment (and script)Victim

Page 35: Server side

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

Parameter Data

Also need to escape quotes withinattribute values.

Page 36: Server side

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

Parameter Data

Page 37: Server side

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

Page 38: Server side

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

Parameter Data

Page 39: Server side

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

Parameter Data

username

lifestory

boxgroup1 (values same as labels)doit

Page 40: Server side

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

Parameter Data

Query string produced by browser (all one line):

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

Page 41: Server side

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

Parameter Data

GET vs. POST method for forms: GET:

Query string is part of URLLength of query string may be limitedRecommended 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

Page 42: Server side

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

Parameter Data

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

Page 43: Server side

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

Parameter Data

GET vs. POST method for forms: POST:

Query string is sent as body of HTTP requestLength of query string is unlimitedRecommended if parameter data is intended to cause

the server to update stored dataMost browsers will warn you if they are about to

resubmit POST data to avoid duplicate updates

Page 44: Server side

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

Parameter Data

Page 45: Server side

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

Page 46: Server side

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

Page 47: Server side

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

Sessions

Page 48: Server side

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

Sessions

Server sends backnew unique session ID when the request hasnone

Page 49: Server side

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

Sessions

Client that supportssession stores theID and sends itback to the serverin subsequentrequests

Page 50: Server side

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

Sessions

Server knowsthat all of theserequests arefrom the sameclient. Theset of requestsis known as asession.

Page 51: Server side

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

Sessions

And the serverknows that allof theserequests arefrom a differentclient.

Page 52: Server side

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

Sessions

Returns HttpSession object associatedwith 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

Page 53: Server side

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

Sessions

Boolean indicating whether returnedobject was newly created or alreadyexisted.

Page 54: Server side

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

Sessions

Incremented once per session

Page 55: Server side

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

Sessions

Three webpages producedby a single servlet

Page 56: Server side

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

Sessions

Page 57: Server side

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

Sessions

,,,

Page 58: Server side

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

Sessions

,,, Session attribute is aname/value pair

Page 59: Server side

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

Sessions

,,,

Session attribute willhave null value untila value is assigned

Page 60: Server side

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

Sessions

,,,

Generatesign-in formif session isnew orsignIn attribute has no value,weclome-back pageotherwise.

Page 61: Server side

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

Sessions

Sign-in form

Welcome-backpage

Page 62: Server side

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

Sessions

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

Page 63: Server side

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

Sessions

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

Page 64: Server side

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

Sessions

Text field containinguser name is namedsignIn

Page 65: Server side

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

Sessions

Page 66: Server side

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

Sessions

…RetrievesignInparameter value

Page 67: Server side

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

Sessions

Normalprocessing:signInparameteris present inHTTP request

Page 68: Server side

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

Sessions

GenerateHTML forresponse

Page 69: Server side

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

Sessions

Thank-you page Must escape XML specialcharacters inuser input

Page 70: Server side

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

Sessions

Assign avalue to thesignIn sessionattribute

Page 71: Server side

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

Page 72: Server side

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

Sessions

Errorprocessing(return userto sign-in form)

Page 73: Server side

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

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 objectServlet code can: Terminate a session by calling invalidate()

method on session object Set the expiration time-out duration (secs) by

calling setMaxInactiveInterval(int)

Page 74: Server side

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

Cookies

A cookie is a name/value pair in the Set-Cookie header field of an HTTP responseMost (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 75: Server side

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

Cookies

Tomcat sendssession ID as valueof cookie namedJSESSIONID

Page 76: Server side

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

Cookies

Cookie-enabledbrowser returnssession ID as valueof cookie namedJSESSIONID

Page 77: Server side

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

Page 78: Server side

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

Cookies

Cookies are expired byclient (server can requestexpiration date)

Page 79: Server side

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

Cookies

Page 80: Server side

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

Cookies

Return array of cookiescontained in HTTP request

Page 81: Server side

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

Cookies

Search forcookienamedCOUNT andextract valueas an int

Page 82: Server side

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

Cookies

Page 83: Server side

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

Cookies

Sendreplacementcookie valueto client(overwritesexisting cookie)

Page 84: Server side

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

Cookies

Should calladdCookie()before writingHTML

Page 85: Server side

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

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 86: Server side

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

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 87: Server side

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

Cookies Privacy issues

Due to privacy concerns, many users block cookies

Blocking may be fine-tuned. Ex: Mozilla allows Blocking of third-party cookiesBlocking based on on-line privacy policy

Alternative to cookies for maintaining session: URL rewriting

Page 88: Server side

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

URL Rewriting

Tomcat addssession ID withinHTML documentto all URL’s referring to the servlet Session ID = 4235

Page 89: Server side

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

URL Rewriting

Subsequentrequest will containsession ID in theURL of the request

Session ID = 4235

Page 90: Server side

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

URL Rewriting

Next response mustagain add session IDto all URL’s Session ID = 4235

Page 91: Server side

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

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 92: Server side

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

URL Rewriting

HttpServletResponse method encodeURL() will add session id path parameter to argument URL

Relative URL of servlet

Originalservlet

Servletusing URLrewriting

Page 93: Server side

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

URL Rewriting

Must rewrite every servlet URL in every documentSecurity issues

Web site usingURL rewriting

User A

URL withsession ID7152

Page 94: Server side

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

URL Rewriting

Must rewrite every servlet URL in every documentSecurity issues

Web site usingURL rewriting

User A User BEmail URL

URL withsession ID7152

Page 95: Server side

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

URL Rewriting

Must rewrite every servlet URL in every documentSecurity issues

Web site usingURL rewriting

User A

URL withsession ID7152

User BEmail URL

Visit Web site withsession ID 7152

Page 96: Server side

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

More Servlet Methods

Page 97: Server side

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

More Servlet Methods

Page 98: Server side

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

More Servlet Methods

Page 99: Server side

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

Page 100: Server side

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

More Servlet Methods

Page 101: Server side

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

Page 102: Server side

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

Page 103: Server side

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

Concurrency

Page 104: Server side

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

Concurrency

Tomcat creates a separate thread for each HTTP requestJava 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 105: Server side

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

Page 106: Server side

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

Concurrency

// Output HTML document

Page 107: Server side

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

Concurrency

Page 108: Server side

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

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 109: Server side

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

Concurrency

Page 110: Server side

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

Concurrency

Web application with multiple servlet classes and shared resource:

Page 111: Server side

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

Concurrency

Solution: create a shared class with synchronized static methods called by both servlets

CounterFileCounterReader CounterWriterreadAndReset() incr()

File

Page 112: Server side

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

Common Gateway Interface

CGI was the earliest standard technology used for dynamic server-side contentCGI 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 113: Server side

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