菜鸟学Android笔记(四十二):Include指令及九大隐式对象

一、Include指令

JSP有三大指令,前一节已经介绍了Page指令,这一节来介绍Include指令

include指令用于JSP文件中插入一段包含文本或代码的文件,可以是TXT、HTML、JSP页面

案例: Body.jsp导入Header.html和Foot.jsp

Body.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
    <title>My JSP 'Body.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <%@ include file="Header.html" %>
   <p>这是主体</p>
  <%@ include file="Foot.jsp"  %>
  </body>
</html>

Header.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  <head>
    <title>Header.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    这是头部<br>
  </body>
</html>
Foot.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    
    <title>My JSP 'Foot.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    这是底部 <br>
  </body>
</html>
浏览器会输出:

这是头部

这是主体

这是底部

上面有乱码,但不是今天的重点,就先不解决它,但查看它的源码会发现它有点乱:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
    <title>My JSP 'Body.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Header.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    这是头部<br>
  </body>
</html>

   <p>这是主体</p>
  


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    
    <title>My JSP 'Foot.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    这是底部 <br>
  </body>
</html>

  </body>
</html>
这是为什么?

 include指令用于引入其它JSP页面,如果使用include指令引入了其它JSP页面,那么JSP引擎将把这两个JSP翻译成一个servlet

我们如何解决?

可以适当的去掉里面的jsp或html格式

二、九大隐式对象

在这里重点介绍out和pageContext

1、out

相当于是response.getWriter得到PrintWriter
out和response.getWriter获取到的流不同在于,在于这个out对象本身就具有一个缓冲区.利用out写出的内容,会先缓冲在out缓冲区中,
 直到out缓冲区满了或者整个页面结束时out缓冲区中的内容才会被写出到response缓冲区中,最终可以带到浏览器页面进行展示

如何证明这个?我们可以建一个outTest.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'outTest.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%out.write("我是第一"); %>
    <%response.getWriter().write("我是第二"); %>
    <%out.write("我是第三"); %>
  </body>
</html>

当浏览器输出的时变成这样了
我是第二 我是第一 我是第三

如何查看它的源代码,我们可以看出:

我是第二


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="http://localhost:80/Day06/">
    
    <title>My JSP 'outTest.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    我是第一
    
    我是第三
  </body>
</html>
“我是第二”竟然在全部代码的上面了

查看生产的Servlet文件,我们更直观的了解

out.write('\r');
      out.write('\n');

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

      out.write("\r\n");
      out.write("\r\n");
      out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
      out.write("<html>\r\n");
      out.write("  <head>\r\n");
      out.write("    <base href=\"");
      out.print(basePath);
      out.write("\">\r\n");
      out.write("    \r\n");
      out.write("    <title>My JSP 'outTest.jsp' starting page</title>\r\n");
      out.write("    \r\n");
      out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
      out.write("\t<meta http-equiv=\"expires\" content=\"0\">    \r\n");
      out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
      out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
      out.write("\t<!--\r\n");
      out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
      out.write("\t-->\r\n");
      out.write("\r\n");
      out.write("  </head>\r\n");
      out.write("  \r\n");
      out.write("  <body>\r\n");
      out.write("    ");
out.write("我是第一"); 
      out.write("\r\n");
      out.write("    ");
response.getWriter().write("我是第二"); 
      out.write("\r\n");
      out.write("    ");
out.write("我是第三"); 
      out.write("\r\n");
      out.write("  </body>\r\n");
      out.write("</html>\r\n");
out并不是一下子就可以输出的,而是先输到缓冲区,到最后的时候再一起输出,这就解释了为什么
response.getWriter().write("我是第二"); 
这段代码先输出的原因

我们在JSP的Page指令中有buffer属性,之前没有介绍,现在就来介绍

[buffer="none | 8kb | sizekb" ]可以用来禁用out缓冲区或设置out缓冲区的大小,默认8kb
 [ autoFlush="true | false"]用来设置当out缓冲区满了以后如果在写入数据时out如何处理,如果是true,则先将满了的数据写到response中后再
 接受新数据,如果是false,则满了再写入数据直接抛异常
 在jsp页面中需要进行数据输出时,不要自己获取response.getWriter,而是要使用out进行输出,防止即用out又用response.getWriter而导致输出顺序错乱的问题

看案例,如果我们设置为none会发生什么状况?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" buffer="none"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'outTest.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%out.write("我是第一"); %>
    <%response.getWriter().write("我是第二"); %>
    <%out.write("我是第三");  %>
   
  </body>
</html>
那么输出的就是:

我是第一 我是第二 我是第三

而源代码也变成了:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="http://localhost:80/Day06/">
    
    <title>My JSP 'outTest.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    我是第一
    我是第二
    我是第三
   
  </body>
</html>
out没有缓冲的功能,所以有就直接输出了

2、pageContext对象

pageContext不但可以设置page范围的属性,也可以用来设置其他范围的属性,下面一一解析它的作用

1、可以作为入口对象获取其他八大隐式对象的引用

主要的方法如下:

 getException方法返回exception隐式对象
 getPage方法返回page隐式对象
 getRequest方法返回request隐式对象
getResponse方法返回response隐式对象
 getServletConfig方法返回config隐式对象
getServletContext方法返回application隐式对象
getSession方法返回session隐式对象
getOut方法返回out隐式对象

2、域对象,四大作用域的入口,可以操作四大作用域中的域属性

下面看一个案例:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'pageContext.jsp' starting page</title>
    
  </head>
 
  <body>
   <% pageContext.setAttribute("小红","1");%>
   <% session.setAttribute("小红","2");%>
   <% request.setAttribute("小红","3");%>
   <% application.setAttribute("小红","4");%>
   <%=pageContext.getAttribute("小红",pageContext.SESSION_SCOPE) %>
   <%=pageContext.getAttribute("小红",pageContext.PAGE_SCOPE) %>
   <%=pageContext.getAttribute("小红",pageContext.REQUEST_SCOPE) %>
   <%=pageContext.getAttribute("小红",pageContext.APPLICATION_SCOPE) %>
  </body>
</html>
通过  public void getAttribute(java.lang.String name, java.lang.Object value,int scope)

就能分别获得不同范围的域属性

3、提供了请求转发和请求包含的快捷方法


具体格式如下:

 pageContext.include("/index.jsp");
 pageContext.forward("/index.jsp");   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值