| Previous | Table of Contents | Next |
Once you are comfortable that your servlet works, you can move it to your production Web server. You can also use a Web server such as the Java Web Server for testing purposes. Read the documentation on your server to determine where to put the servlet. For more information concerning adding servlet support to your Web server, read the documentation that comes with the JSDK.
The Java Web Server provides several mechanisms for creating dynamic content, including server-side includes and servlet chaining. This section provides an overview of these features. If you are going to use the Java Web Server, you may want to check out http://jserv.javasoft.com:80/products/java-server/documentation/webserver1.0.2/index_developer.html for more information on these two features.
Servlets may be invoked by Web servers to preprocess Web pages as server-side includes in Web servers that provide complete servlet support. Server-specific enhancements to the HTML syntax send an indication to the Web server to preprocess. The following code is an example of HTML syntax that sends this message to the server:
<SERVLET NAME=ServletName> <PARAM NAME=param1 VALUE=val1> <PARAM NAME=param2 VALUE=val2> This text only displays if the server doesnít support Server side includes for servlets </SERVLET>
The <SERVLET> tag indicates that a servlet ServletName should be loaded and then called with a particular set of parameters. The output of the servlet is included at this point in the HTML file returned to the client. You can think of the <SERVLET> tag as a way of telling the server to fill in the blank with values generated by the servlet. You could use this technique to include a table that is dynamically generated by values in a database. Figure 6.5 illustrates the output of the following HTML:
<HTML>
Here is your order so far:
<HR>
<SERVLET NAME=ProcessOrderServlet>
<PARAM NAME=orderid VALUE=123>
</SERVLET>
<HR>
</HTML>
The HTML indicates to the Web server that a servlet called ProcessOrderServlet should be called to fill in the HTML that goes between the <HR> tags. The servlet is passed one parameter, the order id, which is used to access the order database, then outputs the result shown in Figure 6.5.
The Java Web Server supports chaining local and remote servlets. The input from the browser is sent to the first servlet in the chain; the output from the last servlet in the chain is sent back as the response to the browser. Each servlet in the chain has the inputs and outputs piped to the servlet before and after it, respectively. You could use this feature to create small servlets that act as data filters or business rules. For instance, a Web page containing an order to process could submit form data to the first servlet, which validates the credit card entered on the form. If it is a valid card, the information is passed on to the order-processing servlet, which checks the inventory database for stock and then forwards the information to the result servlet, which presents the client with the success or failure result of its submission. These chains are configured by using the Administration tool that is part of the Java Web Server or by configuring an association between a specific MIME type and a list of servlets. This is done in the mimeservlets.properties file residing in the directory <server_root>/properties/server/javawebserver/webpageservice/. For more information on this feature, see the Java Web Server tool kit documentation at http://jserv.javasoft.com:80/products/java-server/toolkit/index.html.
Figure 6.5 Output of server-side include.
NOTE: Because servlets must be able to support requests from multiple clients, the servlet API supports both single-threaded and multithreaded servlets. The developer still needs to handle synchronizing other resources such as database connections. Chapter 7, Programming Servlets, covers this in more detail.
Using servlets as server-side includes allows you to create dynamic Web content. Using servlets in a chain allows you to create small filter services that can parse input from a form and run it through multiple business rules before sending results back to the client. These features are specific to the Java Web Server, but other vendors will be adopting them or something similar as the standards are flushed out.
Servlets are a natural extension to Java and greatly simplify the task of writing enterprise solutions in Java. The main benefits of servlets are as follows:
Chapter 7, Programming Servlets, provides a detailed definition of the process and API required for creating servlets. Chapter 8, A Servlet-Based Search Engine, walks you through the development of a complete servlet-based search engine that you could use on your own Web site.
| Previous | Table of Contents | Next |