Jsp 中 include 指令和 include 动作的区别

来源:互联网 发布:魅族官方网络授权店 编辑:程序博客网 时间:2024/05/16 07:19


include指令与<jsp:include>动作的区别:

(1)include 指令只能引入遵循 JSP 格式的文件,被引入的文件与当前 JSP 文件需要共同合并后才能翻译成一个 Servlet 源文件,最终编译的文件只有一个;<jsp:include> 动作要引入的资源和当前 JSP 页面是两个彼此独立执行实体,即被引入的资源必须能够被 Web 容器独立执行,最终分别对两个文件进行编译。

 

(2)include 指令引入的资源是在编译时期包含的,包含的是源代码(静态包含);<jsp:include> 动作要引入的资源是在运行时才包含的,而且只包含运行结果(动态包含)。

 

(3)<jsp:include> 动作运行原理和 RequestDispatcher.include 方法类似,即被包含的页面不能改变响应状态码或者设置响应头;而 inlcude 指令则没有此限制。

 

注意:(使用<jsp:include>动作通常是包含那些经常改动的文件,因为被包含的文件改动不会影响到包含文件,因此不需要对包含文件进行重新编译)

 

(4)这里有一个简单的例子,对上面的第(1)(2)点证明

① data.jsp

  1. <%
  2.     int num = 1;
  3. %>

②include 指令案例,include_command.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Include 指令</title>
  8. </head>
  9. <body>
  10.     <%@ include file="data1.jsp" %>
  11.     <%
  12.         out.print("num="+num);
  13.     %>
  14. </body>
  15. </html>

这里可以正常输出


③<jsp:include> 动作案例, include_action.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2.     pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Include 动作</title>
  8. </head>
  9. <body>
  10.     <jsp:include page="data1.jsp" flush="true"/>
  11.     <%
  12.         out.print("num="+num);
  13.     %>
  14. </body>
  15. </html>

在 Eclipse 下出现小红叉,看来过不了编译。最终显示500,因为没有初始化 num 变量


 

(5)最后给一个案例

 include.jsp

  1. <%@ page language"java" contentType="text/html;charset=UTF-8" %>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8">
  5.         <title>JSPinclude动作实例</title>
  6.     </head>
  7.     <body>
  8.         <%@ include file = "Static.txt" %>
  9.         <jsp:include page="Dyamic.jsp" flush="true"></jsp:include>
  10.     </body>
  11. </html>

Static.txt

  1. <%@ page language"java" contentType="text/html;charset=UTF-8" %>
  2. <form action="JSPIncludeActiveDemo.jsp" method=post>
  3.     用户名:    <input type=text namename=name><br>
  4.     密码:    <input type=password name=password><br>
  5.     <input type=submit value=登录>
  6. </form>

Dyamic.jsp

  1. <%@ page language"java" contentType="text/html;charset=UTF-8" %>
  2. <br>
  3. 用户名:<%=request.getParameter("name") %>
  4. <br>
  5. 密码:<%=request.getParameter("password") %>
  6. <br>

 

本文链接:https://liuyanzhao.com/5147.html

原创粉丝点击