Вы находитесь на странице: 1из 45

Developing Java Web Applications

Dr. Harry Chen CMSC 491S/691S February 11, 2008

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/

Agenda
Web application architecture Java Servlet technology WebWork: building webapps made easy Spring: Inversion of Control (IoC) Maven: Java project management and build tool

A canonical Web architecture

Do you see any technical issues in this architecture?

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

Stateless communication
Communications between your browser and the Web Server is usually stateless Cookie was invented to solve this problem.

Thin client vs. Thick (fat) client


Thick clients live here.

Thin clients live here.

Where do you place your business logic? Thin client: most of the business logic live on the client side Thick client: application states are managed by the server

How do we create View?


Raw data must be processed before its presented to the user. Users see the View (HTML, PDF, RSS etc.) Should you hardwire code in the business logic to generate Views?

View (HTML, PDF, etc)

Raw data

How will be the Views be generated?

Overhead in access data


Do I have to call SELECT, INSERT, UPDATE all the time?

HTML

SQL DB

A major part of the application implementation consists of accessing and updating the raw data which can be costly. Reduce this overhead can speed up your development (i.e., save cost)

Framework
Software engineering is like civil engineering: we need framework.
Option1: Build everything from scratch

Option 2: Use framework

Your Goal

Framework: Its a juggle out there.


http://en.wikipedia.org/wiki/List_of_web_application_frameworks
JavaScript PHP Backbase Clean AJAX Dojo Toolkit Echo Ext JQuery Microsoft AJAX Library Mochikit MooTools OpenLink AJAX Toolkit Prototype JavaScript Framework qooxdoo Rialto Toolkit Rico Script.aculo.us SmartClient Spry framework Yahoo! UI Library Akelos PHP Framework Antares Framework CakePHP Canvas Framework CodeIgniter DIY Framework Drupal epesi FUSE Horde Joomla! KohanaPHP MODx PHP For Applications PHPOpenbiz PostNuke PRADO Qcodo QPHP Framework Seagull PHP Framework Simplicity PHP framework Symfony JBoss Seam Makumba OpenLaszlo OpenXava Oracle ADF Reasonable Server Faces RIFE Shale Framework (software) SmartClient Spring Framework Stripes (framework) Tapestry ThinWire WebObjects WebWork Wicket framework ZK Framework ztemplates

Java

Java Apache Cocoon Apache Struts AppFuse Aranea framework Click [fleXive] Google Web Toolkit Grails Hamlets ICEfaces IT Mill Toolkit ItsNat JavaServer Faces

ColdFusion

Perl Catalyst Interchange Maypole Mason

ASP.NET MonoRail DotNetNuke CSLA ASP.NET MVC Framework

ColdBox ColdFusion on Wheels ColdSpring Fusebox Mach-II Model-Glue onTap

Java Servlet technology

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

HelloWorld Servlet
Implementation that handles HTTP GET request

Set HTTP Header: Content-Type (HTML)

Creates the HTML page content

Outputs the HTML into the HTTP Response

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

Do you see any technical issues with this implementation?

JSP (Java Servlet Pages)


Technology that allows Java code and certain pre-defined actions to be embedded into static content. Java Servlet:
Write code compile deploy

JSP
Write code deploy

HelloWorld JSP

Use Java as if its a scripting language

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

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?

MVC: Model-View-Control
Java BluePrints developed a recommended design pattern for building Java Servlet applications Goal: help you to design, implement and maintain your Java Web applications
http://java.sun.com/blueprints/patterns/MVC-detailed.html

Example: J2EE webapp


This is what you want to build.

Example: J2EE webapp (cont.)


This is an MVC solution
All DB access and update operations are implemented here.

HTML, PDF and whatever goes here. They read and parse data object from the Model

All business logic and applications states are stored here.

Problems with MVC


MVC is only a design pattern i.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

WebWork

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/

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

WebWork workflow

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

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

Action: HelloWorld.java
Extends a standard Action superclass.

Implement the business logic

How to access the Bean (the message)

View: helloworld.jsp

Use JSP Tag lib to access our Bean (the message)

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

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

WebWork: HelloWorld Output

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

Spring IoC

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.

Inversion of Control (IoC)


IoC is a principle that encourage the decoupling of code. Read: http://martinfowler.com/articles/injection.html
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

IoC Solution
Introduce an intermediate component to handle the assembly of class objects.
<creates>

Solution: If you want to use a different MovieFinder, just change how Assembler calls <creates>.

Spring IoC
A framework that can be coupled with WebWork to provide Dependency Injection.
Spring IoC implements a flexible Assembler.
Developers can tell this Assembler how to perform <create> via XML configuration.

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

Spring IoC: HelloWorld in gnizr


How HelloWorld Action is instantiated in gnizrs HelloWorld demo

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

Spring IoC: a more complex example

(1)

(2)

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

Apache Maven

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 you have experienced in the past (in school or at work)?

Problems I have faced


When some JAR library changed, I have to manually download the JAR from the project web site. Make sure dependency library are included in my CLASSPATH for building and testing Decide the directory layout of my project (where is src, classes, resources). Figure out how to write documentation and call javadoc in my project.

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/

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 be included 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

Maven: Create a project

Maven: POM file

Maven: build package

Try Maven in 5 minutes

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

Summary
Building a successful and maintainable Java Web application requires the effective use of software frameworks and development tools. WebWork and Spring are excellent frameworks for building scalable and high-quality Java Web applications. Apache Maven solves many project management issues that programmers have been struggling for a long time.

Вам также может понравиться