- 浏览: 13933 次
- 性别:
- 来自: 深圳
最近访客 更多访客>>
最新评论
-
Aether:
Here is include a jsp file!
Thi ...
JSP拾遗(五)——jsp文件包含指令
<%@include file=”fileName” %> 与<jsp:include flush="true" page="fileName">
看到很多教程已经面试中都对jsp中的两个文件包含指令比较纠结,这里就特别对这两个指令做一个比较直观的对比。
参数指定:filename不带路径时默认为当前页目录,如果设置目录,则需以应用的根目录设置地址。jsp:include中的参数flush必须为true,因为在jsp1.1中false是一个无效的设定值。
下面示例,先看一下<%@include %>的处理方式:修改MyJsp.jsp,内容如下:
新建一个include.jsp,内容如下:
浏览器上刷新MyJsp.jsp页面。我们可以看到,页面输出结果为:
由结果可以看出,Myjsp.jsp中不仅输出了include.jsp文件中的内容,而且include.jsp中程序代码也共用了MyJsp.jsp中所定义的字符串test。打开work下的源文件目录,我们可以发现,目录下面并没有include_jsp.java文件。打开MyJsp_jsp.java文件:
从java代码中可以看到,include.jsp中的内容被直接插入到MyJsp.jsp中输出,这个操作过程相当于直接把include.jsp的内容复制到MyJsp.jsp的相应位置,因此include.jsp可以和MyJsp.jsp直接共享所有的对象,和在同一个页面中编码一样。这里应该注意的是,include.jsp中的代码是始终包含在MyJsp.jsp中的,即使把<%@ include %>放在MyJsp.jsp的最末一行,在生成的java文件中,include的内容也将包含在一个_jspService()方法中。因此include.jsp中对公共对象的设置都会对Myjsp.jsp产生影响,特别是当include.jsp中调用response.getOutputStream()输出并关闭输出流后,如果不重新打开输出流,将会引起OutputStream is already closed的IO异常。
将MyJsp.jsp中的包含更改成jsp:include:
再次刷新MyJsp.jsp页面,结果显示如下:
Include.jsp的内容并没有显示出来,查看tomcat控制台我们可以看到,有异常报出: 这里提示test变量未定义。打开work下源代码目录,我们可以查找到include_jsp.java文件,但找不到include_jsp.class文件,因此在编译时出现了异常。很显然,用jsp:include时,所包含的jsp文件不能共用调用者的对象,并且本身就会编译成java类。 查看MyJsp_jsp.java文件,我们发现,这里关于include.jsp的调用,只有一句代码 很明显,jsp:include只调用一个工具方法来显示被包含文件的显示结果。两者的代码互不影响(当然,有三个对象还是传递了过去,你可以在include.jsp中改变这几个对象的属性值),因此include.jsp中的异常并不会影响到MyJsp.jsp文件的正常显示。 介于jsp:include并不能与调用者共享自定义的对象,当调用者需要向被包含页面传入参数时,需要使用<param >标签来传入参数,并在被包含页用request.getParameter()方法来获取。将MyJsp.jsp修改一下: include.jsp更改为: 刷新MyJsp.jsp,页面即正常显示了。Html:
综合上面比较可以看出,<%@ include %>包含文件时,相当于直接把被包含的文件的内容插入到包含页里面,因此被包含页的每次更新,就会导致所有包含页的全部重新编译,而被包含页并不会主动被编译,可以称之为jsp编译指令。而<jsp:include>则相当于直接调用被包含页的输出结果,被包含页的任意更新并不会导致包含页重新编译,可以称之为jsp运行时调用。这也引出了一个几乎所有教程中都会提到的一个结论:在包含不会经常修改到页面时,应使用<%@include %>,在包含经常修改到的页面时,应使用jsp:include。这里的修改指的是文件本身的修改,在实际系统应用中,网站的前台页面因样式等原因修改的几率比较大,因此应该使用jsp:include,那些以html静态页和jsp动态页来区分使用哪个include的方式,显然是错误的。<html>
<body>
This is my first JSP! <br/>
<%
String test ="This is a test value!";
%>
Here is include a jsp file, named include.jsp.<br/>
<%@ include file="include.jsp" %>
</body>
</html>
<!-- start of include.jsp -->
<html>
<body>
This is the include jsp.<br/>
Here will print the String "test" if it's existing. </br>
test = <%= test%>
</body>
</html>
<!-- end of include.jsp -->
This is my first JSP!
Here is include a jsp file, named include.jsp.
This is the include jsp.
Here will print the String "test" if it's existing.
test = This is a test value!
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class MyJsp_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static java.util.Vector _jspx_dependants;
static {
_jspx_dependants = new java.util.Vector(1);
_jspx_dependants.add("/include.jsp");
}
public java.util.List getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("<html>\r\n");
out.write(" <body>\r\n");
out.write(" This is my first JSP! <br/>\r\n");
out.write(" ");
String test ="This is a test value!";
out.write("\r\n");
out.write(" \r\n");
out.write(" Here is include a jsp file, named include.jsp.<br/>\r\n");
out.write(" ");
out.write("<!-- start of include.jsp -->\r\n");
out.write("<html>\r\n");
out.write(" <body>\r\n");
out.write(" This is the include jsp.<br/>\r\n");
out.write(" Here will print the String \"test\" if it's existing. </br>\r\n");
out.write(" test = ");
out.print( test);
out.write("\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
out.write("<!-- end of include.jsp -->");
out.write("\r\n");
out.write(" </body>\r\n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
<html>
<body>
This is my first JSP! <br/>
<%
String test ="This is a test value!";
%>
Here is include a jsp file, named include.jsp.<br/>
<jsp:include flush="true" page="include.jsp" %>
</body>
</html>
This is my first JSP!
Here is include a jsp file, named include.jsp.
2009-7-12 18:47:45 org.apache.jasper.compiler.Compiler generateClass
严重: Error compiling file: /D:/Program/Java/Tomcat5/work/Catalina/localhost/test//org/apache/jsp\include_jsp.java [javac] Compiling 1 source file
D:\Program\Java\Tomcat5\work\Catalina\localhost\test\org\apache\jsp\include_jsp.java:48: 找不到符号
符号: 变量 test
位置: 类 org.apache.jsp.include_jsp
out.print( test);
^
1 错误
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "include.jsp", out, true);
<html>
<body>
This is my first JSP! <br/>
<%
String test ="This is a test value!";
%>
Here is include a jsp file, named include.jsp.<br/>
<jsp:include flush="true" page="include.jsp">
<jsp:param name="test" value="<%=test %>"></jsp:param>
</jsp:include>
</body>
</html>
<!-- start of include.jsp -->
<html>
<body>
This is the include jsp.<br/>
Here will print the String "test" if it's existing. </br>
test = <%= request.getParameter("test")%>
</body>
</html>
<!-- end of include.jsp -->
This is my first JSP!
Here is include a jsp file, named include.jsp.
This is the include jsp.
Here will print the String "test" if it's existing.
test = This is a test value
发表评论
-
Hadoop从零开始(一)——环境准备
2012-07-12 23:17 1469一、Linux虚拟机安装 Hadoop要运行在Uni ... -
JSP拾遗(十四)——单点登录
2009-08-03 09:58 0todo -
JSP拾遗(十三)——文件上传
2009-08-01 19:53 0TODO -
JSP拾遗(十二)——分页查询
2009-08-01 19:52 0TODO -
JSP拾遗(十一)——注册验证
2009-08-01 19:52 0TODO -
JSP拾遗(十)——权限控制
2009-08-01 19:51 0TODO -
JSP拾遗(九)——中文乱码
2009-08-01 19:48 0TODO -
JSP拾遗(八)——Listener
2009-08-01 19:44 2006Javax.servlet中定义了三类监听器:ServletC ... -
JSP拾遗(七)——Filter
2009-08-01 18:48 983一个过滤器必须要实现javax.servlet.Filter, ... -
JSP拾遗(六)——Servlet
2009-08-01 18:42 936在典型的J2EE中,我们的应用是通过应用服务器(如TOMCAT ... -
JSP拾遗(四)——jsp页面标签
2009-07-10 22:41 1871第二节中,我们通过JSP编译成的java源文件对比出了 ... -
JSP拾遗(三)——jsp内置对象
2009-07-08 22:40 1195在看本章介绍的jsp ... -
JSP拾遗(二)——从jsp到servlet
2009-06-16 22:08 1763无代码无真相。这里我们来看看,一个简单的jsp页面, ... -
JSP拾遗(一)——初识JSP
2009-06-16 21:53 1141我的java编程学习,即是从jsp开始。 什么是jsp ...
相关推荐
jsp网络编程——多媒体文件(7),对初学者很有帮助的
jsp网络编程——多媒体文件(6),对初学者有帮助
jsp网络编程——多媒体文件(8),对学习用帮助
JSP源码——JSP Explorer 文件浏览器 v1.0_fileexplorer.zip
JSP核心技术——JSP引擎内幕(2010).pdf 内除了引擎外,还有jsp标签、线程安全
jsp毕业设计——jsp报名系统.zip
JSP技巧收集——超多超赞的JSP实用小技巧哦!
jsp网络编程——源文件(06——09)帮助
jsp网络编程——源文件(10——11)帮助
jsp网络编程——源文件(12——13),帮助
jsp网络编程——源文件(01——05),帮助
jsp毕业设计——jsp网上选课系统.zip
JSP网络编程——多媒体(1——3),对出学者很有帮助的。
jsp毕业设计——jsp企业人事管理系统.zip
JSP扩展——自定义标签JSP扩展——自定义标签JSP扩展——自定义标签JSP扩展——自定义标签JSP扩展——自定义标签
jsp毕业设计——jsp商品进销存系统.zip
jsp毕业设计——jsp-企业人事管理系统.zip
用JSP写的——企业人力资源系统,希望大家不要错过!经典啊,经典
jsp开发实例——购物车,是一个综合开发实例。比较有针对性。
用eclipse集成开发环境, jsp 过滤器——解决中文乱码问题