码迷,mamicode.com
首页 > 其他好文 > 详细

【转】 SSI(Server Side Includeds)使用详解

时间:2014-08-27 12:37:07      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   os   java   使用   io   for   文件   

SSI使用入门:

什么是SSI技术?

SSI(Serve Side Includes),通常称之为服务器端包含,使用SSI技术文件默认的后缀名为.shtml,SSI技术通过在html文件中加入SSI指令,让服务器端在输出html之前解释SSI指令,并把解释完的结果和html代码一同输出给客户端。SSI指令实在服务器端执行的。

在大部分项目中我们主要使用SSI的包含指令<!--#include virtual="/globle/foot.jsp"-->

标签。

为什么要使用SSI技术?

既然jsp中的<jsp:include page="/globle/foot.jsp"/>标准动作,也提供了类似的功能。为什么我们还要使用SSI技术?

主要原因有一下几点:

1,首先SSI技术是通用技术,不局限于运行环境,在java dotne CGIASPPHP都 可以使用SSI技术。

2,解释SSI指令要比解释JSP的效率要高,因为servlet规范提供了太多的功能。这些功能都需要好servlet引擎一一进行解释,所以效率比较低。

在目前大部分门户网站都是用了SSI技术,解释SSI文件的最佳服务器是Apache Http Server。大型门户网站基本上都采用Apache Server来解释SSI文件

Tomcat6.x中怎么应用SSI技术:

介绍:

SSI(服务器端包含)放置在HTML页面的指令,他们让你动态生成的内容添加到现有的HTML页面,而无需通过一个CGI程序,或其他动态技术服务整个页面。当你使用Tomcat作为你的HTTP服务器,并且需要SSI支持,你能够在Tomcat下增加SSI支持。通常,这是在开发过程中,当你不想要运行一个象Apache Web服务器(Apache Http Server)TomcatSSI支持实现Apache(Apache Http Server)相同的SSI指令。如果在运营网站的时候应该使用Apache Http Server服务器解释SSI指令,如果使用Tomcat作为解释SSI指令的服务器,虽然可以实现但是效率低。启用SSI支持可以通过一个servletFilter,但是你只能选一种方式,要么是servlet或者是filter

基于servletSSI支持已经被实现通过类org.apache.catalina.ssi.SSIServlet。习惯上,servlet的映射URL路径为"*.shtml"

基于filterSSI支持已经被实现通过类org.apache.catalina.ssi.SSIFilter。习惯上,servlet的映射URL路径为"*.shtml"。尽管它可以映射到“*”,因为它会选择性地启用/禁用SSI的处理基于MIME类型,contentType的初始化参数允许您应用SSI处理JSP页面,JavaScript,或者你想要的任何其他内容。

Tomcat默认情况下禁用SSI支持。

环境搭建:

警告:

可以使用SSI指令的执行程序到Tomcat JVM外部。如果你是使用Java SecurityManager,这将绕过你的安全策略配置在catalina.policy里。

要使用SSIservlet,删除在$ CATALINA_BASE / conf / web.xmlSSI servletservlet的映射周围的XML注释。

要使用SSIservlet,删除在$ CATALINA_BASE / conf / web.xmlSSI 过滤器和过滤器的映射周围的XML注释。

只有标记为特权的上下文可以使用SSI功能(见Context元素的特权属性)。在context.xml文件中配置如下:<Context privileged="true"></Context>

servlet配置:

有几个servlet初始化参数可用于配置的SSI servlet的行为:

debug:通过这个servlet为日志信息调试详细级别。 Default 0

expires:在一个带有SSI指令的页面将要过期的秒数,默认的行为是为所有的SSI指令评估每次请求。

isVirtualWebappRelativeSSI指令“virtual”路径被解释为相对与上下文的根目录,而不是服务器的根目录?(0=false, 1=true) Default 0 (false)

inputEncoding :必须为SSI的资源指定编码,如果不能从资源本身决定。默认值是默认的平台编码。

outputEncoding :用于编码的SSI处理的结果。默认是UTF - 8

allowExec exec命令启用了吗?默认值是false

filter配置:

有几个filter初始化参数可用于配置的SSI filter的行为:

contentType SSI的处理被应用之前,必须匹配一个正则表达式模式。 Default is "text/x-server-parsed-html(;.*)?". 

debug :通过这个servlet为日志信息调试详细级别。 Default 0

expires :在一个带有SSI指令的页面将要过期的秒数,默认的行为是为所有的SSI指令评估每次请求。

isVirtualWebappRelative SSI指令“virtual”路径被解释为相对的上下文根目录,而不是服务器的根目录?(0=false, 1=true) Default 0 (false)

allowExec exec命令启用了吗?默认值是false

