JSP隐式对象

2020-03-12  本文已影响0人  开心的小哈

隐式对象的说明(作用域对象)
pageContext
提供获取其他隐式对象的方法(getRequest,getOut等)
作用域设为page时,数据保存在pageContext中(setAttribute)
request
使用同Servlet
作用域设为request时,数据保存在request中
session
使用同Servlet
作用域设为session时,数据保存在session中
application
作用域设为application时,数据保存于application中(整个运用共享)
代码演示:
.xml中设置要跳转到的jsp初始值test

<servlet>
  <servlet-name>YUinshiduixiang</servlet-name>
  <jsp-file>/yinshiduixiang.jsp</jsp-file>
  <init-param>
  <param-name>test</param-name>
  <param-value>123123123</param-value>
  </init-param>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>YUinshiduixiang</servlet-name>
  <url-pattern>/dddd</url-pattern>
  </servlet-mapping>

yinshiduixiang.jsp
其中pageContext只在当前页
而application是全局的

<%@ page %>
<!DOCTYPE html>
<html>
<head>
<title>zuoyongyu</title>
</head>
<body>
<%


String cfg=config.getInitParameter("test");
pageContext.setAttribute("test",cfg);
application.setAttribute("test", cfg);
%>

</body>
</html>

设置好对象的值在index页进行访问

<!DOCTYPE html>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
is like<%@ include file="include/date.jsp" %>
<br>
pageContext:<%=pageContext.getAttribute("test") %>
application:<%=application.getAttribute("test") %>

</body>
</html>

如果先访问index页面是没有值的需要先访问上面的/dddd进行初始化值,再访问index页面默认页面,显示打印结果:

pageContext:null application:123123123
上一篇 下一篇

猜你喜欢

热点阅读