300130 Internet Programming

Dr Zhuhan Jiang Penrith Campus

1

1 Contact Details

„ Name: Zhuhan Jiang

„ Office: ECG-64, Parramatta campus

„ Telephone: 9685 9336

„ : [email protected]

„ Unit Homepage: http://www.cit.uws.edu.au/~zhuhan/ip

2 Time Table

„ Lectures

„ 10am-12pm Thursday, UG13 „ Practical (workshops)

„ 2pm-4pm Thursday, Y241

„ 4pm-6pm Thursday, Y241 (repeat, not available) „ Consultation

„ 11am-1pm Tuesdays, ECG-64, Parramatta nd „ First workshop starts on the 2 week

3 Prerequisites/Exclusions

„ Prerequisites

„ Introduction to Analysis and Design (or equivalent)

„ Object Oriented Programming (or equivalent programming experience)

„ Exclusions

„ Objected Oriented/Internet Programming

„ Internet Computing

4 Assessment

„ 3 Random Workshops 5% each „ 1 Individual Assignment 15% „ 1 Group Assignment 25%

„ 3-4 people per group

„ Self-organised or assigned „ Final Exam 45% „ All submissions (other than group assignment) are electronic.

5 Text/Reference Books Real code for real „ Core Web Programming programmers nd „ Hall M & Brown L, 2 ed, , Prentice-Hall, 2001

„ , How to Program th „ Deitel H M & Deitel P J, 4 ed, Prentice-Hall, 2000 (3rd ed also ok)

„ The Java Tutorial Continues: The Rest of JDK (free on web)

„ Campione M et al, Addison-Wesley, 1998 (free on web)

6 Main objectives

„ Master principles & techniques of OOP

„ Master for the Internet curve „ Web pages with forms and CGI/PERL diary „ with GUI and multithreads ufo „ Multimedia and Internet games explorer „ Java networking and security server „ Distributed computing with RMI chatlite „ Java Server Pages, JDBC, Beans, XML etc

7 Hardware and

„ Windows 2000, Linux, Unix platforms

„ Web, CGI, PERL or others

„ Java 2 (ver 1.4), IDE

„ J2RE plug-in for IE and

„ Tomcat for servlets and JSP

„ MS Access for JDBC

„ Java BeanBox

8 What is Java?

„ Prominent in OOP „ Portable at bytecode level „ Less time for application development „ Connection with C/C++ „ Other keywords

„ Interpreted, distributed, robust, secure, multithreaded „ Main executables

„ javac, java, appletviewer, javadoc 9 Internet and Browsers

„ Internet

„ World’s largest IP based network

„ Talk to each other using IP protocol

„ Connected world wide, owned distributively „ Mainstream browsers typically support Java, CSS, Javascript etc

„

„

„ HotJava, etc

10 Java Program Execution /* filename: SimpleJava.java */ public class SimpleJava { public static void main(String[] args) { System.out.println("Hello world!"); } }

„ Naming for code and class files

„ Compile: javac SimpleJava.java

„ Execute: java SimpleJava

11 HTML Documents

„ normal text interspersed with markup tags affected text

„ may contain Javascript, Java applets, server- side includes etc

„ javadoc - generates class docs in HTML tags document title document content 12 Access Attributes

„ Web server often run as user nobody „ All etc files to be accessed by a remote browser must have “readable to others” attributes

„ chmod a+r index.html *.gif „ CGI needs “executable to others”

„ Not needed if running cgiwrap, see later „ CGI in script needs “readable” „ All webpath subdirectories must be “executable”

13 Forms http://localhost/ip/webquiz/submit.pl

„ Collect data from user of „ This data may be used locally on the HTML page, or sent to CGI for processing

„ web search engine

„ guest book, registration, logon

„ online shopping, online quiz

„ feedback form

14 General Format

„ "formname" is any string and is used to identify a particular form if more than one is used in a page

15 Method & Action

„ "method" is either "POST" or "GET"

„ In the "GET" method, the form data is appended to the URL of the action

„ In the "POST" method the form data is sent as part of the HTTP header information

„ "action" is usually the URL of a CGI program on the server that will process the form data

16 CGI Work

HTML Form Data from B Form Input r Form o Web Processing w User Server program s Data from (CGI) E Form HTML r Output Document

HTML Document

17 Form Example with CGI

Input Name: Input Age:

„ “POST” reads paras from STDIN

„ “GET” from env QUERY_STRING

„ Or directly, e.g. http://…/listall.cgi?FullName=…&Age=…

18 CGI Languages

„ UNIX shell script „ PERL (Practical Extraction and Reporting Language) „ C/C++ „ ASP - Active Server Pages „ Virtually any language that understands stdin/stdout and environment variables

19 CGI URL

„ Popular/standard format

„ http://localhost/~zhuhan/listall.cgi (Apache)

„ http://localhost/ip/listall.asp (IIS) „ CIT’s format (on the Unix machine) „ http://www.cit.uws.edu.au/cgi- Where to keep bin/cgiwrap/~zhuhan/listall.cgi CGI programs username„ CGI program listall.cgi resides in /~zhuhan/Homepage/cgi-bin (“~” represents $HOME) at the CIT site „ cgiwrap more secure: server assumes the status of the program’s owner

20 Simple CGI Program

#!/bin/sh # file: listenv.cgi read message_by_post echo content-type: text/plain echo if ! [ -z "$message_by_post" ] ; then echo POST_STDIN=$message_by_post fi env

„ CGI in any script or in compiled form „ Must be preceded by a header 21 CGI Header

