Transcript Document

Programming for
Geographical Information Analysis:
Core Skills
Lecture 10:Core Packages:
Network communications
This lecture
How the web works
Applets
URLs
Applets++
Other internet communication
The internet and web
The internet: fundamental network over which different
communication technologies can work (including email, file
transfer, the web). Generally regarded as those elements
using the TCP/IP protocol (more later).
The web: a hypertext (linked text) system with embedded
media based within/utilising the internet.
Java and the internet
Java first major language to be built for/after the web.
Web-based options (Applets, URLConnections, JSP,
Servlets).
Other internet communication options.
To understand the web-based elements we need to first
understand the basics of the web and webpages.
The web
The web has a client-server architecture.
Server or ‘Host’
Request
Server Program
Client
Web browser
Files returned
Webpages
As we’ve mentioned they’re usually sent out by a web server to
clients who ask for them, but you can open them directly on your
harddrive.
They consist of text that will be displayed and tags that won’t, which
include formatting details or references to other files like images or
java code.
The tags are referred to as the HyperText Markup Language (HTML).
Saved as text files with the suffix .html (or sometimes .htm).
A basic webpage
<HTML>
<HEAD>
<TITLE>Title for top of browser</TITLE>
</HEAD>
<BODY>
<!--Stuff goes here-->
</BODY>
</HTML>
The HEAD contains information about the page, the BODY contains
the actual information to make the page.
<BODY>
The break tag breaks a line<BR>
like that.
<P>
The paragraph tags
</P>
leave a line.
This is <B>Bold</B>.
This is <I>Italic</I>
<IMG src=“tim.gif” alt=“Photo: Pic of Tim” width=“50”
height=“50”></IMG>
<A href=“index.html”>Link text</A>
</BODY>
The text in the file will only be shown with the format set out in the
tags. Any line breaks etc. won’t show up on screen.
Good web design.
Like any GUI, good web design concentrates on usability.
There are a number of websites that can help you – these
are listed on the links page for this course.
See also the tutorial on the course pages.
This lecture
How the web works
Applets
URLs
Applets++
Other internet communication
Applets
Small programs that run on web clients.
Are contained in a tight security ‘sandbox’.
Can’t read or write files on the browser’s machine.
Can’t start local programs or access the local OS.
Can only access the machine it was sent from (though can get webpages from
others).
Can’t switch off and replace the JVM.
You can, however, “sign” applets. If the user trusts you the
applets can be given more freedom.
Basics
Normal class code called by a line within a webpage which
says that an applet should be inserted and where to find it.
All applets extend java.applet.Applet.
All therefore need to import java.applet.*.
java.applet.Applet is a subtype of Panel.
All applets are GUI based and all but simplest are Event Based.
Therefore they also need to import java.awt.* and
java.awt.event.*. Note though, like panels, they can’t
have menus in the webpage (though they can open Frames).
Methods
Don’t usually have a ‘main’ method.
init()
Replaces the main/constructor – used to set-up the applet. Called
when applet first shown.
start()
Used each time an applet is shown – e.g. if a webpage is visited, left
and then revisited. Calls paint().
paint()
Inherited by Applet from Panel / Component. Called at start and
whenever needed, e.g. if the browser is covered by another window
and then uncovered.
Methods
stop()
Called when a browser leaves the webpage.
destroy()
Called when the browser kills the applet - ‘stop’ is always called
first. Not usually possible to say when this happens.
You don’t have to implement all these methods – each has a
default.
Just implement those you want – usually init, start and paint.
Basic Applet
import java.applet.*;
import java.awt.*
import java.awt.event.*
public class OurApplet extends Applet {
public void init() {
System.out.println(“Hello World!”);
}
}
System.out is the browser’s Java Console.
This lecture
How the web works
Applets
URLs
Applets++
Other internet communication
java.net.URL
To understand advanced Applet methods we need to understand URLs.
Encapsulates a Uniform Resource Locator (URL).
http://www.w3.org:80/People/Berners-Lee/Overview.html
A URL represents another file on the network. It’s comprised of…
– A method for locating information – i.e. a transmission protocol,
e.g. the HyperText Transmission Protocol (http).
– A host machine name, e.g. www.w3.org
– A path to a file on that server, e.g.
/People/Berners-Lee/Overview.html
– A port to connect to that server on, e.g. http connects to port 80.
Client – Server architecture
A file requested using a URL will usually be handled by a clientserver architecture.
Which server program gets which requests will depend on the port
they are sent to, which is usually related to the transmission
protocol.
e-mails are sent out to servers using Simple Mail Transfer Protocol
(SMTP) which usually goes into Port 25.
One server can serve several clients at once.
Webservers use http/port 80 to listen for requests, then send out
webpages from a particular requested directory.
Making a URL object
Need to import java.net.*, and deal with the
MalformedURLException that the URL constructors
throw.
You can then use one of the URL class constructors. Some let
you put in the port, or split off the path.
Easiest is…
URL url = new URL(String urlSpecifier);
Where urlSpecifier is an address like…
"http://www.w3.org/People/Berners-Lee/Overview.html"
Basic URL Methods
getFile()
Gets the file name of this URL.
getHost()
Gets the host name of this URL, if applicable.
getPath()
Gets the path part of this URL.
getPort()
Gets the port number (int) of this URL.
This lecture
How the web works
Applets
URLs
Applets++
Other internet communication
Advanced Applet methods
getImage(URL)
Returns an java.awt.Image object from a given URL
object.
getDocumentBase()
Returns a URL object representing the directory the
applet starting webpage was in.
Together with the URL methods, these methods allow us
to retrieve image files from our web server.
Resource Streams
Class thisClass = getClass();
URL url = thisClass.getResource("debts.txt");
As well as getting a URL Object this way, you can actually
open a Stream to resources.
Class Objects have a getResourceAsStream method,
which returns an InputStream.
This can be used to read the resource across the network or
into a program from a local file.
More details at:
http://docs.oracle.com/javase/7/docs/technotes/guides/lan
g/resources.html
java.net.URLConnection
If you want specific files from an http server which exists, it’s
easier to use a URLConnection object.
URL url = new
URL(“http://www.bbc.co.uk/eastenders/index.html”);
URLConnection urlc = url.openConnection();
Methods include…
getDate()
getContentLength()
getInputStream()
Knowing the number of bytes of a resource (the
ContentLength), and having an InputStream, you can now read
the file.
Advanced Applet methods
getAppletContext()
Returns an AppletContext object representing the environment
the applet is running in.
AppletContext ap = getAppletContext();
The most important method in an AppletContext object is…
showDocument(URL webpage)
This allows you to put a webpage into the browser window.
ap.showDocument(new URL(“http://www.w3.org/”));
Putting an applet into a webpage
<APPLET
code = appletFile (without the .class)
width = widthInPixels
height = heightInPixels
align = left or right or center
>
<PARAM name=“name1” value=“attributeValue1”>
<PARAM name=“name2” value=“attributeValue2”>
</APPLET>
PARAMs
We can pull in the parameters using the applet’s
getParameter(String paramName) method, which returns the
values associated with paramName as a String. For example, in our
web page we might have:
<PARAM name = "colorTypes" value = "1" >
and in our init() we might have…
String colorScheme = getParameter("colorTypes");
if (colorScheme.equals("1")) {
setBackground(Color.WHITE);
}
Note that we treat “1” as a String, even though it is a numerical
character.
Putting an applet into a webpage
<OBJECT
codetype="application/java"
classid= appletFile
width = widthInPixels
height = heightInPixels
>
<PARAM name=“name1” value=“attributeValue1”>
<PARAM name=“name2” value=“attributeValue2”>
</OBJECT>
http://docs.oracle.com/javase/1.5.0/docs/guide/plugin/de
veloper_guide/using_tags.html
Transferring files
When writing for the network, you can compress class files and
associated resources using the jdk1.7/bin/jar.exe compressor. This
creates “zip”-like files that can be read in java using java.util.zip
or Class’s getResource().
<APPLET
code = myClass
archive = myJar.jar
width = 300 height = 300>
</APPLET>
Web accessibility
If you are working for a public organisation, accessibility
for the disabled has to be a major design driver. In
addition, not everyone can see applets (~2% of browsers).
Generally you can make webpages accessible by not
putting important information in images and sound.
Java 1.1 applets have no accessibility built in.
Java Foundation Class / Swing applets have accessibility
built in – will be usable with speaking browsers etc.
Web accessibility: Java 1.1
<APPLET code = appletFile width = widthInPixels
height = heightInPixels>
Please visit this <A href=“text.html”>site</A>
for a text list of our information.
</APPLET>
Anything you write between in the APPLET / OBJECT tags will
only be displayed if the applet doesn’t work.
Writing joint Applets and Applications
Because Applets are types of Panels, we can do this...
public class TwoFaced extends java.applet.Applet {
public void init() {
// All our code here.
}
public static void main(String[] args) {
Because the class is
a type of Panel we
can add it to the
frame.
Frame appFrame = new Frame("Two Faced");
TwoFaced twoFaced = new TwoFaced ();
twoFaced.init();
appFrame.add(twoFaced, BorderLayout.CENTER);
appFrame.setSize(300, 300);
appFrame.setVisible(true);
}
}
This lecture
How the web works
Applets
URLs
Applets++
Other internet communication
Introduction to network communications
Communication is via a port, using a protocol. Several protocols may
be involved at once. Most use the following over the internet…
Internet Protocol (IP)
Used to split data into small chunks called “packets”
Addresses them to the right machine.
Transport Control Protocol (TCP)
Guarantees packets get to their destination.
Controls the route taken and lets computers confirm receipt.
Adds packets back together in the right order.
HyperText Transfer Protocol (HTTP)
java.net.InetAddress
We saw the URL class, and how it encapsulates the
location of things on a network.
There is a second address class that deals with finding
machine IP address-specific information.
java.net.InetAddress
This has no constructors. You create one using the class’s
static methods.
InetAddress creation
InetAddress ina =
InetAddress.getLocalHost();
Returns an InetAddress object representing the
machine the program is running on.
InetAddress ina =
InetAddress.getByName(hostNameString);
Returns an InetAddress corresponding to the host
named.
InetAddress[] ina =
InetAddress.getAllByName(siteNameString);
Returns an array of InetAddress objects containing all
the servers associated with a given site name.
InetAddress methods
IP addresses are numeric
http://129.11.93.2
will get the old School webserver.
However, many machines hold a registry which contains the numbers
and what they’d prefer to be called. This scheme is called the Domain
Name Service (DNS)
http://www.geog.leeds.ac.uk
Methods exist for finding a numerical internet address (IP address)
from the domain name, and vice versa.
These methods might have to do the DNS look-up on another
computer on the network - this may take some time.
Sockets
Once you have your address, you can then connect to it.
To do this, we use “sockets”.
These are TCP/IP based stream connections to a computer
port. You need a client and a server.
java.net.Socket
Sends requests to communicate to a port using TCP or equivalent.
java.net.ServerSocket
Lets you build your own server.
Sits and listens to a port in case something calls it.
Sockets
Socket(InetAddress ipAdd, int port)
ServerSocket(int port)
TCP/IP reserves the first 1024 ports, otherwise you can use most
numbers up to 49151.
A ServerSocket is set to listen with its accept()method which
returns a Socket when activated.
Once you have your sockets connected, you can use their
getInputStream(), getOutputStream() and close() methods to
talk using streams.
Sockets example
On the server…
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
BufferedInputStream in =
new BufferedInputStream(s.getInputStream());
Start the program.
Once a connection has been accepted, use the socket to
communicate, not the ServerSocket.
On the client…
Socket s = new Socket(iNetAddress, 9999);
BufferedOutputStream b =
new BufferedOutputStream(s.getOutputStream());
b.write(aByteArray);
s.close();
Ports and Firewalls
Always check what other programs, if any, use the port you
are on.
Some networks have “Firewalls”. These are security devices
that sit on Ports and stop some connecting. Check for
them.
Review
The web is a client-server based architecture.
It is used to link and distribute hypertexts and embedded
media.
It runs on the internet.
The text is coded using the HyperText Markup Language.
Review
Client – server architectures are a common mechanism
for retrieving files over the internet.
One java class that uses them is URL which encapsulates a
file location and the protocol to get it.
Applets can use URL objects to get files from the internet
and display them.
Applets are usually run from webpages. The System.out is
the Java Console.
Review
Applets are programs that run in web browsers.
Because of this they have their own security limitations.
They have init, start, stop, and destroy methods.
init and destroy are called when the applet is first started
and killed respectively.
start and stop are called when the webpage is re-entered
and left.
Review
Most networks use TCP/IP to communicate.
HTTP gets and sends specific data-type requests on TCP/IP
networks.
We can encapsulate specific machine addresses with an
InetAddress object, and specific resources with a URL object.
Using a URL/URLConnection we can get basic resource types
from a http / web server.
For more complex data and object transmission, we can set up
sockets.
We can use ServerSocket objects to make our own servers.
Practical
Introduction to Advanced Programming:
Cry 'Havoc!', and let slip the dogs of
war…
Agent-based modelling.
Next lecture
Mopping up