J2EEOnlineJ2EE





Java Servlets   «Prev  Next»
Lesson 4Generating part of a page from a servlet
ObjectiveWrite a servlet and HTML to combine static and dynamic content.

Generating Part of a Page from a Servlet

The servlet in the last lesson generated an entire page of HTML. That’s equivalent to the way a CGI program or script works. But servlets can also be used to generate just part of a page, like server side includes.
To include servlet output in a page, you need to do two things:
  1. Change the extension of the HTML file to .shtml
  2. Add a <SERVLET> tag to your HTML
Changing the extension of the file just alerts the server that it may be getting some of the HTML from a server side include. It has no effect on the way the browser renders the HTML.
The <SERVLET> tag is very much like the <APPLET> tag. In its simplest form, it just provides the classname of the servlet to be executed. The Web server will run the servlet, and substitute the servlet output where the tag was in the source HTML.
Here’s some sample HTML in a file called include.shtml:

Ordinary HTML
Ordinary HTML
  1. Here I used a Horizontal Rule to show the start of the servlet output
  2. CODE is the only required attribute of the SERVLET tag
  3. Do not forget to end your SERVLET tag
  4. Another Horizontal Rule makes it easy to spot the end of the servlet output

Include Servlet Output
This HTML needs a servlet called Include before it will work (notice the <SERVLET CODE=Include.class> tag in the above MouseOver.) Here’s how you would write the code for the servlet:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Include extends HttpServlet {     
    public void doGet (HttpServletRequest req, 
    HttpServletResponse res) throws ServletException, IOException{
      res.setContentType("text/html");  
      PrintWriter out = res.getWriter();
      out.println("Servlets don't have to generate entire pages...");  
      out.close();    
    }    
}

This is similar to the servlets that you saw last lesson, except it generates only some text output, not an entire HTML page, which would need to include the <HTML> and </HTML> tags.

Testing the servlet

You have tested your earlier servlets with the JSDK mini-server, but it only serves out servlets.
To test this servlet, you need a server that can deliver the shtml file to a browser, and include your servlet output.
You will have to use the Java Web Server. First start the Java Web Server, and then follow these steps:
  1. Enter the HTML into a file and name it with a .shtml extension (include.shtml in my example). Copy the file to the directory where the Java Web Server looks for HTML (on my system, that’s C:\JWS\public_html, which is the default for the Java Web Server).
  2. Enter the Java into a file (Include.java in my example) and compile it
  3. Copy the .class file (Include.class in my example) to the directory where the Java Web Server looks for servlets (on my system, that’s C:\JWS\Servlets).
  4. Start a browser, and enter a URL to the HTML file you created. You should see the combined output.
The combination of my include.shtml and my Include servlet produced this output:

include.shtml output
include.shtml output
See how to track a piece of information between page loads in the next lesson.

Generating Servlet Page - Exercise

Write a servlet to include in an HTML page using the <servlet> tag.
Generating Servlet Page - Exercise