Developing Java Web Applications
-
Upload
hchen1 -
Category
Technology
-
view
22.662 -
download
5
description
Transcript of 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
Agenda
Web application architecture Java Servlet technologyWebWork: building webapps made easySpring: 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
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.
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?
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?
Framework
Software engineering is like civil engineering: we need framework.
Option 2: Use framework Your Goal
Option1: Build everything from scratch…
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
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
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
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
Source: http://mainline.brynmawr.edu/~dkumar/JSP/
Use Java as if it’s a scripting language
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 (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
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
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 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 don’t 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
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. 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
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>
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 gnizr’s HelloWorld demo
http://code.google.com/p/gnizr/wiki/HelloWorldDemo
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)
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)?
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…
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.