CSM-15_Lecture_8_old
Download
Report
Transcript CSM-15_Lecture_8_old
Component-Based
Software Engineering
Internet Applications
Paul Krause
Lecture 8 Internet Applications
Contents
Reminder of Client-Server architectures
Client-side programming with Applets and
JavaScript
Server-side programming with JSP and
Servlets
Client/server Architecture
Client
Client
Internet
Internet
Browser,
Applets, …
Browser,
Applets, …
Server
Database, servlets, …
Generic 5-tier Architecture
Client Tier
Presentation
Business Rules
Integration
Resources
Application Components
Web pages
Program components
HTML + scripting language code
Provides dynamic processing of data and modification
of visual appearance
Perform the main computations
Receive input from HTML forms
Present output as web-pages
Configuration files
Enables application to be quickly deployed on
different servers
Types of Program Component
Controller components
e.g. selection and customisation of the web-pages to
return to the client
Session beans
encapsulate information and operations specific to a
client session
• stateless session beans - e.g. check data entry
• stateful session beans - e.g. shopping cart
Entity beans
provide access to persistent data
A statefull session bean
public class BasketBean {
private HashMap contents = new HashMap;
public void addItem(Long itemid) { … }
public void updateItem(Long itemid, int quantity)
{…}
public Set getItems( ) { … }
public int getQuantity(Long itemid) { … }
public void deleteItem(Long itemid) { … }
public int numberOfItems( ) { … }
}
Internet Programming in Java
Java-based
technologies include:
Client-side:
Applets
JavaScript
Server-side:
JSP - Java Server Pages
Java Servlets
Lecture 8 Internet Applications
Contents
Reminder of Client-Server architectures
Client-side programming with Applets and
JavaScript
Server-side programming with JSP and
Servlets
HelloFromVenus.java
import java.awt.*;
import java.applet.Applet;
// Copyright (c) 1999-2002, Xiaoping Jia.
public class HelloFromVenus extends Applet {
public void paint(Graphics g) {
Dimension d = getSize();
g.setColor(Color.black);
g.fillRect(0,0,d.width,d.height);
g.setFont(new Font("Helvetica", Font.BOLD, 24));
g.setColor(new Color(255, 215, 0)); // gold color
g.drawString("Hello From Venus!", 40, 25);
g.drawImage(getImage(getCodeBase(), "Venus.gif"),
20, 60, this);
}
}
Invoking the Applet
<HTML>
<HEAD>
<TITLE>
Object-Oriented Software Development Using Java | HelloFromVenus
Applet
</TITLE>
</HEAD>
<BODY BGCOLOR=BLACK TEXT=white>
<CENTER>
<H2>Here is the <EM>Hello From Venus</EM> Applet</H2>
<APPLET CODE="HelloFromVenus.class" WIDTH=300 HEIGHT=350>
</APPLET>
</CENTER>
Venus photo courtesy of NASA.
</BODY>
</HTML>
JavaScript
Notation
is close to Java, although with
some important differences
no explicit types for variables
simplified structure to the code - a “scripting
language”
But provides a powerful client-side
processing and scripting language
Following examples from Lano et al,
Software Design Using Java 2
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<HTML>
<HEAD>
<TITLE>Date Check Program</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
// Some Java-like processing stuff goes in here
// …
</SCRIPT>
</HEAD>
<BODY>
Click Refresh or Reload to run script again
</BODY>
</HTML>
var firstNumber, secondNumber, number1, number2;
firstNumber = window.prompt("Enter month (1--12)", "1");
secondNumber = window.prompt("Enter day (1--31)", "1");
number1 = parseInt(firstNumber);
number2 = parseInt(secondNumber);
if (1 <= number2 && number2 <= 31 &&
(number1 == 1 || number1 == 3 || number1 == 5 ||
number1 == 7 || number1 == 8 || number1 == 10 ||
number1 == 12))
{ document.writeln("<H1>Date is correct</H1>"); }
else if (1 <= number2 && number2 <= 30 &&
(number1 == 4 || number1 == 6 || number1 == 9 || number1 == 11 ))
{ document.writeln("<H1>Date is correct</H1>"); }
else if (1 <= number2 && number2 <= 29 && number1 == 2 )
{ document.writeln("<H1>Date is correct</H1>"); }
else
{ document.writeln("<H1>Date is not correct!</H1>"); }
Browser Events
Event
HTML Syntax
Page is loaded
<BODYONLOAD = “function( )”>
Button is pressed
<INPUTTYPE = “button”
VALUE = “command”
ONCLICK = “function( )”>
<BODYONMOUSEMOVE =
“function( )”>
Mouse is moved
Mouse over element <…ONMOUSEOVER = “function( )”>
Form entry active
<INPUT…ONFOCUS = “function()”>
Lecture 8 Internet Applications
Contents
Reminder of Client-Server architectures
Client-side programming with Applets and
JavaScript
Server-side programming with JSP and
Servlets
JavaServer Pages (JSP)
Works
on the Server side to add
functionality to HTML pages
Typically, JSP is used to add more
complex functionality than JavaScript
JSP has a much closer relationship with
Java Beans:
Beans can be invoked from within JSP files
Indeed, this is recommended to separate the
GUI (html) from back-end code (Beans)
What does being a Bean mean?
In
this context it is simply an instance of a
Java class, conforming to an agreed
pattern:
The class has a no argument constructor
Properties are accessed through “get”
methods (“is” methods in the case of
booleans)
Properties are updated through “set” methods
A JSP
file then knows how to interface to it
A Booking Bean
public class BookingBean {
private int month;
private int day;
private String name;
private String number;
private boolean smoking:
public BookingBean( ) { }
// no argument constructor
public void setMonth(String mon)
{month = Integer.parseInt(mon); }
public String getMonth( )
{return “ “ + month;} …
Using a Bean in JSP
Within
a Java Server Page, we may wish
to update and read a particular instance of
the BookingBean class.
There is a simple mapping from lines in a
JSP script, and the equivalent Java
statements
This mapping is enabled by usage of the
Java Bean coding guidelines
Creating and updating a Bean
<jsp : useBean id = “booker” class = “BookingBean”/>
Corresponds to the Java statement:
BookingBean booker = new BookingBean( );
<jsp : setProperty name = “booker” property = “month”
param = “month”/>
Corresponds to the Java statement:
booker.setMonth(request.getParameter(“month”));
Note, in this last case request is a Java object
representing an HTTP request
Example jsp file
<jsp : useBean id = “booker” class = “BookingBean”/>
<jsp : setProperty
name = “booker” property = “month” param = “month”/>
<jsp : setProperty
name = “booker” property = “day” param = “day”/>
// …
<HTML>
<HEAD><TITLE>Echo Parameters</TITLE></HEAD>
<BODY>
Thank you, <jsp:getProperty name = “booker” property =
“name” /> for your booking. You requested …
Java Servlets
Servlets
use Java packages for server
side processing of internet applications
This is a pure Java approach, rather than
the hybrid Java/HTML approach of JSP
Can therefore write better structured, and
easier to debug applications
Summary
A quick survey of Java technologies for Internet
Applications
We saw the different needs of:
client-side programming - relatively light-weight
processes for data validation and gui configuration
server-side programming - more computationally
intensive applications with access to persistent data
sources
Structure these applications using ideas from
Software Components to avoid a serious mess!