Sun Java System Web Server 7.0 Update 8 Developer's Guide

Chapter 2 Server-Parsed HTML Tags

HTML files can contain tags that are executed on the server. In addition to supporting the standard server-side tags, Web Server allows you to embed servlets and define your own server-side tags.

This chapter has the following sections:

Enabling Server-Side HTML

The server parses server-side tags only if server-side parsing has been enabled. The following procedure describes how to enable server-side parsing using the Administration interface.

ProcedureEnable Server-Side HTML

  1. Login to Admin Console

  2. Access the Edit Virtual Server , and click the Content Handling tab.

  3. Click the General tab in the Content Handling screen.

  4. Select the options to enable the server-parsed HTML and with exec tag.

  5. In the parsed files drop-down list, select a resource for which the server will parse HTML.

    Choose the virtual server or a specific directory within the virtual server. If you choose a directory, the server will parse HTML only when the server receives a URL for that directory or any file in that directory.

  6. Choose the files to parse.

    • Files with the extension .shtml. The server parses only files with the extension .shtml. This option is the most common (and default) choice.

    • Files with the execute bit and the extension .html. (Unix and Linux only) The server parses files whose UNIX and Linux permissions specify that the execute bit is on. Using the execute permissions can be unreliable because in some cases the bit is set on files that are not executable.

    • All HTML files. The server parses all HTML files. Choosing this option can slow server performance.

  7. Click Save.

  8. Add the following directives are added to the magnus.conf file. Set NativeThread="no" for Web Server.

    In addition, these functions now originate from Shtml.dll (or libShtml.so on UNIX), which is located in install_dirC:\Program Files\Sun\WebServer7 for Windows, and install_dir/sun/webserver7 for UNIX.

  9. To enable parsing of server-side tags for files with extensions other than .shtml, add the extension to the appropriate line in the mime.types file.

    For example, the following line in mime.types indicates that files with either a .shtml or .jbhtml extension are parsed for server-side tags:


    type=magnus-internal/parsed-html exts=shtml,jbhtml

Using Server-Side HTML Commands

This section describes the HTML commands for including server-parsed tags in HTML files. These commands are embedded into HTML files, which are processed by the built-in SAF parse-html.

The server replaces each command with data determined by the command and its attributes.

The format for a command is:

<!--#command attribute1 attribute2 [Please define the ellipsis text entity] -->

The format for each attribute is a name-value pair such as:

name="value"

Commands and attribute names should be in lower case.

The commands are hidden within HTML comments so they are ignored if not parsed by the server. The standard server-side commands are listed below, and described in this section:

config Command

The config command initializes the format for other commands.

Example

<!--#config timefmt="%r %a %b %e, %Y" sizefmt="abbrev"-->

This example sets the date format to a value such as 10:45:35 AM Wed Apr 21, 2006, and the file size format to the number of KBytes or MBytes of characters used by the file.

include Command

The include command inserts a file into the parsed file. You can nest files by including another parsed file, which then includes another file, and so on. The client requesting the parsed document must also have access to the included file if your server uses access control for the directories in which they reside.

In Web Server, you can use the include command with the virtual attribute to include a CGI program file. You must also use an exec command to execute the CGI program.

Example

<!--#include file="bottle.gif"-->

echo Command

The echo command inserts the value of an environment variable. The var attribute specifies the environment variable to insert. If the variable is not found, “(none)” is inserted. For a list of environment variables, see Environment Variables in Server-side HTML Commands.

Example

<!--#echo var="DATE_GMT"-->

fsize Command

The fsize command sends the size of a file. The attributes are the same as those for the include command (virtual and file). The file size format is determined by the sizefmt attribute in the config command.

Example

<!--#fsize file="bottle.gif"-->

flastmod Command

The flastmod command prints the date a file was last modified. The attributes are the same as those for the include command (virtual and file). The date format is determined by the timefmt attribute in the config command.

Example

<!--#flastmod file="bottle.gif"-->

exec Command

The exec command runs a shell command or CGI program.

Example

<!--#exec cgi="workit.pl"-->

Environment Variables in Server-side HTML Commands

In addition to the standard set of environment variables used in CGI, you can include the following variables in your parsed commands:

Embedding Servlets

Web Server supports the <servlet> tag. This tag enables you to embed servlet output in an .shtml file. No configuration changes are necessary to enable this behavior. If SSI and servlets are both enabled, the <servlet> tag is enabled.

The <servlet> tag syntax is slightly different from that of other SSI commands in that it resembles the <APPLET> tag syntax:


<servlet name=
         name code=
         code codebase=
         path iParam1=
         v1
          iParam2=
         v2>
<param name=
         param1 value=
         v3>
<param name=
         param2 value=
         v4>
.
.
</servlet>

      

If the servlet is part of a web application, the code parameter is required and other parameters are ignored. The code parameter must include:

Defining Custom Server-Parsed HTML Tags

In Web Server , you can define your own server-side tags. For example, you could define the tag HELLO to invoke a function that prints “Hello World!” You could have the following code in your hello.shtml file:

<html>
  <head>
    <title>shtml custom tag example</title>
  </head>
  <body>
    <!--#HELLO-->
  </body>
</html>

When the browser displays this code, each occurrence of the HELLO tag calls the function.

ProcedureTo Define Customized Server-parsed Tag

  1. Defining the Functions that Implement a Tag.

    You must define the tag execution function. You must also define other functions that are called on tag loading and unloading and on page loading and unloading.

  2. Registering a New Tag.

    Write an initialization function that registers the tag using the shtml_add_tag function.

  3. Loading a New Tag Into the Server.

    These actions are described in the sections that follow.

