javaweb_jsp@include/jsp:include/implicit objects

发布时间 2023-06-14 07:05:32作者: ming1010

jsp @include Directive

jsp:include Action

 1 <html>
 2 <head>
 3     <title>Title</title>
 4 </head>
 5 <body>
 6 <%--两个页面合二为一,包含在同一块代码里--%>
 7 <%@include file="common/header.jsp"%>
 8 <h1>the body of page, jsp directives</h1>
 9 <%@include file="common/footer.jsp"%>
10 <%--页面拼接,本质上是3个页面--%>
11 <jsp:include page="/common/header.jsp"></jsp:include>
12 <h1>the body of page, jsp tag</h1>
13 <jsp:include page="/common/footer.jsp"></jsp:include>
14 
15 </body>
16 </html>

implicit objects_内置对象,主要掌握作用域(scope)

 1 <%@ page isELIgnored="false" %>
 2 <%--否则不能读取EL表达式--%>
 3 <html>
 4 <head>
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 <%
 9     pageContext.setAttribute("name1", "Ming1");
10     request.setAttribute("name2", "Ming2");
11     session.setAttribute("name3", "Ming3");
12     application.setAttribute("name4", "Ming4");
13 %>
14 <%
15     String name1 = (String) pageContext.findAttribute("name1");
16     String name2 = (String) pageContext.findAttribute("name2");
17     String name3 = (String) pageContext.findAttribute("name3");
18     String name4 = (String) pageContext.findAttribute("name4");
19     String name5 = (String) pageContext.findAttribute("name5");//not exist
20     pageContext.forward("pageContextDemo02.jsp");//pageContextDemo02.jsp can access name2.
21 
22 %>
23 <h1> get values of names</h1>
24 <h3>${name1}</h3>
25 <h3>${name2}</h3>
26 <h3>${name3}</h3>
27 <h3>${name4}</h3>
28 <h3>${name5}</h3><%--直接过滤,不显示--%>
29 <h3><%= name5%></h3><%--null--%>
30 </body>
31 </html>

 

 

note:

访问本页面课获取全部name值

从另一个新的页面获取name的值时,只能获取name3 和name4

request若被forward,则也可以获取,即 pageContext.forward("pageContextDemo02.jsp")