„ MIME: Multipart Internet Mail Extensions „ output of CGI must have a MIME header to indicate what type of output is to expect Content-type: type/subtype Other-settings-if-any: values header terminated by an empty line e.g. text/plain, text/html, image/gif, image/jpeg

22 Associative array PERL useful $paras{'name'}=value but not examinable object Another Example

use CGI; # filename: paras.pl $cgi=new CGI; A field of the object %paras=$cgi->Vars; print "content-type: text/html\n\n"; print "ALL FORM PARAMETERS ARE:


"; foreach $i (keys %paras) { print "
$i=",$paras{$i}; }

http://localhost/ip/sale.htm 23 Java Basics

„ What are classes?

„ Blueprint or prototype defining variables and methods common to all objects of a certain kind „ What is an object?

„ A software bundle of variables and related methods

„ An instance of a class „ Every Java program is a class def.

24 Class Definition

Modifier class ClassName { public class Demo { variable declarations/initiation; private int i; method declaration/definition; public void set(int x) { more of such declarations i=x; } } int get() { return i; } } „ Modifiers: public, protected, private, abstract

25 Simple Class Example Fields va ria public class Group { bl public long id; es public String name; public Group stem; public Group root; public static long nextID = 1; } public Group(){}; Default constructor 26 Primitives & Type Casting

„ Primitive types are of fixed size „ Class variables defaulted, method variables must be initialised „ Auto type cast from smaller to larger

„ byte

„ char

„ All operands first promoted to richest type

„ To int if smaller than int „ All primitive but boolean, char, void are signed „ Array: int [ ] x= { 1, 2, 3 }; Integer [ ][ ] y; „ Logical operators and values

„ true, false, ==, !=, >, <, >=, <=, &&, ||, !

„ Obj instanceof ClassName „ No goto, limited use of label new op Better structured 28 "static" "final" Keywords

„ static storage contains data available for entire time a program is runs „ Located in a “fixed location” „ static variables/methods can be accessed without via an object

„ Math.PI, Math.random() in class Math „ Constants defined by final

No pre- „ E.g. public static final int VALUE=10; processor No #define 29 Static Membership

class Dumb { Class members referred int i; by “.” static String x; ObjName.MethodName( int f() { return 0; } arg1,…,argN) }

„ Dumb.x - static member referred to without a=new an object (“fixed location”) Dumb(); „ if a is an object of Dumb then a.i, a.x, a.f() are legal reference to the members

30 Memory Management

„ All memory allocation made via keyword new

„ Memory de-allocation done via automatic garbage collection

„ Order and time unpredictable

„ May explicitly request via System.gc()

„ finalize() of an object called before being garbage collected

„ No malloc(), free(), compared with C

31 Library Packages

„ Auto-linked library package java.lang

„ Object, Math, String, System, Thread

„ Standard output System.out.println(TextString); Online API „ Package java.util, document „ Class System belongs to java.lang

„ Random, Date, Arrays, Vector

32 Objects

„ Create an object: Use new operator Dumb d1 = new Dumb();

d1 is an object leaf „ Java Runtime will create enough space to store the fields or new throws OutofMemoryError Root Trunk

33 Constructor

„ A constructor - normally for initialization

„ has the same name as the class

„ has no return type

„ is called when an object is created

„ A default constructor is called if there is no constructor given

„ default constructor, if needed, must be declared if constructor is defined for other input parameters

34 public class RandomNumbers { /* make 10 random chars */ /** just a constant below */ public static final int LENGTH=10;

static Store [ ] array=new Store[ LENGTH ]; Entry point /** entry point to the class */ from OS public static void main(String[ ] args) { for( int i=0; i

35 Methods Overloading

„ Multiple definitions for a method with the same name, but different arguments (signatures) „ If the number of arguments is the same, at least one argument must have a distinct type int check( int i); int check( float i); int check( int i, float j); -- signature=(int,float)

36 Multiple Constructors

„ Same name with different signatures „ A constructor can call another from the same class, using this() invocation „ Which constructor is called depends on the signature toString method in any class: object.toString() returns a string describing the object 37 Example class A { // "A" is the class name int i; // initialized to 0 String s; // initialized to null A() { i=1; } // constructor A(int x) {i=x; } // overloaded constructor String toString() { return "I’m A"; } public static void main(String[] args) { String ss; // not initialized A a=new A(9); System.out.println(a.i+a.s+a); } } // output: 9nullI’m A 38 Java Applets

„ Designed to run within a web browser „ Applets can't touch the local disks „ Applets can take longer to display „ No installation issues (platform independent) „ Meant to be included in web pages „ Any subclass derived from the class java.applet.Applet defines an applet „ any subclass of JApplet also defines an applet

39 Simplest Applet import javax.swing.JApplet; import java.awt.Graphics; public class WelcomeApplet extends JApplet { public void paint( Graphics g ) { g.drawString("Wecome to Java!", 25,25 ); }}

„ applet is updated by the paint method „ paint method uses a Graphics object g (passed automatically to the method) to draw on the screen 40 WelcomeApplet.html Webpage for an Applet

„ width, height specify the size of the applet „ may use appletviewer WelcomeApplet.html to view the applet „ If a browser is used to display WelcomeApplet.html, it may not give the result appletviewer gives. This is because additional steps have to be taken to ensure Swing is properly understood. 41 TicTacToe Applet

„ Several classes are needed „ Best make use of JAR tool 42 Reading

„ Core Web Programming

„ Part I – html

„ Chapter 6

„ Unit Homage Password

„ ip/ip789 –initially

„ Will be deleted later on

„ Register and use your own

43 Thoughts for Next Week

„ How does Apache web server control the web access?

„ How does Microsoft IIS control the web access?

44