Server-Side Include (SSI) allows us to embed (include) servlets within HTML pages. The HTML files containing servlets typically have the extension .shtmi. The web server understands this extension and forwards it to an internal servlet. This internal servlet parses the HTML document and executes the referenced servlets and sends the result back to the web server after merging it with the original HTML document. This lets us include dynamically generated content in an existing HTML file, without having to serve the entire page via a CGI program or other dynamic technologies.
Tomcat uses org.apache.cataiina.ssi.ssiServiet as its internal servlet. To enable SSI support, remove the XML comments from around the SSI servlet and servlet-mapping configuration in $tomcat_ HOME/conf/web.xml. Also modify the <context> tag in $TOMCAT_HOME/conf/context.xml as follows:
<Context privileged=”true”>
…
<WatchedResource>WEB-INF/web.xml</WatchedResource>
…
</Context>
The syntax for including servlets in HTML documents varies widely from web server to web server. Tomcat SSI directives have the following syntax:
<!–#directive attribute=”value” attribute=”value” … –>
Tomcat SSI directives are formatted like an HTML comment. So, if SSI is not correctly configured, the browser will ignore it. Otherwise, the directive will be replaced by its results before sending the page to the client. A brief description of the directives is given below:
echo
It just inserts the value of a variable. For example, the following directive inserts the value of the local date.
<!–#echo var=”DATE_LOCAL” –>
This generates the following result:
Sunday, 11-Aug-2013 04:10:02 PDT
There are a number of standard variables including all the environment variables that are available to servlets. Additionally, you can define your own variables with the set directive. A list of SSI servlet variables is given in Appendix A at the end of this chapter.
printenv
It returns the list of all defined variables.
<!–#printenv –>
It results in the following:
HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/28.0.1500.95 Safari/537.36 HTTP_ACCEPT_LANGUAGE=en- US,en;q=0.8
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
LAST_MODIFIED=08/11/13 DOCUMENT_URI=/net/SSI.shtml
HTTP_ACCEPT_ENCODING=gzip,deflate,sdch REMOTE_PORT=56237 SERVER_SOFTWARE=Apache
Tomcat/8.0.0-RC1 Java HotSpot(TM) Client VM/23.3-b01 Windows 7 SERVER_NAME=172.16.5.81
SCRIPT_FILENAME=D:\apache-tomcat-8.0.0- RC1\webapps\net\SSI.shtml DATE_LOCAL=08/11/13
SERVER_ADDR=172.16.5.81 SERVER_PROTOCOL=HTTP/1.1 REQUEST_METHOD=GET
DOCUMENT_NAME=SSI.shtml SERVER_PORT=8080 SCRIPT_NAME=/SSI.shtml REMOTE_ADDR=172.16.5.81
DATE_GMT=08/11/13 REMOTE_HOST=172.16.5.81 HTTP_HOST=172.16.5.81:8080 HTTP_CONNECTION=keep-alive UNIQUE_ID=BEBC7FB2030AF4392C5871ED88FBCFB1 QUERY_STRING= GATEWAY_INTERFACE=CGI/1.1 org.apache.catalina.ssi.SSIServlet=true REQUEST_URI=/net/SSI.shtml
config
We can use the config directive, with a timefmt attribute, to modify the formatting of the date and time as follows:
<!–#config timefmt=”%A %B %d, %Y” –>
<!—#echo var=”DATE_LOCAL” —>
This generates the following result:
Sunday August 11, 2013
fsize
This directive returns the size of the file specified by the file attribute.
<!–#fsize file=”SSI.shtml” –>
This directive returns the size of the file ssi.shtml in kilobytes.
flastmod
The last modification time of a file is inserted using flastmod directive whose file attribute specifies the file name. The following code inserts the last modification time of the file named ssi.shtml.
<!–#flastmod file=”SSI.shtml” –>
You need to replace the SSi.shtml with the actual file name you want to refer to. Note that this piece of code does not give the last modification time of an arbitrary file. It is better to use the echo directive to print the value of the last_modified environment variable, which specifies the last modification time of the file where this directive is included.
<!—#echo var=”LAST_MODiFiED” —>
include
The most common use of SSI is to include the result of a server-side program such as servlet or CGI. The following directive includes the result of our HeiioWorid servlet in the place of the directive.
<!–#include virtual=”servlet/HelloWorld” –>
The attribute virtual specifies the server-side script to be used. Note that the servlet URL used here is relative to the .shtml file containing this directive.
So, create a file ssi.shtml containing a single line as follows and put in the $tomcat_home\ webapps\net directory.
<!–#include virtual=”servlet/HelloWorld” –>
It generates the same output as our HelloWorld servlet, as shown in Figure 20.9.
The include directive is useful if you want to insert the result of the same servlet in different pages. Use the following to insert a header in every page.
<!–#include virtual=”header.html” –>
Note that the include directive can insert the result of any type of file. The following example
includes two html files header.html and footer.html.
<!–#include virtual=”header.html” –>
SSI allows us to include other documents within HTML pages.
<!–#include virtual=”footer.html” –>
It first inserts the content of header.html whose content is shown below:
<!–header.html–>
<h1>SSI: Server Side Include</h1>
Then a single line of text is inserted. Finally, it inserts the content of another file footer.html whose content is shown below:
<!–header.html–>
<h4>Note:syntax of SSI varies widely from web server to web server. </h4>
A sample output is shown in Figure 20.10:
set
It is used for creating variables and setting their values for later use. The following directive creates a variable SSI with the value “Server Side Include”.
<!–#set var=”SSI” value=”Server Side Include” –>
Similarly, the following code creates a variable sum with the value “0”.
<!–#set var=”sum” value=”0″ –>
set
It is used for creating variables and setting their values for later use. The following directive creates a variable SSI with the value “Server Side Include”.
<!–#set var=”SSI” value=”Server Side Include” –>\
Similarly, the following code creates a variable sum with the value “0”.
<!–#set var=“sum” value=“0” –>
set
It is used for creating variables and setting their values for later use. The following directive creates a variable SSI with the value “Server Side Include”.
<!–#set var=“SSI” value=“Server Side Include” –>
if elif endif else
This directive is used to write conditional sections. For example:
<!–#config timefmt=”%A” –>
<!—#if expr=”$DATE_LOCAL = /Sunday/” —>
<p>Today is Sunday</p>
<!—#elif expr=”$DATE_LOCAL = /Saturday/” —>
<p>Today is Saturday</p>
<!–#else –>
<p>Week day</p>
<!–#endif –>
Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.