Generating a web site front page from a Java Servlet (ISAPI filter)

Here is a simple example using an ISAPI filter to show how to make the frontpage of a web site be generated by a Java Servlet. It is written in C.

/*
 * Map requests for '/' to '/dir/file'
 *
 * This ISAPI filter shows how to make the frontpage of a website be generated
 * by a Java Servlet.
 *
 * Normally Java Servlets are mounted under a URL prefix, such as '/jserv'.
 * However this means it is not directly possible to mount Java Servlets
 * directly under '/', since then a request for any URL, such as
 * /images/bar.gif would get mapped onto a Java Servlet.
 *
 * This ISAPI filter rewrites requests for the frontpage '/' onto a Java
 * servlet onto mounted under '/jserv', i.e. requests for '/' transparently
 * get mapped onto '/jserv/frontpage'.
 *
 */

/* Include ISAPI definitions and constants */
#include <httpfilt.h>
#include <string.h>

/* This is the URL that / will be mapped to */
#define NEW_URL "/jserv/SimpleServlet"

/* This function is called when the filter is loaded by the web server */
BOOL WINAPI
GetFilterVersion( HTTP_FILTER_VERSION *ver )
{
  /*  Set the filter version */
  ver->dwFilterVersion = HTTP_FILTER_REVISION;
  /*  Set a description string for the filter */
  strncpy( ver->lpszFilterDesc, "Map / to /dir/file",
	   SF_MAX_FILTER_DESC_LEN );
  /*  We want to hook onto the pre-processing headers stage */
  ver->dwFlags = SF_NOTIFY_PREPROC_HEADERS;

  return TRUE;
}

/*  This function is called for every HTTP request */
DWORD WINAPI
HttpFilterProc( PHTTP_FILTER_CONTEXT pfc,
		DWORD notificationType,
		LPVOID notificationInfo )
{
  char url[1024];
  int i;
  PHTTP_FILTER_PREPROC_HEADERS tbl
    = (PHTTP_FILTER_PREPROC_HEADERS) notificationInfo;

  /* Retrieve the URL asked for */
  if( tbl->GetHeader( pfc, "url", url, &i ) == TRUE &&
      !strcmp( url, "/" )) {
    /* This is a request for '/',
     * transparently rewrite this to 'NEW_URL' */
    tbl->SetHeader( pfc, "url", NEW_URL );
  }

  return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
Content Manager [Administrator] 16 December 2005  Permalink  
Download Free Trial

Recent Articles

Other Resources



www.zeus.com