首页 > 代码库 > 【Head First Servlets and JSP】笔记24:include指令与include动作 & param动作 & foward动作
【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="images/header.jpg" alt="上海鲜花港 - 郁金香" /><p>我是页眉</p>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><img src="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=http://www.mamicode.com/"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="http://www.mamicode.com/we take the string out of SOAP." /></jsp:include>
修改Footer.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><a href="http://www.mamicode.com/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动作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。