JSP中的四大域

JSP中四大域

什么是域对象呢?

> jsp默认支持四个域对象:
HttpServletContext  application;   //整个项目
HttpSession         session;       //一次会话
HttpServletRequest  requst ;       //一次请求
HttpPageContext     pageContext;   //当前页面

核心方法

setAttribute(键,值);
getAttribute(键);
removeAttribute(键);

开启EL表达式

isELIgnored="false" 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5BwN4FAa-1600139413977)(C:\Users\Carlos\AppData\Roaming\Typora\typora-user-images\image-20200915100318632.png)]

然后在四大域中存数据:

<! --添加数据到四个作用域对象中-->
<%
  application.setAttribute("name1", "baoqiang1");
  session.setAttribute("name1", "baoqiang2");
  request.setAttribute("name1", "baoqiang3");
  pageContext.setAttribute("name1", "baoqiang4");
%>

取数据的几种方法:

方式1:从四大域中取数据--%>
<%--
<%=application.getAttribute( "name1")%>
<%=session.getAttribute( "name2")%>
<%=request.getAttribute("name3")%>
<%=pageContext.getAttribute( "name4")%>
<hr/>

<%--方式2:从四大域中取数据(EL表达式)--%>
${applicationScope.name1}
${sessionScope.name2}
${requestScope.name3}
${pageScope.name4}
心%--方式3:从四大域中取数据(EL表达式)--%>
<%--这种方式,如果找不到对应的键,返回空串--%>
<%--这种方式,会从作用范围最小的域开始找,所以每个域的键的名字不能重复--%>
${name1}
${name2}
${name3}
${name4}

运行效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37924905/article/details/108595143