Server-Side Includes (Java Enterprise in a Nutshell) Book Home Java Enterprise in a Nutshell Search this book

5.6. Server-Side Includes

Servlets are not confined to handling entire requests. Some web servers allow servlets to add small amounts of dynamic content to otherwise static HTML pages. This is similar to the server-side include functionality found in most web servers, but includes additional servlet-specific functionality. For example, let's assume that we want to use a server-side include to add a randomly selected advertisement to a page. A page that uses the advertisement servlet is written just like a normal HTML page, except that it contains one or more <SERVLET> tags and is saved with the .shtml extension. The <SERVLET> tag is similar to the <APPLET> tag, which loads an applet within a page. When a client requests a .shtml page, the server finds all of the <SERVLET> tags in the text and replaces them with the output from the appropriate servlets.

When you use a <SERVLET> tag, you must include a CODE parameter that identifies the servlet to be loaded. This can be a class name or a servlet alias set up within the server. On some servers, you can specify an optional CODEBASE parameter that loads the servlet code from a remote location. Any additional parameters are treated as servlet initialization parameters. Each <SERVLET> tag must be matched by a closing </SERVLET> tag. Between the opening and closing tags, you can include as many <PARAM> tags as necessary, where you specify NAME and VALUE attributes for each one. The servlet can then access these parameters with getParameter().

Now let's look at an HTML page that actually uses a servlet with a server-side include. Here's a sample .shtml file:

<HTML>
<HEAD><TITLE>Today's News</TITLE></HEAD>
<BODY>
<H1>Headlines</H1>
<H2>Java Servlets Take Over Web!</H2>
<SERVLET CODE=AdMaker>
<PARAM NAME=pagetitle VALUE="Headlines">
</SERVLET>
</BODY>
</HTML>

The actual AdMaker servlet is shown in Example 5-6.

Example 5-6. An Advertising Servlet

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class AdMaker extends HttpServlet {
  
  static String[] adText = { "Al's Web Services", 
                             "Bob's House of HDs",
                             "Main St. Computers" };
  int currentAd = 0; 

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    
    String adContent;
    PrintWriter out = resp.getWriter();
    synchronized(this) {
      adContent = adText[currentAd];
      currentAd++;
      if (currentAd >= adText.length)
        currentAd = 0;
    } 
    String title = req.getParameter("pagetitle");
    if(title != null)
      out.println(title + " is brought to you by");
    else
      out.println("This page is brought to you by");
    out.println(adContent);
  }
}

This servlet really isn't too different from the other ones we've looked at. It accesses parameters (in this case from the <SERVLET> tag instead of a set of HTTP form values) and uses a PrintWriter to produce HTML. It does not set a content type or manipulate any HTTP headers, however, because that information may have been sent to the browser before the servlet begins executing.

Server-side includes can be a powerful tool, but they are not part of the standard Servlet API, and therefore some servlet implementations may not support them at all. To make matters worse, some implementations may work in a different manner (this is especially true of third-party servlet runners that plug into non-Java-aware web servers). The example here was developed and tested with Java Web Server. If you want to use server-side includes, you should read your server documentation first.

JavaServer Pages (commonly referred to as JSP) is another technology for accessing server-side Java components directly in HTML pages. The overall effect is not unlike Microsoft's Active Server Pages (ASP). As of this writing, Sun has just finalized the JSP 1.0 specification and several server vendors have announced support for it.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.