【Head First Servlets and JSP】笔记24:include指令与include动作 & param动作 & foward动作
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动作的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- 分别应用include指令和include动作标识在一个jsp页面中包含一个文件。
分别应用include指令和include动作标识在一个jsp页面中包含一个文件. hello.jsp <%@ page language="java" import=&qu ...
- include 指令和 include 动作引入 jsp 页面时中文乱码
include指令:<%@ include file="new.jsp" %> include动作:<jsp:include page="new.jsp ...
- JSP -- include指令与include动作的区别
JSP -- include指令与include动作的区别 (1)格式的区别: include指令:<%@include file = "文件名"%> include动 ...
- 牛客网Java刷题知识点之什么是JSP的3大常用指令、JSP的6大哪些动作、JSP中include指令和include动作有什么区别
不多说,直接上干货! JSP的3大常用指令 包含指令(Include directive):用来包含文件和合并文件内容到当前的页面. 页面指令(Page directive):用来定义JSP页面中特定 ...
- include指令和include动作有什么区别?
include指令 称为文件加载指令,可以将其他的文件插入jsp网页,被插入的文件必须保证插入后形成的新文件符合jsp页面的语法规则. include指令语法格式:<%@incl ...
- 2017.9.18 include指令和include动作有什么区别?
问题:include指令和include动作有什么区别? 答:include指令合并静态文档或Jsp页面中的内容,可以用于包括动态生成的输出结果,因此可以包含一个Servlet include指令在编 ...
- 转: JSP中include指令和include动作的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改,否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如果 ...
- JSP中include指令和include动作区别
首先 <%@ include file=” ”%>:为指令元素 <jsp:include page=” ” flush=”true”/>:为 动作元素 先说指令元素: incl ...
随机推荐
- Android开发人员必须掌握的10 个开发工具+应该深入学习的10个开源应用项目
一.Android开发人员必须掌握的10 个开发工具 Android SDK 本身包含很多帮助开发人员设计.开发.测试和发布 Android 应用的工具,在本文中,我们将讨论 10 个最常用的工具. ...
- Linux的文件权限(简单易懂)
学习这个章节,必须明白以下三个概念: 1.所有者 2.所属组 3.其他人 明白这三个概念后,接下来就学习文件的属性,那么文件的属性有什么呢?如何查看文件的属性? 在命令行下,执行 ls -l 可以得到 ...
- SharePoint 2013/2010 在一个列表或文档库内移动列表项,文档和目录位置而保持last modify by 等系统字段保持不变
本文讲述SharePoint 2013/2010 在一个列表或文档库内移动列表项.文档和目录位置而保持last modify by 等系统字段保持不变的解决方式. 近期遇到客户一个需求,在一个列表或文 ...
- 下载xftp,xshell进行与linux端的远程操作
在window下下载xftp5和xshell5 xshell主要是对远程的及其进行访问,在远程的情况下进行操作 xftp可以对远程的机器进行文件传输. 我安装这两个是单个的安装的. 进入官网 http ...
- 将json转为 SortedDictionary
#region ConvertJsonToSortedDictionary 将json转为 SortedDictionary /// <summary> // ...
- Leetcode-Convert Sorted List to BST.
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Django 中的自定义分页标签
目录结构: 1.在应用下,migrations的同级目录创建templatetags目录(主要两个文件,包含__init__.py) 2.创建分页标签(pagetag.py) #!/usr/bin/e ...
- vitess元数据跨机房灾备解决方案
测试使用vitess的时候发现vitess元数据的实现有多种方案,etcd, etcd2, zk,zk2, 由于刚开始测试的时候使用的是基于k8s集群+etcd的,以下就分步说明灾备实现方案: 1. ...
- Making training mini-batches
Here is where we'll make our mini-batches for training. Remember that we want our batches to be mult ...
- [刷题]ACM ICPC 2016北京赛站网络赛 D - Pick Your Players
Description You are the manager of a small soccer team. After seeing the shameless behavior of your ...