2.mvc

Upload: parameswari-balasubramaniam

Post on 03-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 2.Mvc

    1/33

    Perspective: Servlet and JSP

  • 8/12/2019 2.Mvc

    2/33

    Servlet Life Cycle

  • 8/12/2019 2.Mvc

    3/33

    Life of a JSP file

    http://en.wikipedia.org/wiki/File:JSPLife.png
  • 8/12/2019 2.Mvc

    4/33

    Uses of JSP Constructs

    Scripting elements calling servlet code directly

    Scripting elements calling servlet code indirectly (by means of

    utility classes)

    Beans

    Servlet/JSP combo (MVC)

    MVC with JSP expression language

    Custom tags

  • 8/12/2019 2.Mvc

    5/33

    Servlet vs JSP

    JSP is a webpage scripting language that can generate dynamic

    content while Servlets are Java programs that are already compiled

    which also creates dynamic web content.

    Servlets run faster compared to JSP

    JSP can be compiled into Java Servlets

    Its easier to code in JSP than in Java

    JSP and Java Servlets are usually used in conjunction nowadays

  • 8/12/2019 2.Mvc

    6/33

    Why Combine Servlets & JSP?

    Typical picture: use JSP to make it easier to develop and

    maintain the HTML content

    For simple dynamic code, call servlet code from scripting elements

    For slightly more complex applications, use custom classes called from scriptingelements

    For moderately complex applications, use beans and custom tags

    But, that's not enough

    For complex processing, starting with JSP is awkward 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

    P ibili i f H dli

  • 8/12/2019 2.Mvc

    7/33

    Possibilities for Handling a

    Single Request

    Servlet only

    Output is a binary type. E.g.: an image

    No output. E.g.: you are doing forwarding or redirection as in Search Engine

    example.

    Format/layout of page is highly variable. E.g.: portal.

    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.

    These apply to a sing le request

    You still use both servlets and JSP within your overall application.

  • 8/12/2019 2.Mvc

    8/33

    Design PatternsModelViewController

  • 8/12/2019 2.Mvc

    9/33

    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.

  • 8/12/2019 2.Mvc

    10/33

    10

    Design Patterns

    The hard problem in O-O programming is deciding what objects to

    have, and what their responsibilities are

    Design Patternsdescribe the higher-level organization of solutions to

    common problems

    Design patterns are a major topic in O-O design

  • 8/12/2019 2.Mvc

    11/33

    11

    Observable

    An Observableis an object that can be observed

    An Observeris notified when an object that it is observingannounces a change

    When an Observablewants the world toknow about what it has done, it executes:

    setChanged(); notifyObservers(); /* or */ notifyObservers(arg);

    The ar gcan be any object

    The Observabledoesnt know or care who islooking

    But you have attach an Observerto theObservablewith: myObservable.addObserver(myObserver);

    This is best done in the controller classnot in the mod el class!

  • 8/12/2019 2.Mvc

    12/33

    12

    Observerand Observable

    java.utilprovides an Observerinterfaceand an Observableclass

    An Observableis an object that can be observed

    An Observeris notified when an object that it is observingannounces a change

    Heres an analogy:

    An Observableis like a Button

    An Observeris like a Listener

    You have to attach a Listenerto a Button

    Another analogy:

  • 8/12/2019 2.Mvc

    13/33

    13

    Observer

    Observeris an interface

    An Observerimplementspublic void update(Observable obs, Object arg)

    This method is invoked whenever an Observablethat it is listeningto does an addNotify()or addNotify(arg)

    The obsargument is a reference to the observable object itself

    If theObservabledidaddNotify(), thearg isnull

  • 8/12/2019 2.Mvc

    14/33

    Observer Pattern

    Defines a one-to-many dependency between objects so that when

    one object changes state, all its dependents are notified and

    updated automatically.

    Used to decouple the subject from the observer, since the subject

    needs little information to notify the observer.

    Can result in excessive notifications.

  • 8/12/2019 2.Mvc

    15/33

    15

    The MVC pattern

    MVCstands for Model-View-Controller

    The Modelis the actual internal representation

    The View(or a View) is a way of looking at or displaying the model

    The Controllerprovides for user input and modification

    These three components are usually implemented as separate

    classes

  • 8/12/2019 2.Mvc

    16/33

    MVC

    Divides interactive applications into three distinct components:

    ModelContains core functionality and data.

    ViewDisplays information to user.

    ControllerHandles user input.

    Includes a change-propagation mechanism.

    Ensures consistency between user interface and model.

  • 8/12/2019 2.Mvc

    17/33

    Model

    Encapsulates data.

    Exports procedures that perform specific application processing.

    Used by Controller on behalf of user.

    Provides functions to access Models data. Used by View, maybe Controller too.

  • 8/12/2019 2.Mvc

    18/33

    Model

    Manages change-propagation mechanism.

    Maintains registry of components dependent on the model.

    Views and controllers register their need to be informed about changes.

    Changes in state of model trigger notifications.

    Responsibilities include:

    Providing functional core.

    Register dependent views and controllers.

    Notify dependent components about changes.

  • 8/12/2019 2.Mvc

    19/33

    View

    Presents information to user.

    Different Views present information in different ways.

    Defines an update procedure that is activated by change-

    propagation mechanism in Model.

    Retrieves current data values to be displayed from Model.

    During initialization, associates itself with Model and registers for

    change notification.

    Corresponds to a single Controller.

    Offers functionality that allows Controller to manipulate display.

    E.g. scrolling.

  • 8/12/2019 2.Mvc

    20/33

    Controller

    Accepts user inputs as events.

    Translates events into requests for the associated Model or View.

    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.

  • 8/12/2019 2.Mvc

    21/33

    21

    Combining Controller and View

    Sometimes the Controller and View are combined, especially in

    small programs

    Combining the Controller and View is appropriate if they are

    very interdependent

    The Model should still be independent

    Nevermix Model code with GUI code!

  • 8/12/2019 2.Mvc

    22/33

    22

    Separation of concerns

    As always, you want code independence

    The Model should not be contaminated with control code ordisplay code

    The View should represent the Model as it really is, not someremembered status

    The Controller should talk tothe Model and View, notmanipulatethem

    The Controller can set variables that theModel and View can read

  • 8/12/2019 2.Mvc

    23/33

    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.

  • 8/12/2019 2.Mvc

    24/33

    24

    Key points

    A Model does the business logic

    It should be I/O free

    Communication with the Model is via methods

    This approach gives maximum flexibility in how the model is used

    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 shouldbe 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.

  • 8/12/2019 2.Mvc

    25/33

    MVC1 Architecture Page centric

  • 8/12/2019 2.Mvc

    26/33

    MVC2 Architecture

  • 8/12/2019 2.Mvc

    27/33

    Comparison of Web Frameworks

  • 8/12/2019 2.Mvc

    28/33

    MVC Frameworks

    Adobe Flex

    Grails, together with Groovy

    Google Web Toolkit (GWT), together with GWT-Ext

    JavaServer Faces (JSF), together with Facelets

    Seam

    Spring MVC, which is a part of the Spring Framework

    Stripes

    Struts 2

    Tapestry

    Wicket

  • 8/12/2019 2.Mvc

    29/33

    Pros and Cons

  • 8/12/2019 2.Mvc

    30/33

  • 8/12/2019 2.Mvc

    31/33

    Spring MVC

    Pros:

    Lifecyle for overriding binding, validation, etc.

    Integrates with many view options seamlessly:

    JSP/JSTL, Tiles, Velocity, FreeMarker, Excel, PDF

    Inversion of Control makes it easy to test

    Cons:

    Configuration intensive - lots of XML

    Almost too flexible - no common parent

    Controller

    No built-in Ajax support

  • 8/12/2019 2.Mvc

    32/33

    Stripes

    Pros:

    No XML - Convention over Configuration

    Good documentation (easy to learn)

    Enthusiastic community

    Cons:

    Small Community

    Not as actively developed as other projects

    Hard-coded URLs in ActionBeans

  • 8/12/2019 2.Mvc

    33/33

    Struts 2

    Pros:

    Simple architecture - easy to extend

    Tag Library is easy to customize with

    FreeMarker or Velocity

    Controller-based or page-based navigation

    Cons:

    Documentation is poorly organized

    No feedback for missing properties or invalid

    OGNL expressions

    Googling results in Struts 1.x documentation