Server Side Include in GlassFish

Server Side Include (SSI) allows including dynamic contents in html. SSI and CGI were very popular before the the appearance of JSP and PHP. The SSI code in GlassFish workspace is based on Tomcat. In GlassFish v3, SSI will be a supported feature. Let us look at a very simple example.

Create a SSI file

In our example, we create a index.shtml which
  • includes the content of header.html,
  • prints a Hello message with server side timestamp, and
  • executes a command say, uname (or any command in your operating system).
The page is as follows:

    <!--#include virtual="header.html"-->
    <br>Hello, it is <!--#echo var="DATE_LOCAL"-->.
    <br>Result: <!--#exec cmd="uname"-->

Note that the extension shtml is configurable in web.xml (see servlet-mapping below).

Enable SSI processing

The SSI processing can be enabled in a war file by adding SSIServlet et al in web.xml as follows:

  <web-app>
    <servlet>
      <servlet-name>ssi</servlet-name>
      <servlet-class>org.apache.catalina.ssi.SSIServlet</servlet-class>
    </servlet>

    <servlet-mapping>
      <servlet-name>ssi</servlet-name>
      <url-pattern>*.shtml</url-pattern>
    </servlet-mapping>

    <mime-mapping>
      <extension>shtml</extension>
      <mime-type>text/html</mime-type>
    </mime-mapping>
  </web-app>

One can find more details about the configuration of SSIServlet in the section below. Alternatively, one can enable SSI by uncommenting the corresponding sections in default-web.xml.

Note that the mime-mapping is to notify the browser that the result of shtml file is of content-type: text/html. If you don't specify this, then GlassFish will try to get the mime-type from default-web.xml or the default in the system.

Configuration of SSIServlet

One can configure SSIServlet by specifying the init-param as follows:

init-paramTypeDescriptionDefault
bufferedboolean (or String converted to boolean)whether the output should be bufferedfalse
debugintrepresents debug level0 (no debug)
expiresLongexpiration time in secondsdo not set "Expires" header in Http Response
inputEncodingStringencoding for SSI input if there is no URL content encoding specifiedserver platform encoding
isVirtualWebappRelativeboolean (or String converted to boolean)whether the "virtual" path of "#include" directive is relative to content-rootfalse (means relative to the given SSI file)
outputEncodingStringencoding for SSI outputUTF-8
Comments:

Tried your code above. Still not working.
We have multiple web apps running on Sun 1. I am building a new Glasfissh v2.1 server.
A line from one of our html files;
<!--#include virtual="/articles/gear/header.html" -->

I added your code to default-web.xml and restarted Glasfish.
I still see the <!--#include virtual=" /articles/gear/header.html" --> in the view source results which means that Glassfish is not processing the SSI.

Section added to default-web.xml;
<!-- -->
<!-- Server Side Includes processing servlet, which processes SSI -->
<!-- directives in HTML pages consistent with similar support in web -->
<!-- servers like Apache. Traditionally, this servlet is mapped to the -->
<!-- URL pattern "\*.shtml". This servlet supports the following -->
<!-- initialization parameters (default values are in square brackets): -->
<!-- -->
<!-- buffered Should output from this servlet be buffered? -->
<!-- (0=false, 1=true) [0] -->
<!-- -->
<!-- debug Debugging detail level for messages logged -->
<!-- by this servlet. [0] -->
<!-- -->
<!-- expires The number of seconds before a page with SSI -->
<!-- directives will expire. [No default] -->
<!-- -->
<!-- isVirtualWebappRelative -->
<!-- Should "virtual" paths be interpreted as -->
<!-- relative to the context root, instead of -->
<!-- the server root? (0=false, 1=true) [0] -->
<!-- -->
<!-- inputEncoding The encoding to assume for SSI resources if -->
<!-- one is not available from the resource. -->
<!-- [Platform default] -->
<!-- -->
<!-- outputEncoding The encoding to use for the page that results -->
<!-- from the SSI processing. [UTF-8] -->
<!-- -->
<!-- -->

<servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>
org.apache.catalina.ssi.SSIServlet
</servlet-class>
<init-param>
<param-name>buffered</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>expires</param-name>
<param-value>666</param-value>
</init-param>
<init-param>
<param-name>isVirtualWebappRelative</param-name>
<param-value>1</param-value>
</init-param>
<load-on-startup>3</load-on-startup>

<servlet-mapping>
<servlet-name>ssi</servlet-name>
<url-pattern>\*.html</url-pattern>
</servlet-mapping>

<mime-mapping>
<extension>shtml</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
</servlet>

The url www.insure.com/articles/homeinsurance will show U what it is supposed to look like. All I get on Glassfish is the content of the article with the left header, top header, or right side footer missing.

Any help would be much appreciated.
Thanks,
CalvaryMike

Posted by CalvaryMike on July 17, 2009 at 02:43 AM PDT #

SSI is a support feature in GlassFish v3. In the above servlet-mapping, I have url-pattern \*.shtml. But in yours, the url-pattern is \*.html. Also, you can tried to put the above xml in web.xml rather than default-web.xml. I don't think it is a good idea to have SSI turned on in default-web.xml.

Posted by Shing Wai Chan on July 20, 2009 at 03:28 AM PDT #

I have always been unable to get SSI to work. Any time something changes, it stops working. SO, these days, we have been generating the web pages using biterscripting with the sal command ( http://www.biterscripting.com/helppages/sal.html ). Stands for string alterer. It basically replaces the #include X part by the contents of file X.

Posted by PatrickMc on November 29, 2009 at 04:33 AM PST #

I have verified that SSI is working fine in GlassFish v3. The biterScripting mentioned about is not SSI. SSI has its own syntax, see http://en.wikipedia.org/wiki/Server_Side_Includes .

Posted by Shing Wai Chan on November 29, 2009 at 10:57 AM PST #

Post a Comment:
Comments are closed for this entry.
About

Shing Wai Chan

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today