440 likes | 488 Views
Introduction to JAVA. Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002. Physics. Optics http://158.132.155.24/javaintro/physics/physics_1.htm Plane Pendulum http://158.132.155.24/javaintro/physics/physics_2.htm Interference
E N D
Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002
Physics • 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
Mathematics • 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
Chemistry • 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
Overview • 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? • A programming language. • Well suited for use with the Internet. • Create an active and dynamic content web pages. • Client/server programming.
What is Java? • Used for middleware to communicate between client and server. • Write once run anywhere. • Similar to C++, C.
Java Features • Simple • Object-Oriented • Distributed • Multithreaded • Secure • Portable • Free of charges
Advantages of Java • Platform independence • Similar to syntax of C++ • Fully object oriented (multiple inheritances) • Secure • Dynamic • Multithreaded
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 vs JavaScript
Java Virtual Machine • Developed by Sun Microsystems • Run Java Program • Act as an interface between Java binary code and the microprocessor. • Installed in local computer.
Java 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 Machine • Java code is really small, ideal for transmission over the network. • Programmers can design it as simple as they want. • Platform independence
Java Application • Stand-alone program • No initial window • E.g *.exe (Dos/windows)
Java Application (Example) Line 1 public class JavaApplication { Line 2public static void main(String[] args){ Line 3 System.out.println("Welcome to Java Online Course!"); Line 4} Line 5 }
Java Applet • Run within browser • Defined in java.applet package which contains a set of objects. • Example http://www.cityline.com.hk
Java Applet (Example) JAVA Source Code Line 1import java.awt.*; Line 2import java.applet.Applet; Line 3public class JavaApplet extends Applet{ Line 4public void paint(Graphics g) { Line 5g.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) • 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”);
Middle 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 Layer • Client Layer • Renders web pages. • Data Layer • Stores data between transactions.
Java 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? • Create dynamic content of web pages. • Execute in real-time by the web server.
Implementing CGI • C • C++ • Perl • ASP (Active Server Page)
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? • 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 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 Servlets • Portability and flexibility • Security • Performance
Example import 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? • 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? • 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
Example Line1 <%@ 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>
Example • Surrounds by <% %> • Provides a number of server-side tags. • More simpler than Java servlets
Flow 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 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 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 Editor • SUN • Microsoft
Java’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: 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 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 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 Interferencehttp://ciu.ic.polyu.edu.hk/course/offline/Wave%20Interference.htm • NTNU Virtual Physics Laboratoryhttp://www.phy.ntnu.edu.tw/class/demolab/indexTree.html
Game 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