Defining Custom Server-Parsed HTML Tags

                                                                                                                                                                                                                                                                                                                                      Back to [Book]     [Topic]

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.

To 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.

  • Include the header shtml_public.h, which is in the directory install_dir/libinclude/shtml.
  • Link against the shtml shared library. On Windows, shtml.dll is in install_dir/lib On UNIX platforms, libShtml.so or .sl is in install_dir/lib

ShtmlTagExecuteFunc is the actual tag handler. It gets called with the usual NSAPI pblock, Session, and Request variables. In addition, it also gets passed the TagUserData created from the result of executing the tag loading and page loading functions (if defined) for that tag.

The signature for the tag execution function is:

typedef int (*ShtmlTagExecuteFunc)(pblock*, Session*, Request*, TagUserData, TagUserData);

Write the body of the tag execution function to generate the output to replace the tag in the .shtml page. Do this by using the net_write NSAPI function. This function writes a specified number of bytes to a specified socket from a specified buffer.

For more information about NSAPI plugins and functions, see the Sun Java System Web Server NSAPI Developer's Guide.

The tag execution function must return an int that indicates whether the server should proceed to the next instruction in obj.conf:

  • REQ_PROCEED: Execution was successful
  • REQ_NOACTION— Nothing happened
  • REQ_ABORTED— An error occurred
  • REQ_EXIT— The connection was lost

The other functions you must define for your tag are:

  • ShtmlTagInstanceLoad— Called when a page containing the tag is parsed. It is not called if the page is retrieved from the browser’s cache. This function basically serves as a constructor, the result of which is cached and is passed into ShtmlTagExecuteFunc whenever the execution function is called.
  • ShtmlTagInstanceUnload— Gets passed the result that was originally returned from the ShtmlTagInstanceLoad function.
  • ShtmlTagPageLoadFunc— This is called when a page containing the tag is executed, regardless of whether the page is still in the browser’s cache. This function enables you to make information persistent between occurrences of the same tag on the same page.
  • ShtmlTagPageUnLoadFn— Called after a page containing the tag has executed and gets passed the result returned from the ShtmlTagPageLoadFunc.

The signatures for these functions are:

#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 */

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:

  • An Init directive whose fn parameter is load-modules and whose shlib parameter is the shared library to load.
    For example, if you compiled your tag into the shared object install_dir /hello.so, would be:Init funcs="mytag,mytag_init" shlib="install_dir/hello.so" fn="load-modules"
  • An Init directive whose fn parameter is the initialization function in the shared library that uses shtml_add_tag to register the tag. For example:

Init fn="mytag_init"

Labels

java java Delete
server server Delete
sun sun Delete
webserver webserver Delete
application application Delete
system system Delete
webtier webtier Delete
guide guide Delete
webserver70 webserver70 Delete
sunjava sunjava Delete
+devguide +devguide Delete
developers developers Delete
web web Delete
developersguide developersguide Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact