introduction to java instructor : simon hui & louis wong industrial centre the hong kong...

44
Introduction Introduction to to JAVA JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Upload: hillary-baldwin

Post on 26-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Introduction toIntroduction to JAVA JAVA

Instructor : Simon Hui & Louis Wong

Industrial Centre

The Hong Kong Polytechnic University

Sep, 2002

Page 2: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

PhysicsPhysics

Optics

http://158.132.155.24/javaintro/physics/physics_1.htm

Plane Pendulum

http://158.132.155.24/javaintro/physics/physics_2.htm

Interference

http://members.tripod.com/~vsg/interfer.htm

Page 3: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

MathematicsMathematics

Javasketchpad

http://home.netvigator.com/~wingkei9/javagsp/javagsp.html

E.g the graph of y=ax2+bx+c Manipulate Math with Java

http://www.ies.co.jp/math/java/index.html

E.g Calculus Derivative of y=x2

Page 4: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

ChemistryChemistry

Thermochemistry and the microscopic behavior of molecules

http://mc2.cchem.berkeley.edu/Java/

E.g Molecules in Motion Orientation of d-orbitals

http://wwwchem.uwimona.edu.jm:1104/courses/CFTpt2.html

Page 5: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

OverviewOverview

The growth in the popularity of the Internet.

Internet application. E.g. Client and server programming.

Request middleware to communicate between clients and database.

Need Active content. Specific browser configuration.

Page 6: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

What is Java?What is Java?

A programming language. Well suited for use with the Internet. Create an active and dynamic content

web pages. Client/server programming.

Page 7: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

What is Java?What is Java?

Used for middleware to communicate between client and server.

Write once run anywhere. Similar to C++, C.

Page 8: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java FeaturesJava Features

SimpleObject-OrientedDistributedMultithreadedSecurePortableFree of charges

Page 9: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Advantages of JavaAdvantages of Java

Platform independenceSimilar to syntax of C++Fully object oriented (multiple

inheritances)SecureDynamicMultithreaded

Page 10: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java vs JavaScriptJava vs JavaScript

For various programming.

A compiled language. Compiled into Applet

(separate from HTML). Programming language

Web programming only. Not a compiled

language. Embedded directly in

HTML. Scripting language.

Page 11: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java Virtual MachineJava Virtual Machine Developed by Sun MicrosystemsRun Java ProgramAct as an interface between Java binary

code and the microprocessor. Installed in local computer.

Page 12: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java Virtual MachineJava Virtual Machine Compiled the Java program to

bytecode.The bytecode can run on any platform.Virtual Machine interprets the bytecode

one instruction at a time.

Page 13: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Advantages of Java Virtual Advantages of Java Virtual MachineMachineJava code is really small, ideal for

transmission over the network.Programmers can design it as simple as

they want.Platform independence

Page 14: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java ApplicationJava Application

Stand-alone programNo initial windowE.g *.exe (Dos/windows)

Page 15: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java Application (Example)Java Application (Example)

Line 1 public class JavaApplication {

Line 2 public static void main(String[] args){

Line 3 System.out.println("Welcome to Java Online Course!");

Line 4 }

Line 5 }

Page 16: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java AppletJava Applet

Run within browserDefined in java.applet package which

contains a set of objects.Example

http://www.cityline.com.hk

Page 17: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java Applet (Example)Java Applet (Example)

JAVA Source Code

Line 1 import java.awt.*;

Line 2 import java.applet.Applet;

Line 3 public class JavaApplet extends Applet{

Line 4 public void paint(Graphics g) {

Line 5 g.drawString("HelloWorld",25,50);

Line 6 }

Line 7 }

HTML Code

<Applet code=“JavaApplet.class” Width=200 Height=200 file=“abc.jpg”>

</Applet>

Page 18: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java Applet (HTML Interface)Java Applet (HTML Interface)

HTML may pass information to Java via applet tag.

HTML:

<Applet code=“JavaApplet.class” Width=200 Height=200 file=“abc.jpg”>

</Applet>

Java:

getParameter(“file”);

getParameter(“width”);

Page 19: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

General Web Architecture for General Web Architecture for Web-based ApplicationWeb-based Application

Page 20: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Middle tierMiddle tier

Presentation Layer– Generates web pages, including dynamic

content.– Interprets web pages submitted from the

client. Business Logic Layer

– Performs validations and handles interaction with the database.

Page 21: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Client Layer and Data LayerClient Layer and Data Layer

Client Layer– Renders web pages.

Data Layer– Stores data between transactions.

Page 22: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java 2 Enterprise EditionJava 2 Enterprise Edition

Page 23: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java 2 Enterprise EditionJava 2 Enterprise Edition Released in mid of 1999. Provide a comprehensive view of Java middle

tier solution. Implement Presentation Layer.

– Servlets– JavaServer Pages (JSP)

Implement Business Logic layer.– JavaBeans

Provide for access by non-web clients.– J2EE’s Remote Method Invocation (RMI)

