Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lectures_SSD2_Yermakova / Lectures_SSD2 Yermakova.doc
Скачиваний:
226
Добавлен:
25.02.2016
Размер:
3.16 Mб
Скачать

JavaScript

JavaScript is a "scripting language," which means the instructions the programmer writes are not compiled in advance. Instead, they are interpreted by an application when the script is run by the computer. Both 4.2.2 Lab: Macros and 4.3 Batch Script Files contain examples of simple scripting languages. JavaScript is a more complex scripting language that uses much of the structure and syntax of Java. The scripts are included inside an HTML document, using the <SCRIPT> tag. JavaScript provides operations for manipulating the Web browser (for example, creating new pop-up windows), controlling interactions with the user, and generating Web page text "on-the-fly."

In the following JavaScript example, we build a page with a black background and use green letters to display the text "Hello World!" The Web page's title, "JavaScript Demonstration," is retrieved and inserted into the second line of text on the page. This second line is displayed in red.

<html>   <head>     <title>JavaScript Demonstration</title>     <script type="text/javascript" language="JavaScript1.2">       function display() {         document.body.bgColor = "black";         var ln1 = "Hello World!";         var ln2 = "This is the " + document.title + " page.";         var str = ln1.fontcolor("lime") + "<br />" + ln2.fontcolor("red");         document.writeln(str);       }     </script>   </head>   <body>     <script type="text/javascript" language="JavaScript1.2">       display();     </script>   </body> </html>

Launch the JavaScript Demonstration page utilizing the above script in a new window. You can use the Web browser's "View Source" command to see the contents of the file. Note: Netscape converts all JavaScript to normal HTML tags, so using the View Source command in Netscape will display the HTML that results from the conversion. Using the View Source command in Internet Explorer will show the file as a script.

Although the above example does not demonstrate the more advanced features of JavaScript, such as user interactivity, you can see how JavaScript allows developers to generate Web pages dynamically by having the user's browser execute the script when the page is to be displayed.

Java

Java is an object-oriented programming language. It is designed to be both highly portable and easy to use. While Java supports full-scale application development, the kind of Java programs typically encountered in Web pages are small routines called Java applets. An applet is a piece of code designed to run inside a Web browser or other applet viewer. Inside each Web browser is a Java virtual machine (JVM) that executes compiled Java code. Hence, Java programs do not have to be compiled for specific computer architectures—such as Pentiums, PowerPCs, and so on. They are compiled for the Java virtual machine and can then be executed on any computer that provides an implementation of the JVM.

In the example below, we build a Java applet to write the text "Hello World!" inside a rounded rectangle.

import java.applet.Applet; import java.awt.Graphics; public class Demonstration extends Applet {   public void paint(Graphics g) {     g.drawRoundRect(5, 5, 100, 50, 10, 10);     g.drawString("Hello World!", 23, 35);   } }

Here is the result of the applet: 

Java is a more powerful language than JavaScript, allowing the developer to create applets with a wide range of functionality. There are Java email clients, FTP clients, Telnet clients, etc. Java can also easily parse XML documents and run database queries, providing a powerful front end for Web-based database access.