UMBC CMSC 331 Java Introduction to JAVA CMSC 331.

Slides:



Advertisements
Similar presentations
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
Advertisements

Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Object Orientated Programming
IC211 Object Oriented Programming Overview of Java.
Java: History and Introduction (Lecture # 1). History… Java – Based on C and C++ – Developed in 1991 for intelligent consumer electronic devices – Green.
Introduction to Java Kiyeol Ryu Java Programming Language.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
1 CS6320 – Why Servlets? L. Grewe 2 What is a Servlet? Servlets are Java programs that can be run dynamically from a Web Server Servlets are Java programs.
Introduction to Servlet & JSP
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
01 Introduction to Java Technology. 2 Contents History of Java What is Java? Java Platforms Java Virtual Machine (JVM) Java Development Kit (JDK) Benefits.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Getting Started What is Java? A programming language –Fully buzzword-compliant: A simple, object oriented, distributed, interpreted, robust, secure,
Server-side Technologies
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Applets & Servlets.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Java Workshop for Teachers May 6, 2005 A Brief Look at the Java Programming Language.
Introduction to JAVA Instructor : Simon Hui & Louis Wong Industrial Centre The Hong Kong Polytechnic University Sep, 2002.
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
Java Lecture 16: Dolores Zage. WWW n Was a method for distributing passive information n added forms and image maps n interaction was only a new way to.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
POS 406 Java Technology And Beginning Java Code
The Joy of Programming (also known as) Introduction to Object-Oriented Programming.
Chapter 1 Section 1.1 Introduction to Java Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
Java applets. Outline What is Java? Java Applications Java Applets Java Applets Securities Summary.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
We will talking about story of JAVA language. By Kristsada Songpartom.
Java and its Evolution. Contents Java Introduction Java Features How Java Differs from other OO languages Java and the World Wide Web Java Environment.
Web Pages with Features. Features on Web Pages Interactive Pages –Shows current date, get server’s IP, interactive quizzes Processing Forms –Serach a.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
Java Servlets and Java Server Pages Norman White Stern School of Business.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
Introduction Mehdi Einali Advanced Programming in Java 1.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
Java Servlets and Java Server Pages
How CGI and Java Servlets are Run By David Stein 14 November 2006.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
Distributed Web Systems Java Servlets Lecturer Department University.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Fundamental of Java Programming (630002) Unit – 1 Introduction to Java.
Java Server Pages Can web pages be created specially for each user?
Applications Active Web Documents Active Web Documents.
Servlet Fudamentals.
Introduction to Servlets
Java Applets.
Introduction to JAVA CMSC 331.
Chap 1. Getting Started Objectives
Outcome of the Lecture Upon completion of this lecture you will be able to understand Fundamentals and Characteristics of Java Language Basic Terminology.
Presentation transcript:

UMBC CMSC 331 Java Introduction to JAVA CMSC 331

UMBC CMSC 331 Java 2 Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java applets, which run within browsers e.g. Netscape –Java servlets, which run with a web server Example programs tested using Java on Windows and/or Unix

UMBC CMSC 331 Java 3 Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent (except for J++) –this makes it great for Web programming It’s designed to support Internet applications It’s secure It isn’t C++

UMBC CMSC 331 Java 4 Important Features of Java Java is a simple language (compared to C++). Java is a completely object-oriented language. Java programs can be multi-threaded. Java programs automatically recycle memory. Java is a distributed and secure language. Java is robust (potential errors are often caught). To make Java portable, so that they run on a variety of hardware, programs are translated into byte code which is executing by a Java Virtual Machine.

UMBC CMSC 331 Java 5 Historical notes ~1991, a group at Sun led by James Gosling and Patrick Naughton designed a language (code-named “Green”) for use in consumer devices such as intelligent TV “set-top” boxes and microwaves. –The design choices reflected the expectation of its use for small, distributed, and necessarily robust programs on a variety of hardware. –No customer was ever found for this technology. ~1993, Green was renamed “Oak” (after a tree outside Gosling’s office) and was used to develop the HotJava browser, which had one unique property: it could dynamically download programs (“applets”) from the Web and run them. “Oak” was already taken as a name for a computer language, so Gosling thought of the name Java in a coffee shop.

UMBC CMSC 331 Java 6 What is OOP? Object-oriented programming technology can be summarized by three key concepts: –Objects that provide encapsulation of procedures and data –Messages that support polymorphism across objects –Classes that implement inheritance within class hierarchies More on this later!

UMBC CMSC 331 Java 7 Applets, Servlets and applications An applet is a program designed to be embedded in a Web page and run in a web browser –Applets run in a sandbox with numerous restrictions; for example, they can’t read files A servlet is a program which runs in a web server and typically generates a web page. –Dynamically generated web pages are important and Java servlets are an alternative to using Basic (ASP), Python, specialized languages (PHP), and vendor specific solutions (e.g., Oracle) An application is a conventional program Java isn't a baby language anymore!

UMBC CMSC 331 Java 8 Building Standalone JAVA Programs (on UNIX) Prepare the file myProgram.java using any editor Invoke the compiler: javac myProgram.java This creates myProgram.class Run the java interpreter: java myProgram

UMBC CMSC 331 Java 9 Java Virtual Machine The.class files generated by the compiler are not executable binaries –so Java combines compilation and interpretation Instead, they contain “byte-codes” to be executed by the Java Virtual Machine –other languages have done this, e.g. UCSD Pascal, Prolog This approach provides platform independence, and greater security

UMBC CMSC 331 Java 10 HelloWorld Application public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } Note that String is built in println is a member function for the System.out class Every standalone Java application must have a main method like public static void main(String[] args) { }