指令:

指令采取的HTML注释的形式,该指令被替换为在发送页面给客户端之前的解释SSI的结果。

指令的一般形式是:<!--#directive [parm=value] -->

config :<!--#config timefmt="%Y-%m-%d %H:%M:%S" -->格式化时间,本身不输出,要放在输出时间指令的上面,才有效。

echo <!--#echo var="VARIABLE_NAME" -->输出环境变量对应的值,环境变量有SSI提供。

include <!--#include virtual="file-name" --><!--#include file="file-name" -->包含文件。

区别:file文件名是一个相对路径,该路径相对于使用#include文件所在的目录,被包含文件可以是同以目录或者子目录。但不能是上一级目录;

        Virtual文件名是web站点的虚拟完整路径,如表示服务器文档根目录下global目录下的foot.jsp,则为<!--#include virtual="/globle/foot.jsp" -->

flastmod <!--#flastmod file="filename.shtml" -->文件最后修改时间。

fsize <!--#fsize file="filename.shtml" -->返回文件大小。

printenv: <!--#printenv -->返回所有定义的变量列表。

set <!--#set var="foo" value="Bar" -->把值赋给定义的变量。此外仅设置值字面上一样,除了仅仅简单的设置值外,你可以使用任何其他变量,包括环境变量。您将指定的东西是一个变量,而不是一个简单字符串,通过在变量的名称前加上美元符号。

if elif endif else :类似于java的条件语句。

例如:

为了把美元符号放入变量的值,你需要用一个反斜线转译美元符号

<!--#set var="cost" value="\$100" --> 

<!--#set var="date" value="${DATE_LOCAL}_${DATE_GMT}" --> 

<!--#if expr="$DATE_LOCAL = /Monday/" -->

<p>Meeting at 10:00 on Mondays</p>

<!--#elif expr="$DATE_LOCAL = /Friday/" -->

<p>Turn in your time card</p>

<!--#else -->

<p>Yoga class at noon.</p>

<!--#endif --> 

环境变量:

Variable Name

Description

AUTH_TYPE

The type of authentication used for this user: BASIC, FORM, etc.

CONTENT_LENGTH

The length of the data (in bytes or the number of characters) passed from a form.

CONTENT_TYPE

The MIME type of the query data, such as "text/html".

DATE_GMT

Current date and time in GMT

DATE_LOCAL

Current date and time in the local time zone

DOCUMENT_NAME

The current file

DOCUMENT_URI

Virtual path to the file

GATEWAY_INTERFACE

The revision of the Common Gateway Interface that the server uses if enabled: "CGI/1.1".

HTTP_ACCEPT

A list of the MIME types that the client can accept.

HTTP_ACCEPT_ENCODING

A list of the compression types that the client can accept.

HTTP_ACCEPT_LANGUAGE

A list of the languages that the client can accept.

HTTP_CONNECTION

The way that the connection from the client is being managed: "Close" or "Keep-Alive".

HTTP_HOST

The web site that the client requested.

HTTP_REFERER

The URL of the document that the client linked from.

HTTP_USER_AGENT

The browser the client is using to issue the request.

LAST_MODIFIED

Last modification date and time for current file

PATH_INFO

Extra path information passed to a servlet.

PATH_TRANSLATED

The translated version of the path given by the variable PATH_INFO.

QUERY_STRING

The query string that follows the "?" in the URL.

QUERY_STRING_UNESCAPED

Undecoded query string with all shell metacharacters escaped with "\"

REMOTE_ADDR

The remote IP address of the user making the request.

REMOTE_HOST

The remote hostname of the user making the request.

REMOTE_PORT

The port number at remote IP address of the user making the request.

REMOTE_USER

The authenticated name of the user.

REQUEST_METHOD

The method with which the information request was issued: "GET", "POST" etc.

REQUEST_URI

The web page originally requested by the client.

SCRIPT_FILENAME

The location of the current web page on the server.

SCRIPT_NAME

The name of the web page.

SERVER_ADDR

The server‘s IP address.

SERVER_NAME

The server‘s hostname or IP address.

SERVER_PORT

The port on which the server received the request.

SERVER_PROTOCOL

The protocol used by the server. E.g. "HTTP/1.1".

SERVER_SOFTWARE

The name and version of the server software that is answering the client request.

UNIQUE_ID

A token used to identify the current session if one has been established.

SSI是当然不是对CGI或其他的技术用于生成动态网页的替代品,不过这是一个很好的方式对于往页面加入少量的动态的内容,并且不用做额外的工作。

【转】 SSI(Server Side Includeds)使用详解

标签:des   style   http   os   java   使用   io   for   文件   

原文地址:http://www.cnblogs.com/naruto469/p/3939058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!