CS 55.17
Developing Web Applications
with
Class Introduction
● Instructor: David B. Pearson
● Email: [email protected] ● Yahoo! ID: DavidPearson
About David
● I make my living developing web applications
using Java technology.
● I've been working with Java since 1997.
● I've been teaching Java since 2002.
● I am a Sun Certified Java Programmer
● I have also taught Java Programming, PHP
Programming, HTML, Introduction to the Apache Web Server
Goal
The purpose of this course is to learn to develop web applications using several Java
technologies. The skill and knowledge required to write web applications will be developed.
Objectives
● Understand the rational of the Java servlet
specification.
● Employ object-oriented design methods.
● Work within an integrated development
environment.
Prerequisites
● Previous programming experience with Java
Lecture
● 3 Hour Lecture in Room 2913 and online via
Elluminate
● 6:30 – 9:30 PM
Text Books
● Core Servlets and JavaServer Pages: Core
Technologies, Vol. 1 (2nd Edition) by Marty Hall
● More Servlets and JavaServer Pages (1st
Edition) by Marty Hall
● The Java EE 6 Tutorial
Class Website
What is JEE?
● Java Enterprise Edition
● Open standards based platform for
application:
– Development – Deployment – Management
Java Editions
Java Technology
Enabled Devices Java Technology
Tiered Enterprise Applications
● Single Tier
● Two Tier
● Three Tier
Components of Enterprise
Applications
● Things that make up an enterprise application
– Presentation logic – Business logic
– Data access logic (and data model) – System services
● The evolution of enterprise application
framework reflects
– How flexibly you want to make changes
Single Tier
● Typically mainframe-based
● Dumb terminals are directly connected to the
computer
● Centralized model
● Presentation, business logic and data access
Single Tier: Pros & Cons
● Pros:
– No client side management is required – Data consistency is easy to achieve
● Cons:
– Functionality (presentation, data model,
business logic) is intertwined, difficult for updates and maintenance and code reuse
Two Tier
● Fat clients talking to back end database
– SQL queries sent, raw data returned
● Presentation, Business logic and Data Model
processing logic in the client application
Database
Database
SQL request
Two-Tier: Pros & Cons
● Pros:
– Database product independence
● Cons:
– Presentation,data model, business logic are
intertwined at the client side, difficult for updates and maintenance
– Data Model is “tightly coupled” to every client: if DB
Schema changes, all clients break
– Updates have to be deployed to all clients making
system maintenance nightmare
– DB connection for every client, the difficult to scale – Raw data transferred to client for processing causes
Three-Tier (RPC based)
● Thinner client: business & data model
separated from presentation
– Business logic and data access logic reside in
middle tier while client handles presentation
● Middle tier server is required to handle system
services
– Concurrency control, threading, transaction,
security, persistence, multiplexing, performance, etc. Database RPC request RPC response SQL request SQL response
Three-Tier (RPC based): Pros &
Cons
● Pros:
– Business logic can change more flexibly than
2-tier model
● Most business logic resides in the
middle-tier server
● Cons:
– Complexity is introduced in the middle-tier server – Client and middle-tier server is more tightly
coupled (than the three-tier object based model)
– Code is not really reusable (compared to object
Three-Tier (Remote Object based)
● Business logic and data model captured in
object
– Business logic and database are now
described in “abstraction” (interface language)
● Object models used: CORBA, RMI, DCOM
– Interface language in CORBA is IDL
– Interface language in RMI is Java interface
Database
Object request
Object response
SQL request
Three-Tier (Remote Object based):
Pros & Cons
● Pros:
– More loosely coupled than RPC model – Code could be more reusable
● Cons:
– Complexity in the middle-tier still needs to be
Three-Tier (Web Server)
● Browser handles presentation logic
● Browser talks to web server via HTTP protocol
● Business logic and data model are handles by
“dynamic content generation” technologies (CGI, Servlet/JSP, ASP)
Database Web Server HTML request HTML response SQL request SQL response
Three-Tier (Web Server based):
Pros & Cons
● Pros:
– Ubiquitous client types – Zero client management
– Support various client devices
● Cons:
– Complexity in the middle-tier still needs to be
Trends
● Moving from single-tier or two-tier to multi-tier
architecture
● Moving from monolithic model to object-based
application model
● Moving from application-based client to
Single-tier vs. Multi-tier
● Single-tier – No separation among presentation, business logic, database – Hard to maintain ● Multi-tier – Separation among presentation, business logic, database – More flexible to change, i.e. presentation can change without affecting other tiersMonolithic vs. Object-based
● Monolithic – 1 binary file – Recompiled, relinked, redeployedevery time there is a change
● Object-based
– Pluggable parts – Reusable
– Enables better design – Easier update – Implementation can be separated from interface – Only interface is published
Outstanding Issues & Solutions
● Complexity at the middle tier still remains
● Duplicate system services still needs to be
provided for the majority of enterprise applications
– Concurrency control, transactions – Load-balancing, Security
– Resource management, Connection pooling
● How to solve this problem?
– Commonly shared container that handles the
above system services
Proprietary Solution
● Use “component and container” model
– Components capture business logic – Container provides system services
● The contract between components and
container is defined in a well-defined but with proprietary manner
● Problem of proprietary solution: vendor lock-in
Open and Standard Solution
● Use “component and container” model in
which container provides system services in a well-defined and as industry standard
● JEE is that standard that also provides
portability of code because it is base on Java technology and standard-based Java
Platform Value to Developers
● Can use any JEE implementation for
development and deployment
– Use production-quality standard implementation
which is free for development/deployment
– Use high-end commercial JEE products for
scalability and fault-tolerance
● Vast amount of JEE community resources
– Many JEE related books, articles, tutorials, quality
code you can use, best practice guidelines, design patterns, etc.
● Can use off-the-shelf 3rd-party business
Platform Value to Vendors
● Vendors work together on specifications and
then compete in implementations
– In the areas of Scalability, Performance,
Reliability, Availability, Management and development tools, and so on.
● Freedom to innovate while maintaining the
portability of applications
● Do not have create/maintain their own
Platform Value to Business
Customers
● Application portability
● Many implementation choices are possible
based on various requirements
– Price (free to high-end), scalability (single CPU
to clustered model), reliability, performance, tools, and more
– Best of breed applications and platforms
Tiered Java Applications
● The Client Tier
● The Web Tier
● The Business Tier
The Web Tier
● Primary focus of this class
● Technology
– Servlets
– JavaServer Pages
– Expression Lanaguage
– JavaServer Pages Standard Tag Library – JavaBeans Components
– JavaServer Faces
The Business Tier
● Provides the functionality to a particular
business domain
● Technologies
– Enterprise JavaBeans – Web services
The Enterprise Information Systems
Tier
● Typically located on a different machine than
the Java EE application server
● Technologies
– Java Database Connectivity API (JDBC) – Java Persistence API
– Java EE Connector Architecture – Java Transaction API (JTA)
Java EE Servers
● A server application that implements the Java
EE platform APIs and provides the standard Java EE services.
● Composed of “Containers”
– The Web Container
– The Application Client Container – The EJB Container
JEE Summary
● JEE is the platform of choice for development
and deployment of n-tier, web-based,
transactional, component-based enterprise applications
● JEE is standard-based architecture
● JEE is all about community
● JEE evolves according to the needs of the
What is a Servlet?
● Java object which extends the functionality of
a HTTP server
● Dynamic content generation
● Better alternative to CGI, NSAPI, ISAPI, etc.
– Efficient
– Platform and server independent – Session management
Servlet vs. CGI
Request Servlet 1 Request CGI 2 Request CGI 3 CGI Based WebserverChild for CGI 1
Child for CGI 2
Child for CGI 3
Servlet Based WebServer
Servlet 1 Servlet 2 JVM Servlet 1 Request CGI 1 Request Servlet 2 Request Servlet 3
A Servlet's Job
● Read explicit data sent by client (form data)
● Read implicit data sent by client (request
headers)
● Generate the results
● Send the explicit data back to client (HTML)
● Send the implicit data to client (status codes
HTTP Headers
● In a request, HTTP sends hidden data from
the browser to the web server, and then it sends what the user is transmitting, this
hidden data, because the data comes first, it is call HTTP Headers
● In a response, HTTP sends hidden data from
the web server to the browser, before it sends the data that gets displayed in the browser
Why build web pages dynamically?
● The Web page is based on data submitted by
the user
– E.g., results page from search engines and
order-confirmation pages at on-line stores
● The Web page is derived from data that
changes frequently
– E.g., a weather report or news headlines page
● The Web page uses information from
databases or other server-side sources
– E.g., an e-commerce site could use a servlet to build a
Web page that lists the current price and availability of each item that is for sale.
What is JSP technology?
● Enables separation of business logic from
presentation
– Presentation is in the form of HTML or XML
– Business logic is implemented as Java Beans
or custom tags
– Better maintainability, reusability
● Extensible via custom tags
The Power of JSP
● Idea
– Use regular HTML for most of page
– Mark dynamic content with special tags
<!DOCTYPE html> <html>
<head><title>Welcome to Our Store</title></head> <body>
<h1>Welcome to Our Store</h1> <p>Welcome,
<!-- User name is "New User" for first-time visitors --> <%= coreservlets.Utils.getUserNameFromCookie(request) %>
<%= coreservlets.Utils.getUserNameFromCookie(request) %>
To access your account settings, click
<a href="Account-Settings.html">here.</a></p>
<p>Regular HTML for rest of on-line store’s Web page</p> </body>
Online Documentation
● Servlets and JSP
– http://download.oracle.com/javaee/6/api/
● Java 6
Installing Java SE 6
● Minimum Java version
– Tomcat 7 (Servlet 3.0) requires Java 6
● Downloading and installation
– Follow directions at
http://download.oracle.com/javase/
● Choose “JDK” not “JRE”
– Not “with Java EE”, “with JavaFX”, or “with
Download and Unzip tomcat
● Start at http://tomcat.apache.org/
– Choose download link on left, then ZIP version
● Tomcat 7
● Just unzip the file
Installing Eclipse
● Eclipse is a free open source IDE for Java,
HTML, CSS, JavaScript, PHP, C++, and more.
– http://www.eclipse.org/downloads/
– Choose “Eclipse IDE for Java EE Developers”
● Need version 3.6 (Helios) or better for Tomcat 7
Configure Eclipse
● Tell Eclipse about Java version
– Window → Preferences → Java → Installed JREs →
Press “Add”, choose “Standard VM”, navigate to JDK folder (not bin subdirectory)
● i.e. C:\Program Files\Java\jdk1.7.0
● Tell Eclipse about Tomcat
– Click on Severs tab at bottom. Right-click in window. – New → Sever → Apache → Tomcat 7.0 → Next →
navigate to folder → Finish
● Suppress serializable warnings
– Window → Preferences → Java → Compiler →
Making Web Apps in Eclipse
● Make empty project
– File → New → Project → Web → Dynamic
Web Project
– For “Target runtime” choose “Apache Tomcat
v7.0”
– Give it a name → A01_Lastname_Firstname – Accept all other defaults
Adding Code to Eclipse Projects
● Locations
– Java Resources: src
● Servlet code
– WebContent
● Web files (HTML, JavaScript, CSS, JSP, images,
etc.)
– WebContent/some-subdirectory
● Web content in subdirectory
– WebContent/WEB-INF
● web.xml
– Optional with servlets 3.0. required in 2.5 & earlier – Will be discussed later
Testing New App
● Deploy project
– Select “Servers” tab at bottom – Right-click on Tomcat
– Choose “Add and Remove” – Choose project
– Press “Add” – Click “Finish”
● Start Server
– Right-click Tomcat at bottom
– Restart (use “Start” if Tomcat not already running)
● Test URL
Homework
● Read
● Install software