06-synch-and-jsps

indexadjustmentInternet and Web Development

Nov 13, 2013 (3 years and 11 months ago)

72 views

May 5, 2008

Sprenkle
-

CS297

1

Objectives


Servlets Review


Synchronization


Version Control


JSPs


Web Application Organization

May 5, 2008

Sprenkle
-

CS297

2

Servlets Review


How do we get a Servlet’s init parameter?


Why do we use init parameters?


Where are init parameters stored?


How can we save state across multiple
requests from a user?


What are the pros and cons of each?

May 5, 2008

Sprenkle
-

CS297

3

Multiple Clients


Web Server executes multiple threads


Handles multiple requests at a time


Original

SurveyServlet

stores the results
of the survey in a file on the server


When >1 client connects to the server at
one time, server handles both clients
concurrently


>1 can execute
SurveyServlet


>1 thread can read/modify file at one time


Can lead to inconsistent data!

May 5, 2008

Sprenkle
-

CS297

4



SurveyServlet?animal=dog

SurveyServlet?animal=cat

Web Application
Server

file

// read file

// update local array

// write file

// print results

SurveyServlet Implementation


Operations can overlap

// read file

// update local array

// write file

// print results

May 5, 2008

Sprenkle
-

CS297

5



SurveyServlet?animal=dog

SurveyServlet?animal=cat

Web Application
Server

file

// read file

// update local array

// read file

// update local array

// write file

// print results

// write file

// print results

Bad Interleaving


Loses yellow’s vote

May 5, 2008

Sprenkle
-

CS297

6

Critical Section


Sections of code that have to happen
uninterrupted or
atomically


Only one thread can execute at a time


What is the critical section in this code?

// read file

// update local array

// write file

// print results

May 5, 2008

Sprenkle
-

CS297

7

Critical Section


Sections of code that have to happen
uninterrupted or
atomically


Only one thread can execute at a time


What is the critical section in this code?


The shared file must be read and written
atomically


Writes

cause trouble

// read file

// update local array

// write file

// print results

May 5, 2008

Sprenkle
-

CS297

8

210 in 297


Even if only one statement in critical section,
synchronize it!


One high
-
level Programming Language
statement probably translates into multiple
VM language statements


Prevent interruption at low level

count++;

Retrieve count

Add 1 to count

Store count

High
-
level:

Virtual Machine level:

May 5, 2008

Sprenkle
-

CS297

9

Synchronization Mechanisms


Synchronized classes


Synchronized methods


Synchronized statements



Expense associated with each of these


But without it, get wrong or inconsistent
answers!

May 5, 2008

Sprenkle
-

CS297

10

Synchronized Classes


java.util.concurrent


Use when have independent data structure
instance variables


May 5, 2008

Sprenkle
-

CS297

11

Synchronized Methods


Add
synchronized

keyword to the method
declaration


Can’t synchronize constructor (doesn’t make sense)


When a thread calls a
synchronized

method of an
object, that
object

becomes
locked


Exactly one shared key for an object


You must have key to enter a synchronized method of
that object


With key, unlock the door to synchronized method you
want to enter


When another thread attempts to enter a synchronized
method, it cannot get the key, so it blocks


Other thread waits for the key

May 5, 2008

Sprenkle
-

CS297

12

Synchronized Methods


Add
synchronized

keyword to the method
declaration


Can’t synchronize constructor (doesn’t make sense)


Example:






Unsynchronized methods can still be called while
object is locked

public
synchronized

void methodName() {




}

May 5, 2008

Sprenkle
-

CS297

13

Synchronized Statements


Every Java
Object

has an implicit lock or
monitor



wait, notify, notifyAll

methods


Synchronize a block of code on an
Object



synchronized

(this) {




}

synchronized

(object) {




}

If
this

doesn’t need to be
synchronized because
object

is independent of
other data in
this


Finer granularity than
methods

May 5, 2008

Sprenkle
-

CS297

14

Examples of Dependence


Dependent Example


ESPN’s update of baseball scores


When final score comes in, both teams records
and standings need to be updated together


Otherwise: inconsistent


Independent Examples


ESPN’s update of horse racing and baseball
scores


May 5, 2008

Sprenkle
-

CS297

15

General Rules


Need to synchronize access to shared
resources


Instance variables, Files


Sessions


Don’t need to worry about local variables


Want to limit size of critical section


Larger section reduces amount of concurrency


Programmer must be very careful not to write
programs in which deadlock can occur


Careful synchronization: keep it simple

May 5, 2008

Sprenkle
-

CS297

16

Synchronizing
SurveyServlet


Identify the shared variables


Identify when shared variables are written,
when they are read


Identify the critical section


How would you synchronize?

May 5, 2008

Sprenkle
-

CS297

17

Aside:
SurveyServlet


Should periodically write the survey results
but not hold up requests


Separate thread to write results

May 5, 2008

Sprenkle
-

CS297

18

Synchronization


Important but difficult issue


We’ll use a database to handle a lot of the
synchronized state issues

May 5, 2008

Sprenkle
-

CS297

19

Backups and Coordination Issues


Maybe you wrote a paper and had several
versions


Good practice to iterate over it!


Keep track of versions using names, e.g.,
paper1.pdf, paper.draft.pdf, paper.outline.pdf,
paper.mar7.pdf


Coordinate a group


One person’s account has
the

version


Make conflicting changes to files


Figure out fix, Merge files

May 5, 2008

Sprenkle
-

CS297

20

File Sharing Problems

From Subversion Book

May 5, 2008

Sprenkle
-

CS297

21

Copy
-
Modify
-
Merge Solution


CVS and Subversion’s solution

From Subversion Book

May 5, 2008

Sprenkle
-

CS297

22

Copy
-
Modify
-
Merge Solution


CVS and Subversion’s solution

From Subversion Book

May 5, 2008

Sprenkle
-

CS297

23

Version Control


Backup and Restore


Files are saved as they are edited


Revert to a specific version/checkpoint


Synchronization


Lets people share files


Stay up
-
to
-
date with the latest version


Track changes to code


Save comments explaining why change happened


Stored in the VCS, not the file


Track how, why a file evolves over time


Track Ownership


Tags every change with the name of the person who
made it

May 5, 2008

Sprenkle
-

CS297

24

Version Control


Short
-
term undo


Messed up a file? Go back to the last
good

version


Long
-
term undo


Created a bug a year ago? Jump back to see change you made.


Sandboxing


Making a big change? Make temporary changes in isolated area,
test, work out kinks before “checking in” your changes


Branching and merging


Branch a copy of your code into a separate area, modify it in
isolation (tracking changes separately)


Later, merge work into common area.

May 5, 2008

Sprenkle
-

CS297

25

Using Version Control

Repository


Keeps public copy of code:
versions of all files,
comments about changes,
who made changes


Have “Working Copies”,
own copy of code


Checkout, commit,
update code

Users


We’re using Subversion,
through Eclipse


Similar to CVS

Code

Code

May 5, 2008

Sprenkle
-

CS297

26

Using Version Control:
checkout


To start, need to
checkout

your working
copy of the code

Repository

checkout

Code

Code

May 5, 2008

Sprenkle
-

CS297

27

Using Version Control:
commit


After you make changes that you want others
to see,
commit

your version

Repository


Checks for conflicts
--

code
conflicts with recent
changes in the public copy

commit

Code*

conflicts

Code


Update code, fix
conflicts


Try commit again

May 5, 2008

Sprenkle
-

CS297

28

Using Version Control:
commit


After you make changes that you want others
to see,
commit

your version


Include comments about what changes you
made and why

Repository


Checks for conflicts


Updates each modified file


Records comments with
updated files

commit

Code*

comments?

comments

Code

May 5, 2008

Sprenkle
-

CS297

29

Using Version Control:
commit


After you make changes that you want others
to see,
commit

your version


Include comments about what changes you
made and why

Repository


Checks for conflicts


Updates each modified file


Records comments with
updated files

commit

Code*

comments?

comments

Code

Code’

Other people’s code
doesn’t change

May 5, 2008

Sprenkle
-

CS297

30

Using Version Control:
update


To see the
current

version of the code,
update

your repository


Resolve conflicts

Repository

update

Code

code

May 5, 2008

Sprenkle
-

CS297

31

Using Version Control:
add, delete


You need to
add

and
delete

files and
directories to the repository, then
commit

Repository

commit

Code


Add, delete files
and directories


Commit


Create new records for added files


Close records for deleted files



Files not deleted from repository

May 5, 2008

Sprenkle
-

CS297

32

Version Control Advice


Does not eliminate need for communication


Process becomes much more difficult if
developers do not communicate


Before picking up again,
update

your
working copy


Commit

only after you’ve tested code and
you’re fairly sure it works


Write descriptive comments so your team
members know what you did and why

May 5, 2008

