Developer's Daily Java Education
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
Pure Java
Articles
   
 
An introduction to Java Page Compilation
 
 

Introduction to "Page Compilation"

The Java Web Server v1.1 introduced an exciting new technology called "Page Compilation". This technology makes it possible to generate dynamic HTML pages from hybrid HTML/Java source files by using a special servlet, named PageCompileServlet.

Why is this so exciting? In a nutshell, it makes it easier than ever before to bring dynamic Java servlet technology to your HTML pages. PageCompilation makes it easy to generate Java servlet pages while letting you keep using your favorite HTML authoring tools (Microsoft FrontPage, Netscape PageComposer, etc.).

We've found that the process of creating dynamic HTML pages with Java is now reduced to these simple steps:

  1. Create the HTML page you want your users to see using your favorite HTML-authoring tool.
  2. Insert your Java code wherever you want to generate dynamic data.
  3. Put the file on your web server with the filename extension '.jhtml'.
  4. Access the file from your web browser.
It's as easy as that to create dynamic content inside of your web pages!
 

Who's doing all the work around here?

The PageCompileServlet simplifies the process of creating Java servlets. The first time your hybrid HTML/Java file is requested, the PageCompileServlet does this work for you:

  1. Translates the hybrid HTML/Java file into a Java source file.
  2. Compiles and instantiates the source file as a servlet.
  3. Runs the compiled file as a servlet.
In the future, if your source code file is changed, the PageCompileServlet detects this and repeats these tasks for you. If you've ever created Java servlets before, you know that this is a great time-saver.
 

How about an example?

Let's walk through a quick and simple example so you can see this process in action. In following with tradition, we'll create a simple "Hello, world" JHTML page.

To start with, suppose you created an HTML file that looks like this with your favorite HTML-authoring tool:

If you're comfortable with HTML, that code isn't too exciting.

To make it interesting, let's add a little spice to it in the form of Page Compilation. Let's say you want to display the time and date within this exciting little page. Using Java Page Compilation, here's all you need to do:

Now, every time this page is accessed, the Java code will determine the current date and time from the web server, and display it in this Hello, World page. To make it easier to see, I've displayed the new Java code in a red color.

After creating this page, just save it in your Java Web Server 1.1 HTML directory structure, using a name like HelloWorld.jhtml.  The first part of the name ("HelloWorld") isn't important, but you must save it with the '.jhtml' extension so it will be properly recognized by the PageCompileServlet.

After saving the file, just access it through your web browser.  On my system, I saved the file in my public_html root directory, so I just entered this URL into my Netscape browser:

 

The first time the page is accessed

As you'll notice, the first time you access this page it will appear painfully slow, and you'll think something has gone wrong. Don't fret though, because this only happens when the page is accessed the first time. That's because the PageCompileServlet is doing a lot of work for you.  The first time the page is accessed, your hybrid HTML/Java page is converted to servlet code and compiled automatically for you.

On all subsequent accesses, your HTML (or should I say "JHTML") page returns very quickly.
 

If you ever change the page

If you ever change your JHTML page, it will again seem to be very slow the first time you access it after your change. Again, that's because the PageCompileServlet is doing the dirty work for you. The same process occurs, and all subsequent accesses will be very fast.
 

Imagine ...

Using your imagination, you can see why this process is so good:

  1. It lets you keep using your favorite HTML authoring tool.
  2. It's very simple - much easier than manually creating a servlet to accomplish the same task. Just put the file on your server in the location you desire.
  3. It lets you make your pages dynamic - you can now access all types of dynamic content, including information from your server, your database servers, and just about any other dynamic resource you can imagine.
Without getting much more excited, let's just say that Java Page Compilation makes the process of creating dynamic content very easy, and now your imagination becomes the limit of the information you can present to your web page visitors!
 

Summary

Well, that's a quick introduction into the Page Compilation process. If you have access to the Java Web Server, or another server that supports Page Compilation, I recommend giving it a try to see if it can make your life easier and more productive. Sun has certainly made the process of generating dynamic content very painless and simple.

In the future we're going to go way beyond the process of demonstrating this simple "Hello, world" page. We'll throw in some database access technology, networking, and whatever else we can think of that might be valuable and interesting. In the meantime, if you have any ideas for Page Compilation topics you'd like to see us cover, just write our Java team with your ideas. We'll be glad to incorporate your ideas into our future articles.
 
 

Relevant version information:
 
Server: Sun Java Web Server 1.1
JDK: JDK 1.1.x
Article publication date: December 18, 1998

What's Related


Copyright 1998-2008 DevDaily Interactive, Inc.
All Rights Reserved.