Discussion on the value process of jsp EL expression and the difference between page and pagecontext

  • 2021-12-09 09:41:14
  • OfStack

1. EL expression parsing process

In JSP, we often write the words ${obj. name}, but have you ever wondered what the value process is and where the attribute value is obtained?

${obj} is equivalent to request. getAttribute ("obj"), which is not strictly rigorous, and the request scope in turn is page, request, session, application

That is to say, if you can't find it in page. getAttribute (), go to request. getAttribute, if you can't find it in request, go to session, if you can't find it in session, go to application

page.getAttribute-------- > request.getAttribute------------ > session.getAttribute----------- > applicaton.getAttribute

2. The difference between page and pagecontext

page is the current jsp page, which is also equivalent to servlet compiled by jsp. Looking at java code, we can know that page is java. lang. Object type

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>

Let's give a concrete example.

page1.jsp, set key-value pair


In page1.jsp, you can take out the name value set above, but all the other pages (page2, page3...) get null

String value = (String)page.getAttribute("name");

pagecontext is the context of page and is of javax. servlet. jsp. PageContext type. It holds request, response and page. servletcontext, servletconfig, etc. can be obtained through pagecontext. It can be seen that it is a bridge to obtain context variables

Related articles: