Servlet vs JSP
content while Servlets are Java programs that are already compiled
which also creates dynamic web content. Servlets run faster compared to JSP
Despite the ease of separating the real code into separate classes, beans, and
custom tags, the assumption behind JSP is that a single page gives a single basic look
JSP only
Output is mostly character data. E.g.: HTML
Format/layout mostly fixed.
Combination
A single request will result in multiple substantially different looking results. Complicated data processing, but relatively fixed layout.
Design Patterns
History
A framework pattern for reusable applications. Depends on the Observer pattern. First developed by Xerox PARC for Smalltalk-80. Used by the Application Kit system in NeXTstep. Used by the Cocoa APIs for Apples OS X. Recommended structural framework pattern in J2EE.
Design Patterns
The hard problem in O-O programming is deciding what objects to have, and what their responsibilities are Design Patterns describe the higher-level organization of solutions to
common problems
Design patterns are a major topic in O-O design
10
Observable
An Observable is an object that can be observed An Observer is notified when an object that it is observing announces a change
When an Observable wants the world to know about what it has done, it executes:
setChanged(); notifyObservers(); /* or */ notifyObservers(arg);
The arg can be any object
The Observable doesnt know or care who is looking But you have attach an Observer to the Observable with:
myObservable.addObserver(myObserver); This is best done in the controller class not in the model class!
11
announces a change
Heres an analogy:
An Observable is like a Button
Another analogy:
12
Observer
Observer is an interface An Observer implements public void update(Observable obs, Object arg) This method is invoked whenever an Observable that it is listening to does an addNotify() or addNotify(arg) The obs argument is a reference to the observable object itself If the Observable did addNotify(), the arg is null
13
Observer Pattern
one object changes state, all its dependents are notified and
updated automatically. Used to decouple the subject from the observer, since the subject
15
MVC
Model
Encapsulates data.
Exports procedures that perform specific application processing.
Used by Controller on behalf of user.
Model
Responsibilities include:
Providing functional core. Register dependent views and controllers. Notify dependent components about changes.
View
Controller
Registers itself with Model if it depends on state of the Model in any way.
E.g. A certain change in the Model results in a menu item being enabled or
disabled.
21
Separation of concerns
As always, you want code independence The Model should not be contaminated with control code or display code The View should represent the Model as it really is, not some remembered status The Controller should talk to the Model and View, not manipulate them
The Controller can set variables that the Model and View can read
22
Advantages
Separation between the data layer and the interface is the key:
The view is easily replaced or expanded. Model data changes are reflected in all interfaces because all views are Observers. Better scalability since UI and application logic are separated. Distribution over a network is greatly simplified.
Key points
A Model does the business logic
It should be I/O free
The Controller organizes the program and provides input (control) to the Model The View displays what is going on in the model
It should never display what should be going on in the model For example, if you ask to save a file, the View shouldnt itself tell you that the file has been savedit should tell you what the model reports.
24
MVC2 Architecture
MVC Frameworks
Adobe Flex
Seam
Spring MVC, which is a part of the Spring Framework Stripes
Struts 2
Tapestry Wicket
JSF
Pros:
Cons:
Tag soup for JSPs Doesn't play well with REST or Security
Spring MVC
Pros:
Stripes
Pros:
Cons:
Small Community Not as actively developed as other projects
Struts 2
Pros: