16.3 Interactive Web Pages

When HTML was first developed, it was amazing in its ability to format network-based text and images in interesting ways. However, that information was static—there was no way to interact with the information and pictures presented in a web page.

As users have clamored for a more dynamic Web, new technologies have been developed to accommodate these requests. These technologies take different approaches to solving the problem. Many of the new ideas are offshoots of the Java programming language, which is able to exploit the Web because of its platform independence. Let’s look briefly at two of these technologies: Java applets and Java Server Pages.

Java Applets

A Java applet is a program that is designed to be embedded into an HTML document and transferred over the Web to someone who wants to run the program. An applet is actually executed in the browser used to view the web page.

An applet is embedded into an HTML document using the APPLET tag. For example:

<applet code=”MyApplet.class” width=250 height=160>
</applet>

When a web user references the page containing this tag, the applet program MyApplet.class is sent, along with any text, images, and other data that the page contains. The browser knows how to handle each type of data—it formats text appropriately and displays images as needed. In the case of an applet, the browser has a built-in interpreter that executes the applet, allowing the user to interact with it. Thousands of Java applets are available on the Web, and most browsers are set up to execute them.

Consider the difficulties inherent in this situation. A program is written on one computer, but then may be transferred to any other computer on the Web to be executed. How can we execute a program that was written for one type of computer on possibly many other types of computers? The key, as briefly explained in Chapter 9, is that Java programs are compiled into Bytecode, a low-level representation of a program that is not the machine code for any particular type of CPU. This Bytecode can be executed by any valid Bytecode interpreter, no matter which type of machine it is running on.

The applet model puts the burden on the client’s machine. That is, a web user brings the program to his or her computer and executes it there. It may be frightening to think that, while you are casually surfing the Web, suddenly someone’s program may begin executing on your computer. That would be a problem, except that Java applets are restricted as to what they can do. The Java language has a carefully constructed security model. An applet, for instance, cannot access any local files or change any system settings.

Depending on the nature of the applet, the client’s computer may or may not be up to the job of executing the applet. For this reason, and because applets are transferred over a network, they tend to be relatively small. Although appropriate for some situations, applets do not resolve all of the interactive needs of web users.

Java Server Pages

A Java Server Page (JSP) is a web page that has JSP scriptlets embedded in it. A scriptlet is a small piece of executable code intertwined among regular HTML content. While not exactly the same as Java, JSP code resembles the general Java programming language.

A JSP scriptlet is encased in special tags beginning with <% and ending with %>. Special objects have been predefined to facilitate some processing. For example, the object called out can be used to produce output, which is integrated into the web page wherever the scriptlet occurs. The following scriptlet produces the phrase “hello there” between the opening and closing tag of an h3 header:

<h3>
<%
out.println (“hello there”);
%>
</h3>

In this particular case, the result is equivalent to

<h3>hello there</h3>

But imagine JSP scriptlets as having the expressive power of a full programming language (which they do). We can make use of almost all aspects of a regular Java program, such as variables, conditionals, loops, and objects. With that kind of processing power, a JSP page can make significant decisions resulting in truly dynamic results.

JSPs are executed on the server side, where the web page re sides. They help dynamically define the content of a web page be fore it is shipped to the user. By the time it arrives at your computer, all active processing has taken place, producing a static (though dynamically created) web page.

JSPs are particularly good for coordinating the interaction between a web page and an underlying database. The details of this type of processing are beyond the scope of this book, but you’ve probably encountered this type of processing while surfing the Web. Electronic storefronts (sites that exist primarily to sell products), in particular, make extensive use of this type of processing. The data about available products is not stored in static HTML pages. Instead, this data is stored in a database. When you make a request for information about a particular product, a JSP may actually respond to you. The scriptlets in the page interact with the database and extract the needed information. Scriptlets and regular HTML format the data appropriately and then ship the page to your computer for viewing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.250.169