Java Web Server

Other Page Compilation Features


Contents / New Features / Administrator Docs / Developer Docs / Index / Page Comp Intro

Some additional features provided by the PageCompileServlet are covered in this document:

Using Servlet Tags

PageCompileServlet supports the use of servlet tags in your hybrid HTML/JAVA pages. Servlet tags which are embedded in .jhtml files are interpreted in the same manner as those in .shtml files. The servlet tag is compiled into pure Java code that gets a reference to the servlet, builds a dictionary of parameters to pass to the servlet and invokes the service method of the specified servlet.

Because servlet tags are compiled rather than interpreted, you cannot dynamically generate servlet tags or parameter tags from Java code, as this output from Java code goes directly to the browser. The following example shows an embedded servlet tag which would result in output going directly to the browser:

<html><body>
<java>
  // INCORRECT - servlet tag goes right to browser
  out.print("<servlet code=\"InlineServlet\">);
  out.print("  <param name=\"file\" value=\"inc.html\">");
  out.print("</servlet>");
</java>

Although you cannot dynamically generate servlet tags or parameters, you can specify dynamic Java code for names and values of parameter tags, as shown in this example:

<html>
<body>

<java>
  String myFile = "inc.html";
  String myName = "file";
</java>

<servlet code="InlineServlet">
  <param name="`myName`" value="`myFile`">
</servlet>

</body>
</html>

Printing Java Expressions

PageCompileServlet provides a shorthand method for printing Java expressions directly to the output stream. This can be done using the <java type=print> tag. This method eliminates the need to surround expressions with out.print calls. The following example will print 'hello':

<html>
<body>

<java>
  String myText = "hello";
</java>

<java type=print>myText</java>

</body>
</html>

You can use the <java type=print> tag for any expression that is suitable for the String.valueOf() Java method call, including any primitive value expression such as int, float and so on, or any Object. An expression that evaluates to a null object results in no text being sent to the output stream.

Catching the Stop Signal

The user is allowed to interrupt the transmission of a page, usually through the use of a Stop button on their browser. If this happens, there is no point in continuing to execute the page since the browser is no longer listening. The next time you print something to out, you'll throw an IOException. PageCompileServlet catches this exception for you, but in doing so your page will be terminated early.

Sometimes, however, you may want to detect that the user terminated the page early. Perhaps you have an advertising system and you want to tell one of your advertisers that people are not reading their ad all the way through.

To do this, you'll want to catch the IOException yourself. You'll need to put a try...catch construct around your entire file.

Here's an example of how this is done. This page will print out the number of times it's been interrupted, then count from 0 to 4999 (thus giving you ample reason to interrupt it). If you interrupt the page somewhere in the middle, an IOException is thrown, and caught by the code at the end of the page. This code uses the globalData to update a counter of how many times it's been interrupted.

<java>
try {
</java>

<html>
<head><title>Interrupted page/title></head>
<body>
<h1>Interrupted page</h1>

<h2>How many times has this page been interrupted?</h2>

<java type=class>
/* 
 * This servlet member variable will keep track of the number of times
 * that this servlet is interrupted by the user.
 */
int numTimesInterrupted = 0;
</java>

<java>
out.println ("<p>This page has been interrupted ");
out.println (numTimesInterrupted);
out.println (" times!");

out.println ("<ul>");
for (int i = 0; i < 500; i++) {
  out.println ("<li>" + i);
  out.flush();  
  /*
   * Give the user a chance to stop the page rendering
   * since this is so fast otherwise. 
   * Flush the buffer to write to the socket
   */
  try {
    Thread.sleep(10);
  }
  catch (IOException e) {}
}
out.println ("</ul>");
</java>

</body>
</html> 
<java>
}
catch (IOException exc) {
  // If the user pressed the "Stop" button, we end up here
  synchronized (this) {
    numTimesInterrupted++;
  }
}
</java>

The call to out.flush() in the example above tells the server to write any stored up data to the browser immediately. The server only detects that the stop button has been pressed when it tries to write new data to the server. Without this line, the entire output of your servlet could be stored in the buffer and the IOException might not be generated until the server flushed the buffer after your service method returned.

Another alternative to catching the IOException is to use the finally statement. You would do this by replacing catch (IOException exc) with finally. If you do this, then the code in this section will be executed whether the page was interrupted or not. In fact, the code in this section will be executed even if any other Exception is thrown in the page.

Accessing the Request and Response Objects

The javax.servlet.http.HttpServletRequest Object allows you to access all the information contained in the request. This includes the values of query and post data, cookies, the request method, and so on.

The following example illustrates how to access the javax.servlet.http.HttpServletRequest Object:

<html>
<body>
<java>
String message;  
    if ((message = request.getParameter("MESSAGE")) == null)
    out.print("No message query argument supplied");
else 
    out.print("Message is " + message);
</java>
</body>
</html>

The javax.servlet.http.HttpServletResponse Object allows PageCompileServlet to directly control the output of a servlet, to perform tasks such as redirecting the request to a different URL.

The following example illustrates how to access the javax.servlet.http.HttpServletResponse Object:

<java>

// Redirect if a MESSAGE Query argument is not supplied
      if (request.getParameter("MESSAGE") == null)
      response.sendRedirect("error.jhtml");

</java> <html> <body> Message is: <java type=print>request.getParameter("MESSAGE")</java> </body> </html>


Top
java-server-feedback@java.sun.com
Copyright © 1997 Sun Microsystems, Inc.
All Rights Reserved.