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. python进阶九_网络编程

    Python网络编程一 一.一些基本概念 在Python网络编程这一节中会涉及到非常多网络相关的术语.对于一些最主要的概念,如TCP/IP,Socket等等不再赘述,不明确的能够自己去查一查,对于一些 ...

  2. linux more less cat

    在使用和维护Linux系统时,常常需要查看文件的相关内容,那么如何才能做到呢?下面小编就以CentOS6.4系统为例演示查看文件内容的几种常用的方法. 工具/原料   CentOS6.4 查看文件内容 ...

  3. C++三大函数(The Big Three)

    本文用Markdown编辑,这里对Markdown的支持不完善,更好的阅读体验请移步:我的Markdown文本 C++三大函数: 析构函数 复制构造函数 operator= 析构函数 函数模样:~S( ...

  4. HTTP请求的过程&HTTP/1.0和HTTP/1.1的区别&HTTP怎么处理长连接

    http://www.cnblogs.com/GumpYan/p/5821193.html

  5. python3----生成器generator(yield)

    # 列表推导式a = [i for i in range(100) if not(i % 2) and (i % 3)]print(a)# 字典推导式b = {i: i % 2 == 0 for i ...

  6. Dropdownlist中用viewmodel传值处理方法

    背景:MVC框架,页面使用razor语法,下拉框的话使用了@Html.DropDownList(),以前传值使用viewdata,但是我们老大说这个方式比较low,希望我可以使用viewmodel的方 ...

  7. location.assign 与 location.replace的区别

    window.location.assign(url) : 加载 URL 指定的新的 HTML 文档. 就相当于一个链接,跳转到指定的url,当前页面会转为新页面内容,可以点击后退返回上一个页面. w ...

  8. [LintCode] 通配符查询

    动态规划: class Solution { public: /** * @param s: A string * @param p: A string includes "?" ...

  9. 前端代码tomcat下简单部署

    软件 filezilla [ftp]  +  visionapp Remote Desktop[远程桌面] (前提:前后端代码分离,如前端angular实现) ftp上传到机器{软件 filezill ...

  10. ASP.NET Identity 2集成到MVC5项目--笔记02

    ASP.NET Identity 2集成到MVC5项目--笔记01 ASP.NET Identity 2集成到MVC5项目--笔记02 继上一篇,本篇主要是实现邮件.用户名登陆和登陆前邮件认证. 1. ...