Server Side Includes are commands and directives placed in the web
page that can be evaluated by the web server when the web page being
served. The servlet code embedded inside the HTML code called as SSI.
SSI cant be supported for all web server. so you can read web
server supported documents before using the SSI in your code.
It helpful for when you want to small part of page loading
dynamically instead of whole page loading. Below the Syntex given,
<SERVLET CODE=MyservletClassname CODEPASE=path initparam1=initparamvalue initparam2=initparam2value>
<PARAM NAME=name1 VALUE=value1>
<PARAM NAME=name2 VALUE=value2>
</SERVLET>
Here the path indicate the MyservletClassname class name path in the server. you can setup the remote file path also. the remote file path syntex is,
http://server:port/dir
Note: If the above text show in the HTML page means the web server not support the <SERVLET> tag.
You can pass the any no of PARAM in that SERVLET. It can be accessed by using getParameter() method in ServletRequest class.
You can save the SSI contains html file using .shtml extension.
Program for SSI :-
The Example can be showing the current time for here and the time in London and Newyork.
First to write the the shtml file to call the servlet class.
<HTML>
<HEAD><TITLE>Times!</TITLE></HEAD>
<BODY>
The current time here is:
<!-- here the ssi servlet class has been called-->
<SERVLET CODE=CurrentTime>
</SERVLET>
<P>
The current time in London is:
<!-- here the ssi servlet class has been called and send the parameter-->
<SERVLET CODE=CurrentTime>
<PARAM NAME=zone VALUE=GMT>
</SERVLET>
<P>
And the current time in New York is:
<!-- here the ssi servlet class has been called and send the parameter-->
<SERVLET CODE=CurrentTime>
<PARAM NAME=zone VALUE=EST>
</SERVLET>
<P>
</BODY>
</HTML>
After that the web server receive the SERVLET tag and process the SSI. First it search the server container has the SSI servlet class i.e .shtml file has the CODE attribute value as treated as SSI servlet class.
Then the file found it process the SSI servlet. The SSI servlet use either doget() or service() method in request process.
CurrentTime SSI servlet class are given below,
package com.shinnedhawks;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Getting date based on time zone
* @author Ramachandran (ramakavanan@gmail.com)
*/
public class CurrentTime extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
Date date = new Date();
DateFormat df = DateFormat.getInstance();
// Here receive the request param from .shtml file
String zone = req.getParameter("zone");
if (zone != null) {
TimeZone tz = TimeZone.getTimeZone(zone);
df.setTimeZone(tz);
}
// Here write the response shtml file
out.println(df.format(date));
}
}
This SSI servlet we can change the printwriter class initialization in the following way also to write the response in that .shtml file,
PrintWrite out= new PrintWriter(res.getOutputStream(), true);
This out put will be shown in browser that replace the SERVLET tag and write the response return form the SSI servlet class,
For example output is,
The current time here is: 6/5/15 1:00 PM
The current time in London is: 6/6/15 1:00 AM
And the current time in New York is: 6/5/15 9:00 PM
No comments:
Post a Comment