Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic...

Post on 26-Dec-2015

218 views 1 download

Tags:

Transcript of Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic...

Introduction toIntroduction to JAVA 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

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

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

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.

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.

What is Java?What is Java?

Used for middleware to communicate between client and server.

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

Java FeaturesJava Features

SimpleObject-OrientedDistributedMultithreadedSecurePortableFree of charges

Advantages of JavaAdvantages of Java

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

inheritances)SecureDynamicMultithreaded

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.

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.

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.

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

Java ApplicationJava Application

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

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 }

Java AppletJava Applet

Run within browserDefined in java.applet package which

contains a set of objects.Example

http://www.cityline.com.hk

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>

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”);

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

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.

Client Layer and Data LayerClient Layer and Data Layer

Client Layer– Renders web pages.

Data Layer– Stores data between transactions.

Java 2 Enterprise EditionJava 2 Enterprise Edition

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)

What is CGI?What is CGI?

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

Implementing CGIImplementing CGI

C C++ Perl ASP (Active Server Page)

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.

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/

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).

Advantages of ServletsAdvantages of Servlets

Portability and flexibility Security Performance

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>");}}

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.

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

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>

ExampleExample

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

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.

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.

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.

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”

Java’s EditorJava’s Editor

SUN Microsoft

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.

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.

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

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

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