@include指令和jsp:include动作的区别 以及 Web容器处理JSP文件请求的执行过程

Think of the include directive as a header file of languages like C/C++. When using the include directive, the combined contents of the original JSP page and all its included resources are translated into the same implementation servlet i.e. the contents of the included resource is copy pasted before translation to a servlet. The important thing here is that all this happens during translation time and only static content is supported.

The include action <jsp:include> is almost similar to the include directive except for a few subtle differences. The include action is executed at request time as opposed to translation time. Thus instead of the content of the include file being included in the calling JSP, the output of the JSP is included or passed to the JSPWriter of the calling page. This allows us to include both static and dynamic content into the JSP page. Using the jsp action also allows you to explicitly pass parameters to the included page.

    <jsp:include page="/index.jsp">
    <jsp:param name="name" value="sos" />
    </jsp:include>


区别:
一:执行时间上:
<%@ include file=”relativeURI”%> 是在翻译阶段执行
<jsp:include page=”relativeURI” flush=”true” /> 在请求处理阶段执行.

二:引入内容的不同:
<%@ include file=”relativeURI”%>
只能引入静态内容(html,jsp)
在翻译阶段,所有通过include指令导入的内容会百合成,并翻译成一个统一的Servlet
<jsp:include page=”relativeURI” flush=”true” />
能同时引入静态内容和动态内容
在请求处理,引入执行页面或servlet所生成的应答文本到目标页面的JSPWriter中



Web容器处理JSP文件请求的执行过程主要包括以下4个部分:
1.客户端发出Request请求
2.JSP Container 将JSP转译成Servlet的源代码
3.将产生的Servlet源代码经过编译后,并加载到内存执行
4.把结果Response(响应)至客户端

在执行JSP网页时,通常可以分为两个时期:翻译时期(Translation Time)和请求时期(Request Time)。

◆翻译时期:JSP网页转移成Servlet类。
◆请求时期:Servlet类执行后,响应结果至客户端。

翻译期间做了两件事情:
◆翻译时期:将JSP网页转移为Servlet源代码 .java.
◆编译时期:将Servlet 源代码 .java编译成 Servlet类 .class.

当JSP网页在执行时,JSP Container会做检查工作,如果发现JSP网页有更新修改时,JSP Container才会再次编译JSP成Servlet; 如果JSP没有更新时,就直接执行前面所产生的Servlet。

猜你喜欢

转载自desert3.iteye.com/blog/1171422