Lesson 1.1 Java and the Web
Introduction to Java
In this lesson you'll get a quick overview of Java as well as a peek at the Internet and the World Wide Web.
Along with this lesson, you should also read the Syllabus which tells you:
- what you need to do to successfully complete the course.
- what software and textbook you'll need.
- the schedule for reading, homework, and quizzes.
In this lesson you'll learn:
- what is meant by "the Java platform."
- why Java is important.
- why Computer Science students should learn Java.
- some Internet and World Wide Web history and vocabulary.
Let's start by answering the question, "What is Java?"
Just What IS Java?
Java is probably one of the most controversial, confused, and misused terms in the entire field of Computer Science. Sun Microsystems, the developers of Java, hoping to alleviate this confusion, have provided a simple definition; according to Sun, Java is :
“A Simple, Object-oriented, Distributed, Interpreted, Robust, Secure, Architecture-neutral, Portable, High-performance, Multithreaded, Dynamic Programming Language”
Instead of walking through each of these "buzzwords", let's take a look at the three faces of Java.
The "Three Faces" of Java
A well-known Indian legend tells the story of seven blind men describing an elephant. The one who felt the trunk thought the elephant was like a snake, the one who felt the legs thought the elephant was like a tree, and so on.
Java is a little bit like that; you'll often hear people say things like:
- "Java is slow" or, less often, "Java is as fast as C++"
- Java programs look "strange"
- Java is better for building Web applications than Microsoft .NET
- Microsoft .NET supports Java along with other programming languages
- Java is cross-platform while VB-script only works in Internet Explorer
Each of these statements, except the last, contains a kernel of truth; each also suffers from confusion between the three parts of the Java platform. In reality, Java is:
- an object-oriented programming language. The Java language, like C++ or SmallTalk, can be supported on different runtime systems, like Microsoft's .NET.
- an interpreter-based runtime system called the Java Virtual Machine or JVM. There are several differnt kinds of JVM's. Some use an interpreter, some use a Just In Time compiler [JIT].
- a set of universally distributed classes called the Java Class Libraries. The class libraries provide support for GUI programming, network and file I/O, etc.
The combination of all three of these are called a Java platform. The most commonly used Java Platform is supplied by Sun [called the JRE], but there are also versions supplied by Apple, Microsoft, IBM, HP, and so on.
JavaScript
JavaScript is often confused with Java, but they are really not related at all. JavaScript is a programming language designed by Netscape, used to manipulate Web documents and Web browsers. JavaScript was originally named LiveWire, but was renamed by Netscape when Java became popular.
JavaScript programs are downloaded to your Web browser, often embedded inside an HTML program, and the instructions are executed by the Web browser. JavaScript and Java are two entirely different languages, used for entirely different purposes. JavaScript can be used to control your Web browser, while Java cannot.
Types of Java Programs
There are three major types of Java programs: applets, applications, and servlets.
Applets
Java applets are Java programs that are "hosted" inside a Web page. Here's how Java applets work:
- The compiled machine code for your program is stored on a Web server.
- The code is automatically downloaded when a user visits your Web page.
- The user's Web brower loads a Java Virtual Machine when it encounters a Java applet, and then runs the applet inside the browser's JVM. This is similar to the way Flash/Shockwave animation, or other plugins are handled.
- For safety sake, Java applets are prohibited from performing actions that might damage a user's machine if used maliciously. Applets cannot read and write files on the user's local machine, for instance.
Sometimes, you'll hear this called "client-side" Java. In this course, most of your homework assignments will consist of Java applets.
Applications
Java applications are programs that are installed and run on the user's local machine, in the same way you would install and run a program written in Visual Basic or C++. There are two kinds of Java applicatons:
- Console-mode applications - These are traditional "teletype-style" programs similar to those you'd write in a Pascal or C++ class.
- GUI applications - These graphical programs use windows, buttons, and mice to create the kind of interactive programs you're used to.
Java actually contains two different GUI libraries, the original AWT [Abstract Window Toolkit] which uses your computer's native look-and-feel, and the JFC [Java Foundation Classes], which provides a set of advanced components that can be made to look and feel the same on all platforms.
Server-Side Java
A third kind of Java program is called a server-side application. Server-side applications are not downloaded or installed on the user's computer. Instead, the Java program runs on an Application Server, and only the input or output is sent to the user's machine.
Server-side applications are most often accessed through a Web browser, and are used for ecommerce and other interactive Web sites. We won't do any server-side Java in this course.
Why is Java Important?
How is Java different from other programming languages you might use, like Visual Basic, Pascal, or C++? The biggest difference is the idea of cross-platform binary portability.
A portable language is one that is designed to run on different computer operating systems or different computer hardware. The C language is portable, for instance, because you can run the same program on both your Windows computer and your Macintosh, by taking your C-language source code and recompiling it with a different C compiler on each machine.
This is called source code portability, and it only works if:
- there is a compiler or interpreter available on each platform.
- the program uses no platform-dependent facilities.
C and C++ were designed to be portable. Languages like Visual Basic, or AppleScript, or Intel assembly language, were not.
WORA
Java programs are designed to offer more than source-code portablility; Sun's goal for the Java Platform is summed up in the "Pure Java" refrain called WORA, or: “Write Once: Run Anywhere”
With WORA, you should be able to compile your Java program on a Mac and then run it, with no changes at all, on a Windows or Unix computer. This is possible because the JVM installed on each platform understands the same byte-code, and because every Java platform has the same classes available.
Problems with WORA
The major problem with Java is that “WORA” really isn't perfect; each implementation of the Java platform has subtle differences, and so developers ended up with: “Write Once: Test Everywhere”
There are several reasons for this:
- AWT GUI performance depends on native peers. In other words, Button objects on the Mac act differently from Button objects on Windows or Unix. Sun changed the classes in the platform but the different browers didn't keep up.
- Sun and Microsoft exhibited a failure to "play well together."
In addition to the WORA failings, Java speed is still definitely an issue. Java GUI apps use more memory and perform more slowly than C++ programs.
Why CS Students Should Learn Java
If Java programs are harder to test than C++ programs, and they run slower, then why not just learn C++ to start with, instead of messing with Java? There are several reasons:
- Java is less complex than the alternatives Java is widely used in University 4-year Computer Science (CS) programs.
- Java lets you build graphical applications easily.
- The tools are simple and free.
Abstraction
One of the major goals of CS education is to teach you to use abstraction. Abstraction is the ability to strip away the non-essential details in a problem, and concentrate on the essential elements of the problem to craft your solution. Java is a modern, object-oriented language that lets you work at a higher level of abstraction than C or C++, or even C#.
For instance, if you are writing an applet in Java to create an "OK" Button object and add it to your program, you can write:
In C++ [using the Microsoft Foundation Classes for Windows] you have to write:
CButton btn;
btn.Create("OK",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
50, 50, 50, 35,
this,
IDOK); |
As you can see, there's just a whole lot more "stuff" [what we call "accidental complexity"] you have to understand, just to add an OK button to your program. In C#, things are not much better; here's an example adapted from Jesse Liberty's "Programming C#", page 316: [I've actually simplified it.]
Button btn = new Button();
btn.Location = new Point(50,50);
btn.Size = new Size(50,35);
btn.Text = "OK";
this.Controls.Add(btn); |
Later in the course , you'll start to study the Java programming language and class libraries. Before you do, though, you're going to learn a little about the Internet and the World Wide Web.
The Internet and the Web
Most of you know what a network is: a collection of computers that can communicate with each other.
The Internet is like a local network, except that:
- the Internet is world-wide instead of being constrained to a local building or even a campus. The Internet connects different networks, rather than simply connecting different computers.
- all of these different networks must use a specific electronic communication code (called the TCP/IP protocol ) when they communicate over the Internet, even if they use a different protocol [Appletalk, IPX, etc.] internally.
Clients and Servers
When you connect to the Internet via your Internet Service Provider (ISP), your local computer is called a client. Many of the computers that are connected to the Internet are called servers. A server (in the Internet sense) is not a special brand or kind of computer; instead, it means that the computer is running a software program that listens for requests from other computers and then attempts to fullfill those requests.
What kind of program listens for requests, and what kind of services are available? Here are the most common ones:
Telnet
A Telnet server allows you to connect to the machine running the service and to use the machine exactly as if you were sitting at the console. Telnet is common in the UNIX world, where each computer is expected to have many users. It's less common in the Mac and PC world, where most machines are single user.
FTP
A File-Transfer-Protocol server allows you to move files between different machines, even if both machines are using different operating systems. Using FTP, it's easy to transfer files between UNIX, Mac, and PC machines over the Internet when it would be difficult--if not impossible--to directly transfer the files.
SMTP
Most of us take global email for granted, but just a few short years ago there were several incompatible email systems, restricted to exchanging mail between local clients on the same local area network. Those proprietary email systems haven't entirely disappeared, but all of them have had to make arrangements to deal with SMTP: Simple Mail Transport Protocol.
HTTP
The Hyper Text Transfer Protocol is the basis for the World Wide Web. HTTP is a "language" used to communicate between Web servers and Web clients.
The World Wide Web
Tim Berners-Lee, the Father of the Web, (and first Duke of URL), invented HTTP, the HyperText Transfer Protocol, while working as a researcher at the European Center for Partical Physics [CERN]. Like many who have had a great impact on the world, Berners-Lee simply wanted to find an easier way to get his work done. In this case, he wanted an easier way to exchange documents with other researchers and academics.
If you've ever read an academic paper, you know that they are literally peppered with references and citations: it's how you get "graded" in that profession. Unfortunately, it makes for somewhat difficult reading because just when you've gotten to the interesting part, there's one of those darn footnotes, and you, being the conscientious type, have to plod down to the library, look up the reference, and then plod back to where you left off, by which time you've forgotten all about what the author was originally trying to say, if you ever cared in the first place. Whew! Berners-Lee's idea was to store the documents on a central computer. When you retrieved the document, all of those pesky references would be translated into hyperlinks. As you read through the document and encountered a reference, rather than breaking your train of thought, you could just select the link and instantly view the reference.
HTTB: Hypertext TidBits
For Berners-Lee's vision to come to pass, there were several different problems that needed to be solved:
- Finding a particular document
- Encoding the document
- Transporting the document
Here are the pieces Berners-Lee designed to solve these problems.
Location, Location, Location
Since the actual documents Berners-Lee wanted to read could be located anywhere in the world, every document had to be given a unique identifier. At first, a scheme was proposed where every document would be branded with an incomprehensible very large number called a GUID.
The idea Berners-Lee actually came up with, and that was adopted, was the URL or Uniform Resource Locator. You'll learn more about these in Lesson 2 - HTML. A URL provides a unique identifier for any document on the Internet, located anywhere in the world. (For those of you who are sticklers for detail, a URL is actually a type of Universal Resource Identifier or URI. The difference between a URI and a URL is that the URL contains information about which type of protocol to use to serve the unique document.)
Linking Up
Knowing where a document lives was a good start. But, to actually create the links inside the document, each of the documents needed to be encoded in some manner. For instance, some of the references in a footnote might not be available online. So, having a link for that reference would be impossible.
The scheme that Berners-Lee came up with to encode these hypertext documents is called HTML or the HyperText Markup Language.
Sending Documents
When a link was encountered in a document, some way was needed for the clients and servers located on different machines to request and transfer those documents. For this part, Berners-Lee invented HTTP or the HyperText Transfer Protocol.
More Clients and Servers
Going back to the beginning of this discussion: do you remember clients and servers? Well, HTTP also has clients and servers. The HTTP server is called a Web server, but in the UNIX tradition [where all this started] it is often named httpd, which stands for the hyper-text-transfer-protocol-daemon.
In UNIX, daemons are resident programs that sit and wait to be of service. As Stan Kelly-Bootle has pointed out, "the spelling indicates such processes are amiable spirits rather than evil demons." Of course, if your httpd program goes bad, it can be hard to tell the difference.
The HTTP client program is called a browser. Tim Berners-Lee built the first Web browser, named appropriately, WorldWideWeb, in 1990 shown here:
You can read Tim's account and see a larger screenshot of the browser which predates Mozilla by several years at:
http://www.w3.org/People/Berners-Lee/WorldWideWeb.html
The only problem with Tim's browser was that it only ran on relatively rare and expensive NeXT machines; all of the other Web browsers were text based, academic papers not being known as hotbeds of creative graphics. It wasn't until an undergraduate student making $9.00 an hour--Mark Andressen--created the first Windows/Mac graphical Web browser, Mosaic, that things really took off.
And that, as they say, is history.
The rest of the sections in Lesson 1 prepare you to attack the Java programming language in earnest. Here's what you'll find:
- You'll get your hands dirty learning a little HTML for your Web site.
- You'll learn how to use the command-line interface.
- You'll learn how to use FTP to send your Web pages to your Web site.
- You'll learn how to add Java applets to your Web pages.
- You'll learn how to install the JDK and compile Java programs.
Once you've completed these lesson components, you'll have all the tools you need to complete Assignment 1, your course Web site.
Something to Talk About
Several years ago [1995], George Gilder, the author of the book, Wealth and Poverty [and President Reagan's favorite author], wrote an article in Forbes magazine where he argued that the invention of the Web, and Java in particular, seriously changed the dynamics of the PC world.
Please continue to the next section of this lesson.
|