JSP之 @include VS jsp:include

阅读更多
第一篇

对于可以重复使用的一段代码,
1、使用 <%@include file="reuse.html"%> 在jsp中进行引用。
2、使用 在jsp中进行引用。

reuse.html 代码:


    reusable
    


     

As the candle burns,so do I



分别访问JSP后,你会得到相同的结果。
然后开始想:这两种引用文件的方式有何不用?

当你查看 jsp 被编译后而生成的 servlet 文件,你会看到不同。

1、使用 <%@include 指令时,所看到编译后的的文件:
out.write("\r\n");
out.write("    \r\n");
out.write("        reusable\r\n");
out.write("        \r\n");
out.write("    \r\n");
out.write("    \r\n");
out.write("         
\r\n"); out.write("

As the candle burns,so do I

\r\n"); out.write(" \r\n"); out.write("\r\n");


2、使用
org.apache.jasper.runtime
    .JspRuntimeLibrary.include(request, response, "reusable.html", out, false);


现在可以看出:

include 指令引入的是原文件的本身到当前的 jsp 中。
所以称作“静态引用” —— 原文件是什么样,引入时就是什么样。

include 标签是执行了方法的调用(invoke call jsp method),
然后传递了 request,response,out 参数过去。
所以称作“动态引用” —— 引入的是 jsp 文件执行过程中的结果。



所引用的文件,对于变量的范围:
<%@include 指令:的方式,通常把一些常用的全局变量,引入到jsp文件中。


所引用的 JSP 文件,对于只是为了输出单纯的文本:
两种引用方式没有任何区别。




第二篇
Reusing Content in JSP Pages
在JSP 中重复使用相同的内容或代码

方法:
- 使用 @include 指令
- 使用 jsp:include 标签


@include 引用的内容,会与 jsp 一起被编译成 一个 servlet class
这里只有一个 jsp class

引用

The include directive is processed when the JSP page is translated into a servlet class. The effect of the directive is to insert the text contained in another file (either static content or another JSP page) into the including JSP page. You would probably use the include directive to include banner content, copyright information, or any chunk of content that you might want to reuse in another page. The syntax for the include directive is as follows:

<%@ include file="filename" %>
For example, all the Duke’s Bookstore application pages could include the file banner.jspf, which contains the banner content, by using the following directive:

<%@ include file="banner.jspf" %>
Another way to do a static include is to use the prelude and coda mechanisms described in Defining Implicit Includes. This is the approach used by the Duke’s Bookstore application.

Because you must put an include directive in each file that reuses the resource referenced by the directive, this approach has its limitations. Preludes and codas can be applied only to the beginnings and ends of pages. For a more flexible approach to building pages out of content chunks, see A Template Tag Library.




jsp:include 引用的是 jsp 编译完成后,jsp 执行后的结果。
这里会有多个 jsp class
jsp 处于不用的上下文环境,注意参数的传递。

引用

The jsp:include element is processed when a JSP page is executed. The include action allows you to include either a static or a dynamic resource in a JSP file. The results of including static and dynamic resources are quite different. If the resource is static, its content is inserted into the calling JSP file. If the resource is dynamic, the request is sent to the included resource, the included page is executed, and then the result is included in the response from the calling JSP page. The syntax for the jsp:include element is:


The hello1 application discussed in Packaging Web Modules uses the following statement to include the page that generates the response:










-
引用请注明:
原文出处:http://lixh1986.iteye.com/blog/2379902




-

引用:

http://docs.oracle.com/javaee/5/tutorial/doc/bnajb.html

what-is-the-difference-between-jspinclude-page-and-include-file
https://stackoverflow.com/questions/7879906









-

你可能感兴趣的:(jsp,include,动作,指令,引用)