Sprenkle
-

CS297

33

Subclipse


You’ll use Subclipse (Subversion plugin for
Eclipse) to manage your working copy and
the public copy


I’ll checkout the public copy, create WAR file,
deploy on servo


Set up is part of Lab 5

May 5, 2008

Sprenkle
-

CS297

34

Java
-
based Web Technology


Key Java
-
based Web technologies:

Moving to here

May 5, 2008

Sprenkle
-

CS297

35

Motivation: JavaServer Pages (JSPs)


Simplify web application development


Separate UI from backend code


Difficult to write HTML in print statements

May 5, 2008

Sprenkle
-

CS297

36

JavaServer Pages (JSPs)


Merge HTML and Java


Separate static HTML from dynamic


Make HTML templates, fill in dynamic content


Encourages separation of tasks


Web application server compiles JSPs into
Servlet code


Clean and efficient


Easier to develop, deploy, modify scripted
pages


How much trouble did you have with HTML in
Strings?

May 5, 2008

Sprenkle
-

CS297

37

JSP Syntax


Enclose code in
<% %>








<
html
>

<
body
>

Hello! The time is now
<%=

new

java.util.Date()
%>

</
body
>

</
html
>

Expression

Evaluated, turned
into a String

May 5, 2008

Sprenkle
-

CS297

38

JSP Syntax


<
html
>

<
body
>

<%


// This is a scriptlet. Notice that the "date"


// variable we declare here is available in the


// embedded expression later on.


java.util.Date date =
new

java.util.Date();

%>

Hello! The time is now
<%=

date
%>

</
body
>

</
html
>

May 5, 2008

Sprenkle
-

CS297

39

Example


SurveyServlet as a JSP


<%


for( ) {



}

%>

Hello! The time is now
<%=

date
%>

</
body
>

</
html
>

May 5, 2008

Sprenkle
-

CS297

40

JSP Directives


Page Directive


Java files to import (like import statement in Java)


<%@
page
import
=
"java.util.*,java.text.*"
%>


<%@
page
import
=
”logic.Quiz"
%>



Include Directive


Include contents of another file: JSP, HTML, or text


Example: include site’s common headers or footers


<%@
include
file
=
”header.jsp"
%>

May 5, 2008

Sprenkle
-

CS297

41

JSP Declarations


For instance variables and methods









We won’t do too much of this


Let servlets do the work

<%!


private ArrayList users;



public void jspInit() {



// on start up: set up


}


public void jspDestroy() {



// on shut down: clean up


}

%>

May 5, 2008

Sprenkle
-

CS297

42

JSP Variables


By default, JSPs have some variables


not explicitly declared in the file


HttpServletRequest
request



HttpServletResponse
response


HttpSession
session


JSPs can access request parameters,
session data

These names
must be used

May 5, 2008

Sprenkle
-

CS297

43

Web Application Architecture

JSP

Java Servlets

Java Classes

(Model)

Client

Server
-
side

DataStore

HTML With
Dynamic Pieces


Heavy lifting of requests


Forward to JSPs

May 5, 2008

Sprenkle
-

CS297

44

Communicating Between JSPs and Servlets


Attributes


Name/Value pairs


Values are
Objects


Can get/set attributes on the
HttpServletRequest

object


Parameters


Names and Values are
Strings


From forms or in URLs


Also in the
HttpServletRequest

object

May 5, 2008

Sprenkle
-

CS297

45

Login Example

login.jsp

welcome.jsp

LoginServlet

Client

Server
-
side


Check user name/password


Set
authenticated
or

error

attribute


Forward to welcome.jsp or login.jsp


First time?


Error
message


Check
authenticated


User’s Options

May 5, 2008

Sprenkle
-

CS297

46

Forwarding Requests


HttpServletRequest
’s
getRequestDispatcher

method


Returns a
RequestDispatcher

object







Can use
RequestDispatcher
’s
include

method similarly


request.getRequestDispatcher(
”mainmenu.jsp”
).


forward(request, response);

The name of the
resource to forward to

May 5, 2008

Sprenkle
-

CS297

47

Adding a JSP to
SurveyServlet


Separate heavy lifting from the HTML


Think of JSP as a template


What is static about the response page?


What is dynamic?


Servlet will handle most of the work

May 5, 2008

Sprenkle
-

CS297

48

TODO


Requirements: Due at Midnight


Static HTML Mockup


Demo to Professor Gregory next Monday


Lab 5
-

Subversion and JSPs