Transcript Document

Network Communication and Applets
Dr Andy Evans
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
The web
The web has a client-server architecture.
Server or ‘Host’
Request
Server Program
Client
Web browser
Files returned
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
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.*.
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.
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
java.net.URL
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.
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
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.
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/”))
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
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
applets.
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.
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
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);
}
}
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working with webpages.
Next week.
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 = 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(InetServer, 9999);
BufferedOutputStream b = s.getOutputStream();
b.write(aByteArray);
s.close();
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.
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.
Applets.
URLs.
Advanced Applets.
Webpages.
Network communications.
The Art of Programming Part Ten: Working
with webpages.
Next week.
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.
There’s also an online tutorial – details on the handout.
Before you design any pages, read through the Design tips
and ‘Writing for the web sections’.
Basic web design tips
Keep it simple and easy to read.
Try to keep to two screenfulls of info on any page if you can.
Don’t use jargon or long paragraphs.
Highlight important sentences to help skim readers.
Make navigation obvious.
Use text links that say where they go.
People prefer having lots of links on one page more than having to
click through several pages to get to info.
When in doubt, keep it plain.
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=“blind.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.
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>
Transferring files
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.
Summary
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.
Summary
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.
Summary
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.