include指令与include动作

1、样例代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import = "java.io.*,java.util.*" %> <html>
<head>
<title>Auto Refresh Header Example</title>
</head> <body>
<%-- 在这里插入页眉文件 --%>
<%@ include file="Header.jsp"%> <center>
<h2>Auto Refresh Header Example</h2>
<%
// Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 60); // Get current time
Calendar calendar = new GregorianCalendar();
String am_pm; int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND); if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
out.println("Crrent Time: " + CT + "\n");
%>
</center> <%-- 在这里插入页脚文件 --%>
<jsp:include page="Footer.jsp" /> </body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<img src="data:images/header.jpg"  alt="上海鲜花港 - 郁金香" />
<p>我是页眉</p>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<img src="data:images/header.jpg"  alt="上海鲜花港 - 郁金香" />
<p>我是页眉</p>

2、源码分析

web项目结构

把这些文件打包成war,部署到本地tomcat的webapps下,启动tomcat。

在第一次访问index.jsp之后,将生成如下文件

我们观察到,并没生成Header_jsp,原因可想而知,因为使用了include指令而不是include动作。

接着查看index_jsp源码:

try {
response.setContentType("text/html;charset=UTF-8");
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("\n");
out.write("\n");
out.write("\n");
out.write("<html>\n");
out.write("<head>\n");
out.write(" <title>Auto Refresh Header Example</title>\n");
out.write("</head>\n");
out.write("\n");
out.write("<body>\n");
out.write('\n');
out.write("\r\n");
out.write("\r\n");
out.write("<img src=\"images/header.jpg\" alt=\"上海鲜花港 - 郁金香\" />\r\n");
out.write("<p>我是页眉</p>\r\n");
out.write("\r\n");
out.write("\n");
out.write("\n");
out.write("<center>\n");
out.write(" <h2>Auto Refresh Header Example</h2>\n");
out.write(" "); // Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 60); // Get current time
Calendar calendar = new GregorianCalendar();
String am_pm; int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND); if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
out.println("Crrent Time: " + CT + "\n"); out.write("\n");
out.write("</center>\n");
out.write("\n");
out.write('\n');
org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "Footer.jsp", out, false);
out.write("\n");
out.write("\n");
out.write("</body>\n");
out.write("</html>");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}

加粗的分别是include指令和include动作的转换代码。

param动作

修改上面的index.jsp代码:

<%-- 在这里插入页脚文件 --%>
<jsp:include page="Footer.jsp">
<jsp:param name="subTitle" value="we take the string out of SOAP." />
</jsp:include>

修改Footer.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<a href="index.jsp">home page 我是页脚!</a>
<h3>${param.subTitle}</h3>

效果截图

foward动作

乱码问题没解决,只能输入中文。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<p>欢迎来到我的页面!</p>
<% if (request.getParameter("userName")== null) { %>
<jsp:forward page="HandleIt.jsp" />
<% } %>

<p>你好!${param.userName}</p>
</body>
</html>

HandleIt.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<p>很抱歉,你需要重新登陆。</p>
<form action="Hello.jsp">
<p>用户名:<input type="text" name="userName"></p>
<p><input type="submit"></p>
</form>
</body>
</html>

【Head First Servlets and JSP】笔记24:include指令与include动作 & param动作 & foward动作的更多相关文章

  1. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  2. 分别应用include指令和include动作标识在一个jsp页面中包含一个文件。

    分别应用include指令和include动作标识在一个jsp页面中包含一个文件. hello.jsp <%@ page language="java" import=&qu ...

  3. include 指令和 include 动作引入 jsp 页面时中文乱码

    include指令:<%@ include file="new.jsp" %> include动作:<jsp:include page="new.jsp ...

  4. JSP -- include指令与include动作的区别

    JSP -- include指令与include动作的区别 (1)格式的区别: include指令:<%@include file = "文件名"%> include动 ...

  5. 牛客网Java刷题知识点之什么是JSP的3大常用指令、JSP的6大哪些动作、JSP中include指令和include动作有什么区别

    不多说,直接上干货! JSP的3大常用指令 包含指令(Include directive):用来包含文件和合并文件内容到当前的页面. 页面指令(Page directive):用来定义JSP页面中特定 ...

  6. include指令和include动作有什么区别?

    include指令         称为文件加载指令,可以将其他的文件插入jsp网页,被插入的文件必须保证插入后形成的新文件符合jsp页面的语法规则. include指令语法格式:<%@incl ...

  7. 2017.9.18 include指令和include动作有什么区别?

    问题:include指令和include动作有什么区别? 答:include指令合并静态文档或Jsp页面中的内容,可以用于包括动态生成的输出结果,因此可以包含一个Servlet include指令在编 ...

  8. 转: JSP中include指令和include动作的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改,否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如果 ...

  9. JSP中include指令和include动作区别

    首先 <%@ include file=” ”%>:为指令元素 <jsp:include page=” ” flush=”true”/>:为 动作元素 先说指令元素: incl ...

随机推荐

  1. Linux文件的打包与压缩

    打包命令: tar tar 的选项与参数非常的多!我们只讲几个常用的选项,更多选项您可以自行 man tar 查询罗! [root@www ~]# tar [-j|-z] [cv] [-f 创建的档名 ...

  2. php 对字符串的分块处理

    //如何把以下的字符串分成每一个小块, 如:2017.2.14\n我们都是中国人\n小日本 这样的为一个小块 $str = '2017.2.14 我们都是中国人 小日本 2017.2.15 订单取消 ...

  3. sublime text 3 并列显示

    alt+shift+1:显示一列 alt+shift+2:显示二列 alt+shift+3:显示三列 ......

  4. 系统内部集成测试(System Integration Testing) SIT 用户验收测试(User Acceptance Testing)

    系统内部集成测试(System Integration Testing) SIT 用户验收测试(User Acceptance Testing) UAT SIT在前,UAT在后,UAT测完才可以上线

  5. linux禁用触摸板驱动

    Method 1: 终端输入如下命令: sudo modprobe -r psmouse 如果打开触摸板就是: sudo modprobe psmouse ------- Method 2: 第一步: ...

  6. ZOJ 3332 Strange Country II

    Strange Country II Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge You want to v ...

  7. python中,有关正则表达式re函数:compile、match、search、findall

    1.全局匹配函数 re.compile(pattern=pattern,re.S).findall(text)函数: compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象.该对 ...

  8. Audit logon events&Logon type

    表一.Logon type 表二.Audit logon events 表三.Logon type details Logon type Logon title Description 2 Inter ...

  9. Python图像处理库Pillow入门

    http://python.jobbole.com/84956/ Pillow是Python里的图像处理库(PIL:Python Image Library),提供了了广泛的文件格式支持,强大的图像处 ...

  10. DOM 编程入门

    DOM (Document Object Model) 文档对象模型 文档: 标记型文档(具有标签, 属性以及标签中封装的数据) 对象: 封装了属性和行为的实例, 可以被直接调用 模型: 所有标记型文 ...