UMBC CMSC 331 Java 11 HelloWorld Application => cd java => ls HelloWorld.java => more HelloWorld.java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } => javac HelloWorld.java => java HelloWorld Hello World! =>

UMBC CMSC 331 Java 12 Java Applets The JAVA virtual machine may be executed under the auspices of some other program, e.g. a Web browser or server. Bytecodes can be loaded off the Web, and then executed locally. There are classes in Java to support this

UMBC CMSC 331 Java 13 Another simple Java program public class Fibonacci { public static void main(String args[ ]) { int first = 1; int second = 1; int next = 2; System.out.print(first + " "); System.out.print(second + " "); while (next < 5000) { next = first + second; System.out.print(next + " "); first = second; second = next; } System.out.println( ); }

UMBC CMSC 331 Java 14 Still another simple program /** * This program computes and prints the factorial of a number pass as an argument on the command line. Usage: java Factorial */ public class Factorial { // Define a class public static void main(String[] args) { // The program starts here if (!(args.length==1)) { // Right number of args? System.out.println("Usage: java Factorial "); } else { int input = Integer.parseInt(args[0]); // Get the user's input System.out.println(factorial(input)); // Call facorial method, print result } } // The main() method ends here public static double factorial(int x) {// This method computes x! if (x<0) return 0.0; // if input's bad, return 0 double fact = 1.0; // Begin with an initial value while(x>1) { // Loop until x equals 1 fact = fact*x; // multiply by x each time x = x-1; // and then decrement x } // Jump back to the star of loop return fact; // Return the result } // factorial() ends here } // The class ends here

UMBC CMSC 331 Java 15 Building Applets Prepare the file myProgram.java, and compile it to create myProgram.class Two ways to run applet program: –Invoke an Applet Viewer (e.g. appletviewer) on windows or unix and specify the html file –Using a browser (e.g., IE or Netscape), open an HTML file such as myProgram.html Browser invokes the Java Virtual Machine

UMBC CMSC 331 Java 16 HelloWorld.java import java.applet.*; public class HelloWorld extends Applet { public void init() { System.out.println("Hello, world!"); }

UMBC CMSC 331 Java 17 hello.html Hello, World

UMBC CMSC 331 Java 18 Running the Applet [3:43pm] linuxserver1 => pwd /home/faculty4/finin/www/java [3:43pm] linuxserver1 => ls HelloWorld.java hello.html [3:43pm] linuxserver1 => javac HelloWorld.java [3:43pm] linuxserver1 => ls HelloWorld.class HelloWorld.java hello.html

UMBC CMSC 331 Java 19 But that’s not right! The “Hello, World” was displayed by the HTML H1 tag… What’s going on? Applets do their output in a much more complicated way, using graphics. When an applet is loaded, by default, the paint method is called

UMBC CMSC 331 Java 20 Here’s a working version import java.applet.*; import java.awt.Graphics; import java.awt.Font; import java.awt.Color; public class HelloWorldApplet extends Applet { public void init() { System.out.println("Hello, world!"); } Font f = new Font("TimesRoman", Font.BOLD, 12); public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString("Hello Again!", 1, 10); }

UMBC CMSC 331 Java 21 Java Servlets Most interesting web applications provide services, which requires invoking programs. More and more of the web consists of pages that are not statically created by human editors, but dynamically generated when needed by programs. How do we invoke these programs and what programming languages should we use? –CGI: Common Gateway Interface –Web servers with built in support for servlets written in Python, Lisp, Tcl, Prolog, Java, Visual Basic, Perl, etc. – ASP (Active Server Pages) is a scripting environment for Microsoft Internet Information Server in which you can combine HTML, scripts and reusable ActiveX server components to create dynamic web pages. –ASP begat PHP, JSP, … Java turns out to be an excellent language for servlets

UMBC CMSC 331 Java 22 A Servlet’s Job Read any data sent by the user –From HTML form, applet, or custom HTTP client Look up HTTP request information –Browser capabilities, cookies, requesting host, etc. Generate the results –JDBC, RMI, direct computation, legacy app, etc. Format the results inside a document –HTML, Excel, etc. Set HTTP response parameters –MIME type, cookies, compression, etc. Send the document to the client

UMBC CMSC 331 Java 23 Why Build Web Pages Dynamically? The Web page is based on data submitted by the user –E.g., results page from search engines and order-confirmation pages at on-line stores The Web page is derived from data that changes frequently –E.g., a weather report or news headlines page The Web page uses information from databases or other server-side sources –E.g., an e-commerce site could use a servlet to build a Web page that lists the current price and availability of each item that is for sale.

UMBC CMSC 331 Java 24 The Advantages of Servlets Over “Traditional” CGI Efficient –Threads instead of OS processes, one servlet copy, persistence Convenient –Lots of high-level utilities Powerful –Sharing data, pooling, persistence Portable –Run on virtually all operating systems and servers Secure –No shell escapes, no buffer overflows Inexpensive

UMBC CMSC 331 Java 25 Simple Servlet Template import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletTemplate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Use "request" to read incoming HTTP headers // (e.g. cookies) and HTML form data (query data) // Use "response" to specify the HTTP response status // code and headers (e.g. the content type, cookies). PrintWriter out = response.getWriter(); // Use "out" to send content to browser }

UMBC CMSC 331 Java 26 HelloWorld Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); }

UMBC CMSC 331 Java 27 Summary Java is an object-oriented programming language. Java features make it ideally suited for writing network- oriented programs. Java class definitions and the programs associated with classes are compiled into byte code, facilitating portability. Java class definitions and the programs associated with them can be loaded dynamically via a network. Java programs can be multithreaded, thereby enabling them to perform many tasks simultaneously. Java does automatic memory management, relieving you of tedious programming and frustrating debugging, thereby increasing your productivity. Java has syntactical similarities with C and C++.