【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 ...
随机推荐
- 用MathType编辑上下尖括号有什么技巧
在MathType中,同一个数学符号可以进行各种变换方向的使用,就比如箭头符号,任意方向都可以使用,这也是很常见的.数学中的符号能够根据各种特殊需要进行灵活使用,除了箭头符号之外,其它符号也可以,比如 ...
- MAMP下配置虚拟主机域名
第一步:修改虚拟主机地址: /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf 第二步:
- OpenSSL Heart Bleed 如何修复
一 . 前言 这两天这个事件沸沸扬扬啊,有了这个bug黑客在电脑前动动手指就能获取各大电商网站.各大银行用户的用户名和密码了,屌爆了 BUG具体内容 : http://heartbleed.com ...
- ios消息推送机制原理与实现
本文转载至 http://hi.baidu.com/yang_qi168/item/480304c542fd246489ad9e91 Push的原理: Push 的工作机制可以简单的概括为下图 图中, ...
- homebrew常用指令
其它Homebrew指令: brew list —列出已安装的软件 brew update —更新Homebrew brew home *—用浏览器打开 brew info *—显示软件 ...
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- Go & SQLite on Windows
一般golang使用的sqlite驱动包都是github.com/mattn/go-sqlite3,但是官方并没有直接支持windows平台的编译,因为windows平台编译默认需要gcc支持 其实解 ...
- HTML Colors
https://www.w3schools.com/html/html_colors.asp <!DOCTYPE html> <html> <body> #169f ...
- 利用Django中间件middleware解决用户未登录问题(转)
add by zhj: Django的中间件一般用于处理通用性的问题,分为五种,按处理顺序为request_middleware,view_middleware,exception_middlewar ...
- django--之登录表单提交
前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...