1、EL表达式解析过程JSP中,我们经常会写为${obj.name}字样,但你有没有想过,它的取值过程是什么,属性值从哪取得?${obj}相当于 request.getAttribute("obj"),这句话严格来说不严谨,依次的请求范围是page、reques...
1、EL表达式解析过程
JSP中,我们经常会写为${obj.name}字样,但你有没有想过,它的取值过程是什么,属性值从哪取得?
${obj}相当于 request.getAttribute("obj"),这句话严格来说不严谨,依次的请求范围是page、request、session、application
也就是说,如果在page.getAttribute()找不到,再去request.getAttribute,如果request找不到,再去session里找,session里找不到,再去application里
page.getAttribute-------->request.getAttribute------------>session.getAttribute----------->applicaton.getAttribute
2、page和pagecontext的区别
page就是当前jsp页面,也等同于jsp编译后的servlet,查看java代码可以得知,page是java.lang.Object类型
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
<span style="white-space:pre"> </span>null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
jspx_out = out;<span style="font-family: Arial, Helvetica, sans-serif;"> }</span>
来个具体的例子吧。
page1.jsp,设置键值对
<%
page.setAttribute("name","obma")
%>
在page1.jsp,可以取出上边设置的name值,但是在其它页面(page2,page3中....)获取到的都是null
<%
String value = (String)page.getAttribute("name");
%>
pagecontext,是page的上下文,是javax.servlet.jsp.PageContext类型,它持有request,response,也持有page,通过pagecontext可以获取servletcontext、servletconfig等,可以看出他是一个桥梁可以获取上下文变量
以上这篇(标题)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
本文标题为:浅谈jsp EL表达式取值过程、page和pagecontext的区别
- 深入了解Spring的事务传播机制 2023-06-02
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- JSP页面间传值问题实例简析 2023-08-03
- JSP 制作验证码的实例详解 2023-07-30
- Java中的日期时间处理及格式化处理 2023-04-18
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- Java实现顺序表的操作详解 2023-05-19
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01