Developing Java Web Applications 120276364738927 5

download Developing Java Web Applications 120276364738927 5

of 45

Transcript of Developing Java Web Applications 120276364738927 5

  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    1/45

    This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

    To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/

    Developing Java Web

    Applications

    Dr. Harry Chen

    CMSC 491S/691S

    February 11, 2008

    http://harry.hchen1.com/http://socailwebtechnologies.blogspot.com/http://socailwebtechnologies.blogspot.com/http://harry.hchen1.com/http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    2/45

    Agenda

    Web application architecture

    Java Servlet technology

    WebWork: building webapps made easySpring: Inversion of Control (IoC)

    Maven: Java project management and

    build tool

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    3/45

    A canonical Web architecture

    Do you see any technical issues in this architecture?

    Source: http://www.ibm.com/developerworks/ibm/library/it-booch_web/

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    4/45

    Stateless communication

    Communications

    between your browser

    and the Web Server

    is usually stateless Cookie was

    invented to solve this

    problem.

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    5/45

    Thin client vs. Thick (fat) client

    Where do you place

    your business logic?

    Thin client: most of

    the business logic liveon the client side

    Thick client:

    application states are

    managed by the

    serverThin clients live here.

    Thick clients live here.

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    6/45

    How do we create View?

    Raw data must beprocessed before itspresented to the user.

    Users see the View(HTML, PDF, RSSetc.)

    Should you hardwire

    code in the businesslogic to generateViews?

    Raw dataView (HTML, PDF, etc)

    How will be the Views be generated?

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    7/45

    Overhead in access data

    A major part of theapplicationimplementationconsists of accessingand updating the rawdata which can becostly.

    Reduce this overheadcan speed up yourdevelopment (i.e.,save cost)

    SQL DBHTML

    Do I have to call

    SELECT,

    INSERT,

    UPDATE all the

    time?

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    8/45

    Framework

    Software engineering is like civil

    engineering: we need framework.

    Option 2: Use framework Your Goal

    Option1: Build everything from scratch

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    9/45

    http://en.wikipedia.org/wiki/OnTaphttp://en.wikipedia.org/wiki/Model-Gluehttp://en.wikipedia.org/wiki/Model-Gluehttp://en.wikipedia.org/wiki/Model-Gluehttp://en.wikipedia.org/wiki/Mach-IIhttp://en.wikipedia.org/wiki/Mach-IIhttp://en.wikipedia.org/wiki/Mach-IIhttp://en.wikipedia.org/wiki/Fusebox_%28programming%29http://en.wikipedia.org/wiki/ColdSpringhttp://en.wikipedia.org/wiki/ColdFusion_on_Wheelshttp://en.wikipedia.org/w/index.php?title=ColdBox&action=edithttp://en.wikipedia.org/wiki/Symfonyhttp://en.wikipedia.org/wiki/Simplicity_PHP_frameworkhttp://en.wikipedia.org/wiki/Seagull_PHP_Frameworkhttp://en.wikipedia.org/wiki/QPHP_Frameworkhttp://en.wikipedia.org/wiki/Qcodohttp://en.wikipedia.org/wiki/PRADOhttp://en.wikipedia.org/wiki/PostNukehttp://en.wikipedia.org/wiki/PHPOpenbizhttp://en.wikipedia.org/wiki/PHP_For_Applicationshttp://en.wikipedia.org/wiki/MODxhttp://en.wikipedia.org/wiki/KohanaPHPhttp://en.wikipedia.org/wiki/Joomla%21http://en.wikipedia.org/wiki/Horde_%28software%29http://en.wikipedia.org/wiki/Fuse_Frameworkhttp://en.wikipedia.org/wiki/Epesihttp://en.wikipedia.org/wiki/Drupalhttp://en.wikipedia.org/wiki/DIY_Frameworkhttp://en.wikipedia.org/wiki/CodeIgniterhttp://en.wikipedia.org/wiki/Canvas_Frameworkhttp://en.wikipedia.org/wiki/CakePHPhttp://en.wikipedia.org/wiki/Antares_Frameworkhttp://en.wikipedia.org/wiki/Akelos_PHP_Frameworkhttp://en.wikipedia.org/wiki/Mason_%28Perl%29http://en.wikipedia.org/wiki/Maypole_frameworkhttp://en.wikipedia.org/wiki/Interchange_%28software%29http://en.wikipedia.org/wiki/Catalyst_%28software%29http://en.wikipedia.org/wiki/ZTemplateshttp://en.wikipedia.org/wiki/ZK_Frameworkhttp://en.wikipedia.org/wiki/Wicket_frameworkhttp://en.wikipedia.org/wiki/WebWorkhttp://en.wikipedia.org/wiki/WebObjectshttp://en.wikipedia.org/wiki/ThinWirehttp://en.wikipedia.org/wiki/Tapestry_%28programming%29http://en.wikipedia.org/wiki/Stripes_%28framework%29http://en.wikipedia.org/wiki/Spring_Frameworkhttp://en.wikipedia.org/wiki/SmartClienthttp://en.wikipedia.org/wiki/Shale_Framework_%28software%29http://en.wikipedia.org/wiki/RIFEhttp://en.wikipedia.org/wiki/Reasonable_Server_Faceshttp://en.wikipedia.org/wiki/Oracle_ADFhttp://en.wikipedia.org/wiki/OpenXavahttp://en.wikipedia.org/wiki/OpenLaszlohttp://en.wikipedia.org/wiki/Makumba_%28framework%29http://en.wikipedia.org/wiki/JBoss_Seamhttp://en.wikipedia.org/wiki/JavaServer_Faceshttp://en.wikipedia.org/wiki/ItsNathttp://en.wikipedia.org/wiki/IT_Mill_Toolkithttp://en.wikipedia.org/wiki/ICEfaceshttp://en.wikipedia.org/wiki/Hamletshttp://en.wikipedia.org/wiki/Grails_%28Framework%29http://en.wikipedia.org/wiki/Google_Web_Toolkithttp://en.wikipedia.org/wiki/Flexivehttp://en.wikipedia.org/wiki/Clickhttp://en.wikipedia.org/wiki/Aranea_frameworkhttp://en.wikipedia.org/wiki/AppFusehttp://en.wikipedia.org/wiki/Apache_Strutshttp://en.wikipedia.org/wiki/Apache_Cocoonhttp://en.wikipedia.org/wiki/ASP.NET_MVC_Frameworkhttp://en.wikipedia.org/wiki/Component-based_Scalable_Logical_Architecturehttp://en.wikipedia.org/wiki/DotNetNukehttp://en.wikipedia.org/wiki/Monorail_%28.Net%29http://en.wikipedia.org/wiki/Yahoo%21_UI_Libraryhttp://en.wikipedia.org/wiki/Spry_frameworkhttp://en.wikipedia.org/wiki/SmartClienthttp://en.wikipedia.org/wiki/Script.aculo.ushttp://en.wikipedia.org/wiki/Rico_%28Ajax%29http://en.wikipedia.org/wiki/Rialto_Toolkithttp://en.wikipedia.org/wiki/Qooxdoohttp://en.wikipedia.org/wiki/Prototype_JavaScript_Frameworkhttp://en.wikipedia.org/wiki/OpenLink_AJAX_Toolkithttp://en.wikipedia.org/wiki/MooToolshttp://en.wikipedia.org/wiki/Mochikithttp://en.wikipedia.org/wiki/Microsoft_AJAX_Libraryhttp://en.wikipedia.org/wiki/JQueryhttp://en.wikipedia.org/wiki/Ext_%28javascript_library%29http://en.wikipedia.org/wiki/Echo_%28framework%29http://en.wikipedia.org/wiki/Dojo_Toolkithttp://en.wikipedia.org/wiki/Clean_AJAXhttp://en.wikipedia.org/wiki/Backbasehttp://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    10/45

    Java Servlet technology

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    11/45

    Java Servlet technology

    Servlets are Java code that run in a server

    application (e.g., Tomcat) and answer

    client request.

    Source: http://www.informit.com/articles/article.aspx?p=26920

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    12/45

    HelloWorld Servlet

    Set HTTP Header: Content-Type (HTML)

    Creates the HTML page content

    Implementation that handles HTTP GET request

    Outputs the HTML into the HTTP Response

    Do you see any technical issues with this implementation?

    Source: http://www.informit.com/articles/article.aspx?p=26920

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    13/45

    JSP (Java Servlet Pages)

    Technology that allows Java code and

    certain pre-defined actions to be

    embedded into static content.

    Java Servlet:

    Write codecompiledeploy

    JSP

    Write codedeploy

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    14/45

    HelloWorld JSP

    Source: http://mainline.brynmawr.edu/~dkumar/JSP/

    Use Java as if its a scripting language

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    15/45

    Open issues in Java Servlet technology

    No mention of any framework or design

    pattern for building web applications

    How to handle stateless communication?

    Use Thin or Thick client?

    How to access data?

    How to create Views?

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    16/45

  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    17/45

    Example: J2EE webapp

    This is what you want to build.

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    18/45

    Example: J2EE webapp (cont.)

    This is an MVC solution

    All DB access and update

    operations are implemented here.

    All business logic and applications

    states are stored here.

    HTML, PDF and whatever goes here.

    They read and parse data object from

    the Model

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    19/45

    Problems with MVC

    MVC is only a design patterni.e., you

    still have to write code to follow this

    specification

    An obvious problem is that everyone has

    to learn and write their own MVC

    framework

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    20/45

    WebWork

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    21/45

    WebWork

    A Java MVC framework that help

    developers to minimize code and to be

    more concentrated on business logic and

    modeling.

    Less plumbing code, more useful code.

    http://www.opensymphony.com/webwork/

    http://www.amazon.com/exec/obidos/tg/detail/-/1932394532/qid=1128052468/sr=8-1/ref=pd_bbs_1/104-0806192-0414332?v=glance&s=books&n=507846http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    22/45

    WebWork terminology

    Action: a piece of code that is executed when a

    URL is visited

    Bean: a data object that holds useful application

    information Views: templates pages for generating outputs

    for the clients to consume.

    Result: the output created by a template page

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    23/45

    WebWork workflow

    Read: http://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.html

    http://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.htmlhttp://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    24/45

    WebWork HelloWorld

    Goal: Go a URL, the webapp outputs

    Hello World!

    3 Steps

    Create an Action class to handle the request

    Create a View (template) to produce HTML

    Configure Action and View

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    25/45

    Action: HelloWorld.java

    Extends a standard Action

    superclass.

    Implement the business logic

    How to access the Bean (the

    message)

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    26/45

    View: helloworld.jsp

    Use JSP Tag lib to access our Bean

    (the message)

    If you dont like to use JSP, you have other options: Freemarkerand Velocity

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    27/45

    WebWork Configuration: HelloWorld

    Edit xwork.xml to glue the Action with the

    Template view.

    Define our Action and name it

    helloworld

    If the action returns success, then

    apply the hello.jsp template

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    28/45

    WebWork: HelloWorld Output

    Read: http://www.javaworld.com/javaworld/jw-10-2005/jw-1010-webwork.html

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    29/45

    Spring IoC

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    30/45

    Dynamic object instantiation

    Often your business logic implementation

    requires runtime configuration

    Customer Relationship App may use a different

    JDBC connection to access a new customer DB

    Dynamically fine tune how many threads the

    robot should instantiate for building search

    index DB

    Configure the username, password and DB url

    for your JDBC connection.

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    31/45

    Inversion of Control (IoC)

    IoC is a principle that encourage the

    decoupling of code.

    A.K.A. Dependency Injection.

    Problem: The use of MovieFinderImpl is hardwired into

    MovieLister. Change this logic will require code rewrite and

    re-compile MovieFinderImpl

    Read: http://martinfowler.com/articles/injection.html

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    32/45

    IoC Solution

    Introduce an intermediate component to

    handle the assembly of class objects.

    Solution: If you want to use a different MovieFinder, just

    change how Assembler calls .

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    33/45

    Spring IoC

    A framework that can be coupled with

    WebWork to provide Dependency

    Injection.

    Spring IoCimplements a

    flexible Assembler.

    Developers can tell this

    Assembler how to

    perform viaXML configuration.

    http://static.springframework.org/spring/docs/2.0.x/reference/beans.html

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    34/45

    Spring IoC: HelloWorld in gnizr

    How HelloWorld Action is instantiated in

    gnizrs HelloWorld demo

    http://code.google.com/p/gnizr/wiki/HelloWorldDemo

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    35/45

    Spring IoC: a more complex example

    (1) The Class object folderTagListener is dynamically associated with

    bookmarkManager via a configuration file, not hardwired in the source of

    bookmarkManager. (2) Developers also fine tune the number of WorkerThread to be

    instantiated by bookmarkManager in the same configuration file.

    (1)

    (2)

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    36/45

    Apache Maven

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    37/45

    Jave project management

    It concerns the management of

    library dependency,

    code building,

    unit testing,

    packaging and

    documentation.

    - Why do you think its

    important to consider those

    issues when building

    software?

    - Can you relate to what youhave experienced in the

    past (in school or at work)?

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    38/45

    Problems I have faced

    When some JAR library changed, I have tomanually download the JAR from the projectweb site.

    Make sure dependency library are included inmy CLASSPATH for building and testing

    Decide the directory layout of my project (whereis src, classes, resources).

    Figure out how to write documentation and calljavadoc in my project.

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    39/45

    Apache Maven

    Maven is an open source Java project

    management tool that simplifies many of

    the tedious tasks associated with Java

    programming.

    Features I love very much:

    Library dependency management

    Build, test, and package

    Pre-defined directory layout for webapp dev.

    http://maven.apache.org/

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    40/45

    POM file

    In Make, we have Makefile

    In Maven, we have POM file (pom.xml)

    In this POM file, you can define

    JAR (version and package names) that should to beincluded in your build CLASSPATH (dependencies).

    The layout of your project (where do you keep source,.class, other resources and configuration)

    Information for generating documentation

    And many other stuff

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    41/45

    Maven: Create a project

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    42/45

    Maven: POM file

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    43/45

    Maven: build package

    http://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    44/45

    Try Maven in 5 minutes

    http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

    http://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://maven.apache.org/guides/getting-started/maven-in-five-minutes.htmlhttp://www.umbc.edu/umbcstyle/images/verthires.jpg
  • 8/12/2019 Developing Java Web Applications 120276364738927 5

    45/45

    Summary

    Building a successful and maintainable Java

    Web application requires the effective use of

    software frameworks and development tools.

    WebWorkand Springare excellent frameworksfor building scalable and high-quality Java Web

    applications.

    Apache Mavensolves many project

    management issues that programmers havebeen struggling for a long time.

    http://www.umbc.edu/umbcstyle/images/verthires.jpg