Page #461 (Chapter 34. Servlets)

 
[Page 1158 ( continued )]

34.2. HTML and Common Gateway Interface

Java servlets run in the Web environment. To understand Java servlets, let us review HTML and the Common Gateway Interface (CGI).

34.2.1. Static Web Contents

You create Web pages using HTML. Your Web pages are stored as files on the Web server. The files are usually stored in the /htdocs directory on Unix, as shown in Figure 34.1. A user types a URL for the file from a Web browser. The browser contacts the Web server and requests the file. The server finds the file and returns it to the browser. The browser then displays the file to the user . This works fine for static information that does not change regardless of who requests it or when it is requested . Static information is stored in HTML files. The information in the files can be updated, but at any given time every request for the same document returns exactly the same result.

Figure 34.1. A Web browser requests a static HTML page from a Web server.

34.2.2. Dynamic Web Contents and Common Gateway Interface

Not all information, however, is static in nature. Stock quotes are updated every minute. Election vote counts are updated constantly on Election Day. Weather reports are frequently updated. The balance in a customer's bank account is updated whenever a transaction takes place. To view up-to-date information on the Web, the HTML pages for displaying this information must be generated dynamically. Dynamic Web pages are generated by Web servers. The Web server needs to run certain programs to process user requests from Web browsers in order to produce a customized response.

The Common Gateway Interface , or CGI , was proposed to generate dynamic Web content. The interface provides a standard framework for Web servers to interact with external programs, known as CGI programs . As shown in Figure 34.2, the Web server receives a request from a Web browser and passes it to the CGI program. The CGI program processes the request and generates a response at runtime. CGI programs can be written in any language, but the Perl language is the most popular choice. CGI programs are typically stored in the /cgi-bin directory. Here is a pseudocode example of a CGI program for displaying a customer's bank account balance:


[Page 1159]
  1. Obtain account ID and password.

  2. Verify account ID and password. If it fails, generate an HTML page to report incorrect account ID and password, and exit.

  3. Retrieve account balance from the database; generate an HTML page to display the account ID and balance.

Figure 34.2. A Web browser requests a dynamic HTML page from a Web server.

34.2.3. The GET and POST Methods

The two most common HTTP requests, also known as methods , are GET and POST. The Web browser issues a request using a URL or an HTML form to trigger the Web server to execute a CGI program. HTML forms will be introduced in §34.6, "HTML Forms." When issuing a CGI request directly from a URL, the GET method is used. This URL is known as a query string . The URL query string consists of the location of the CGI program, parameters, and their values. For example, the following URL causes the CGI program getBalance to be invoked on the server side:

 http://www.webserverhost.com/cgi-bin/  getBalance.cgi?accountId=scott+smith&password=tiger  

The ? symbol separates the program from the parameters. The parameter name and value are associated using the = symbol. Parameter pairs are separated using the & symbol. The + symbol denotes a space character.

When issuing a request from an HTML form, either a GET method or a POST method can be used. The form explicitly specifies which of the two is used. If the GET method is used, the data in the form is appended to the request string as if it were submitted using a URL. If the POST method is used, the data in the form is packaged as part of the request file. The server program obtains the data by reading the file. The POST method is more secure than the GET method.

Note

The GET and POST methods both send requests to the Web server. The POST method always triggers the execution of the corresponding CGI program. The GET method may not cause the CGI program to be executed if the previous same request is cached in the Web browser. Web browsers often cache Web pages so that the same request can be quickly responded to without contacting the Web server. The browser checks the request sent through the GET method as a URL query string. If the results for the exact same URL are cached on a disk, then the previous Web pages for the URL may be displayed. To ensure that a new Web page is always displayed, use the POST method. For example, use a POST method if the request will actually update the database. If your request is not time-sensitive, such as finding the address of a student in the database, use the GET method to speed up the performance.


[Page 1160]

34.2.4. From CGI to Java Servlets

CGI provides a relatively simple approach for creating dynamic Web applications that accept a user request, process it on the server side, and return responses to the Web browser. But CGI is very slow when handling a large number of requests simultaneously, because the Web server spawns a process for executing each CGI program. Each process has its own runtime environment that contains and runs the CGI program. It is not difficult to imagine what will happen if many CGI programs are executed simultaneously . System resource would be quickly exhausted, potentially causing the server to crash.

Several new approaches have been developed to remedy the performance problem of CGI programs. Java servlets are one successful technology for this purpose. Java servlets are Java programs that function like CGI programs. They are executed upon request from a Web browser. All servlets run inside a servlet container , also referred to as a servlet server or a servlet engine . A servlet container is a single process that runs a Java Virtual Machine. The JVM creates a thread to handle each servlet. Java threads have much less overhead than full-blown processes. All the threads share the same memory allocated to the JVM. Since the JVM persists beyond the life cycle of a single servlet execution, servlets can share objects already created in the JVM. For example, if multiple servlets access the same database, they can share the connection object. Servlets are much more efficient than CGI.

Servlets have other benefits that are inherent in Java. As Java programs, they are object-oriented, portable, and platform-independent. Since you know Java, you can develop servlets immediately with the support of Java API for accessing databases and network resources.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net