Top Banner
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
45

Developing Java Web Applications

May 17, 2015

Download

Technology

hchen1

An overview of Java Servlet technology,WebWork, Spring IoC and Apache Maven.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Developing Java Web Applications

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

Page 2: Developing Java Web Applications

Agenda

Web application architecture Java Servlet technologyWebWork: building webapps made easySpring: Inversion of Control (IoC)Maven: Java project management and

build tool

Page 3: Developing Java Web Applications

A canonical Web architecture

Do you see any technical issues in this architecture?

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

Page 4: Developing Java Web Applications

Stateless communication

Communications between your browser and the Web Server is usually stateless

“Cookie” was invented to solve this problem.

Page 5: Developing Java Web Applications

Thin client vs. Thick (fat) client

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

Thin clients live here.

Thick clients live here.

Page 6: Developing Java Web Applications

How do we create View?

Raw data must be processed before it’s presented to the user.

Users see the View (HTML, PDF, RSS etc.)

Should you “hardwire” code in the business logic to generate Views?

Raw dataView (HTML, PDF, etc)

How will be the Views be generated?

Page 7: Developing Java Web Applications

Overhead in access data

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)

SQL DBHTML

Do I have to call “SELECT”, “INSERT”,

“UPDATE” all the time?

Page 8: Developing Java Web Applications

Framework

Software engineering is like civil engineering: we need framework.

Option 2: Use framework Your Goal

Option1: Build everything from scratch…

Page 9: Developing Java Web Applications

Framework: It’s a juggle out there.

http://en.wikipedia.org/wiki/List_of_web_application_frameworks

JavaScript

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

ASP.NET

MonoRail DotNetNuke

CSLA

ASP.NET MVC Framework

JavaApache Cocoon Apache Struts

AppFuse Aranea framework

Click [fleXive]

Google Web Toolkit Grails

Hamlets ICEfaces

IT Mill Toolkit ItsNat

JavaServer Faces

Java

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

Perl

Catalyst Interchange

Maypole Mason

PHP

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

ColdFusion

ColdBox ColdFusion on Wheels

ColdSpring Fusebox Mach-II

Model-Glue onTap

Page 10: Developing Java Web Applications

Java Servlet technology

Page 11: Developing Java Web Applications

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

Page 12: Developing Java Web Applications

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

Page 13: Developing Java Web Applications

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

Page 14: Developing Java Web Applications

HelloWorld JSP

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

Use Java as if it’s a scripting language

Page 15: Developing Java Web Applications

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?

Page 16: Developing Java Web Applications

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

Page 17: Developing Java Web Applications

Example: J2EE webapp

This is what you want to build.

Page 18: Developing Java Web Applications

Example: J2EE webapp (cont.)

This is an MVC solutionAll 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

Page 19: Developing Java Web Applications

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

Page 20: Developing Java Web Applications

WebWork

Page 21: Developing Java Web Applications

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/

Page 22: Developing Java Web Applications

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

Page 23: Developing Java Web Applications

WebWork workflow

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

Page 24: Developing Java Web Applications

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

Page 25: Developing Java Web Applications

Action: HelloWorld.java

Extends a standard Action superclass.

Implement the business logic

How to access the Bean (the message)

Page 26: Developing Java Web Applications

View: helloworld.jsp

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

If you don’t like to use JSP, you have other options: Freemarker and Velocity

Page 27: Developing Java Web Applications

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

Page 28: Developing Java Web Applications

WebWork: HelloWorld Output

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

Page 29: Developing Java Web Applications

Spring IoC

Page 30: Developing Java Web Applications

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.

Page 31: Developing Java Web Applications

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

Page 32: Developing Java Web Applications

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

<creates>

Page 33: Developing Java Web Applications

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

Page 34: Developing Java Web Applications

Spring IoC: HelloWorld in gnizr

How HelloWorld Action is instantiated in gnizr’s HelloWorld demo

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

Page 35: Developing Java Web Applications

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)

Page 36: Developing Java Web Applications

Apache Maven

Page 37: Developing Java Web Applications

Jave project management

It concerns the management of library dependency, code building, unit testing, packaging and documentation.

- Why do you think it’s important to consider those issues when building software?

- Can you relate to what you have experienced in the past (in school or at work)?

Page 38: Developing Java Web Applications

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.

Page 39: Developing Java Web Applications

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/

Page 40: Developing Java Web Applications

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…

Page 41: Developing Java Web Applications

Maven: Create a project

Page 42: Developing Java Web Applications

Maven: POM file

Page 43: Developing Java Web Applications

Maven: build package

Page 44: Developing Java Web Applications

Try Maven in 5 minutes

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

Page 45: Developing Java Web Applications

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.