Defining the Functions that Implement a Tag

Define the functions that implement the tags in C, using NSAPI.


#define TagUserData void*
typedef TagUserData (*ShtmlTagInstanceLoad)(
    const char* tag, pblock*, const char*, size_t);
typedef void (*ShtmlTagInstanceUnload)(TagUserData);
typedef int (*ShtmlTagExecuteFunc)(
    pblock*, Session*, Request*, TagUserData, TagUserData);
typedef TagUserData (*ShtmlTagPageLoadFunc)(
    pblock* pb, Session*, Request*);
typedef void (*ShtmlTagPageUnLoadFunc)(TagUserData);

         

The following example implements the HELLO tag:

/*
 * mytag.c: NSAPI functions to implement #HELLO SSI calls
 *
 *
 */

#include "nsapi.h"
#include "shtml/shtml_public.h"

/* FUNCTION : mytag_con
 *
 * DESCRIPTION: ShtmlTagInstanceLoad function
 */
#ifdef __cplusplus
extern "C"
#endif
TagUserData
mytag_con(const char* tag, pblock* pb, const char* c1, size_t t1)
{
    return NULL;
}

/* FUNCTION : mytag_des
 *
 * DESCRIPTION: ShtmlTagInstanceUnload
 */
#ifdef __cplusplus
extern "C"
#endif
void
mytag_des(TagUserData v1)
{

}

/* FUNCTION : mytag_load
 *
 * DESCRIPTION: ShtmlTagPageLoadFunc
 */
#ifdef __cplusplus
extern "C"
#endif
TagUserData
mytag_load(pblock *pb, Session *sn, Request *rq)
{
    return NULL;
}

/* FUNCTION : mytag_unload
 *
 * DESCRIPTION: ShtmlTagPageUnloadFunc
 */
#
#ifdef __cplusplus
extern "C"
#endif
void
mytag_unload(TagUserData v2)
{

}

/* FUNCTION : mytag
 *
 * DESCRIPTION: ShtmlTagExecuteFunc
 */
#ifdef __cplusplus
extern "C"
#endif
int
mytag(pblock* pb, Session* sn, Request* rq, TagUserData t1, TagUserData t2)
{
    char* buf;
    int length;
    char* client;
    buf = (char *) MALLOC(100*sizeof(char));
    length = util_sprintf(buf, "<h1>Hello World! </h1>", client);
    if (net_write(sn->csd, buf, length) == IO_ERROR)
    {
        FREE(buf);
        return REQ_ABORTED;
    }
    FREE(buf);
    return REQ_PROCEED;
}

/* FUNCTION : mytag_init
*
* DESCRIPTION: initialization function, calls shtml_add_tag() to
 * load new tag
*/
#
#ifdef __cplusplus
extern "C"
#endif
int
mytag_init(pblock* pb, Session* sn, Request* rq)
{
    int retVal = 0;
// NOTE: ALL arguments are required in the shtml_add_tag() function
    retVal = shtml_add_tag("HELLO", mytag_con, mytag_des, mytag, mytag_load, mytag_unload);

    return retVal;
}
/* end mytag.c */

Note –

To Reviewer: please review the above given code.


Registering a New Tag

In the initialization function for the shared library that defines the new tag, register the tag using the function shtml_add_tag. The signature is:


NSAPI_PUBLIC int shtml_add_tag (
    const char* tag,
    ShtmlTagInstanceLoad ctor,
    ShtmlTagInstanceUnload dtor,
    ShtmlTagExecuteFunc execFn,
    ShtmlTagPageLoadFunc pageLoadFn,
    ShtmlTagPageUnLoadFunc pageUnLoadFn);

         

Any of these arguments can return NULL except for tag and execFn.

Loading a New Tag Into the Server

After creating the shared library that defines the new tag, you load the library into Web Server by adding the following directives to the configuration file magnus.conf:

Time Formats

The following table describes the format strings for dates and times used by server-parsed HTML.

Table 2–1 Time Formats

Symbol  

Meaning  

%a 

Abbreviated weekday name (3 characters)

%d 

Day of month as decimal number (01-31)

%S 

Second as decimal number (00-59) 

%M 

Minute as decimal number (00-59) 

%H 

Hour in 24-hour format (00-23) 

%Y 

Year with century, as decimal number, up to 2099 

%b 

Abbreviated month name (3 characters)

%h 

Abbreviated month name (3 characterss) 

%T 

Time "HH:MM:SS" 

%X 

Time "HH:MM:SS" 

%A 

Full weekday name 

%B 

Full month name 

%C 

"%a %b %e %H:%M:%S %Y" 

%c 

Date & time "%m/%d/%y %H:%M:%S" 

%D 

Date "%m/%d/%y" 

%e 

Day of month as decimal number (1-31) without leading zeros 

%I 

Hour in 12-hour format (01-12) 

%j 

Day of year as decimal number (001-366) 

%k 

Hour in 24-hour format (0-23) without leading zeros 

%l 

Hour in 12-hour format (1-12) without leading zeros 

%m 

Month as decimal number (01-12) 

%n 

Line feed 

%p 

A.M./P.M. indicator for 12-hour clock 

%R 

Time "%H:%M" 

%r 

Time "%I:%M:%S %p" 

%t 

Tab 

%U 

Week of year as decimal number, with Sunday as first day of week (00-51) 

%w 

Weekday as decimal number (0-6; Sunday is 0) 

%W 

Week of year as decimal number, with Monday as first day of week (00-51) 

%x 

Date "%m/%d/%y" 

%y 

Year without century, as decimal number (00-99) 

%% 

Percent sign