Server Side Include (SSI) in Servlet

Server Side Include (SSI) in Servlet

In this article, I am going to discuss Server Side Include (SSI) in Servlet. Please read our previous article where we discussed the Single Thread Model Interface in Servlet with Examples

Why Server Side Include (SSI) in Servlet?

All the servlets you’ve been so far generating full HTML pages. If this were all that servlets could do, it would still be plenty. Servlets, however, can also be embedded inside HTML pages with something called server-side include (SSI) functionality. In many servers that support servlets, a page can be preprocessed by the server to include output from servlets at certain points inside the page.

Syntax:

<SERVLET CODE=ServletName CODEBASE=path initparam1=initparamvalue initparam2=initparam2value>
<PARAM NAME=name1 VALUE=value1>
<PARAM NAME=name2 VALUE=value2>
</SERVLET>

Here the path indicates the ServletName class name path in the server. You can set up the remote file path also by the following syntax:

http://server:port/dir

The code attributes specify the class name or registered name of the servlet to invoke. The CODEBASE attribute is optional. Without a CODEBASE attribute, the servlet is assumed to be local. Any number of parameters may be passed to the servlet using the <PARAM> tag. The servlet can retrieve the parameter values using the getParameter() method of ServletRequest.

A server that does not support SSI detects the <SERVLET> tag in the process of returning the page and substitutes in its place the output from the servlet. The server does not parse every page it returns, it parses only the pages with a .shtml extension.

Server Side Include (SSI) in Servlet

Example of Server Side Include (SSI) in Servlet

The example will show the current time here. First, we will write the “index.shtml” file to call the servlet class. After that, the web server receives the SERVLET tag and process the SSI. First, it searches the server container has the SSI servlet class i.e. shtml file has the CODE attribute value as treated as the SSI servlet class. Then the file found processes the SSI servlet. The SSI servlet uses either doget() or service() method in the request process.

index.shtml
<HTML>
<HEAD><TITLE>Times!</TITLE></HEAD>
<BODY>
    The current time here is:
    <!-- here the ssi servlet class has been called-->
    <SERVLET CODE=CurrentTime>
    </SERVLET>
</BODY>
</HTML>
CurrentTime.java
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CurrentTime extends HttpServlet {
 private static final long serialVersionUID = 1L;
        public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 
        {
             PrintWriter out = res.getWriter();
             Date date = new Date();
             DateFormat df = DateFormat.getInstance();
             // Here write the response shtml file
             out.println("The current time here is:");
             out.println(df.format(date));
        }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
  <servlet-name>CurrentTime</servlet-name>
  <servlet-class>CurrentTime</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>CurrentTime</servlet-name>
  <url-pattern>/index.shtml</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.shtml</welcome-file>
 </welcome-file-list>
</web-app>

Output

Example of Server Side Include (SSI) in Servlet

In the next article, I am going to discuss Servlets Debugging. Here, in this article, I try to the Server Side Include (SSI) in Servlet with Examples. I hope you enjoy this Server Side Include (SSI) in the Servlet article.

Leave a Reply

Your email address will not be published. Required fields are marked *