Page 24: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

What is CGI?What is CGI?

Create dynamic content of web pages. Execute in real-time by the web server.

Page 25: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Implementing CGIImplementing CGI

C C++ Perl ASP (Active Server Page)

Page 26: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

What is Servlets?What is Servlets?

Released in 1997. Create web pages with dynamic content Extend the capabilities of Web servers. Standard, server-side Java application

that extends the capabilities of a Web Server.

Page 27: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

What is Servlets?What is Servlets?

A replacement for CGI scripts. Run inside the Java Virtual Machine on

the server. Run on all major web servers. Do not require support for Java in the

browser. E.g. https://ebanking.hangseng.com/

Page 28: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

How to create web page with How to create web page with dynamic content?dynamic content? Parse/decode the user-submitted HTML

form, handle cookie, etc. Access database directly. Derive their dynamic content via

Enterprise Java Beans (runs on an application server).

Page 29: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Advantages of ServletsAdvantages of Servlets

Portability and flexibility Security Performance

Page 30: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

ExampleExampleimport java.io.*;import java.text.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*; public class HelloWorldExample extends HttpServlet { ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,

ServletException{response.setContentType("text/html");PrintWriter out = response.getWriter(); out.println("<html>");out.println("<body>"); out.println("<head>");out.println("<title>Hello World!</title>");out.println("</head>");out.println("<body>");out.println("<h1>Hello World!</h1>");out.println("</body>");out.println("</html>");}}

Page 31: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

What is JavaServer Page?What is JavaServer Page?

Released in 1999. Provides another Java-based solution

for generating dynamic web pages. Is an extension of the Java Servlet. Mix static HTML with server-side

scripting.

Page 32: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

What is JavaServer Page?What is JavaServer Page?

Uses XML-like tags and scriptlets written in the Java programming.

No a single line of Java code. Example

http://www.citibank.com.hk/cgi-bin/bv.cgi/eng/index.jsp

Page 33: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

ExampleExampleLine1 <%@ page session=false %>

Line2 <%

Line3 String title = "Hello, world!";

Line4 %>

Line5 <head>

Line6 <title><%= title %></title>

Line7 </head>

Line8 <body bgcolor=white>

Line9 <h1><%= title %></h1>

Line10 </body>

Page 34: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

ExampleExample

Surrounds by <% %> Provides a number of server-side tags. More simpler than Java servlets

Page 35: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Flow of JSP modelFlow of JSP model A request is sent directly to a JSP page. The JSP engine automatically creates,

compiles, and executes servlets to implements the behavior of the combined HTML and embedded Java code.

Page 36: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Flow of JSP modelFlow of JSP model JSP code can control interactions with

JavaBeans components for business and data logic processing

Display the results in dynamically generated HTML mixed with a static HTML code.

Page 37: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Difference between JSP and Difference between JSP and Servlets?Servlets? Translate the JSP into a Servlet. Put HTML and JSP code together and

let web server do the translation to servlet code.

Page 38: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java’s VersionJava’s Version

Version No. of classes

No. of Packages

Released Comment

1.0 212 8 Jan 1996

1.1 504 23 Feb 1997

1.2 1520 59 Dec 1998 Called “Java 2, Release 1.2”

1.3 1840 76 May 2000 Called “Java 2, Release 1.3,

Standard Edition”

Page 39: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java’s EditorJava’s Editor

SUN Microsoft

Page 40: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java’s Editor: SUNJava’s Editor: SUN

Java Enterprise Edition (J2EE)– Large Enterprise Web applications

Java Standard Edition (J2SE)– Standard Java applet and application

Java Micro Edition (J2ME)– For running on small or embedded consumer

products, including pagers, cellular phones, digital set-top boxes and etc.

Page 41: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Java’s Editor: MicrosoftJava’s Editor: Microsoft

Visual Java ++ 6.0– Introduces the window Foundation Class

for Java (WFC)– Is a new application framework accesses

the Microsoft Window API– Writes full-features windows application

with the Java programming language.

Page 42: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Resources for developmentResources for development

Java Boutique http://javaboutique.internet.com

Java Resources – The complete webmaster

http://abiglime.com/webmaster/articles/java.htm

JPowered.com http://www.jpowered.com

Page 43: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Educational ResourcesEducational Resources Interactive Physics and Math with Java

http://www.lightlink.com/sergey/java/index.html

Molecular dynamic simulation with Java http://ws05.pc.chemie.tu-darmstadt.de/research/molcad/java

Constructive and Destructive Wave Interference http://ciu.ic.polyu.edu.hk/course/offline/Wave%20Interference.htm

NTNU Virtual Physics Laboratory http://www.phy.ntnu.edu.tw/class/demolab/indexTree.html

Page 44: Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002

Game writing in JavaGame writing in Java

Maze in 3D http://users.nbn.net/%7Emr_g/java/Maze3D/arcade/maze/maze.htm

Snapple Good Fruit! Bad Fruit http://www.snapple.com/play/fruit.htm