Talking about the Value Selection Process of JSP EL Expressions, the Difference between Page and Page Context

Time:2019-10-10

1. EL expression parsing process

In JSP, we often write the words ${obj. name}, but have you ever thought about the process of its value taking and where to get the attribute value?

The phrase ${obj} corresponds to request. getAttribute (“obj”), which is not strictly rigorous, and the request range is page, request, session, application in turn.

That is to say, if page. getAttribute () can’t be found, then go to request. getAttribute, if request can’t be found, then go to session, session can’t be found, and then go to application.

page.getAttribute——–>request.getAttribute————>session.getAttribute———–>applicaton.getAttribute

2. The difference between page and page context

Page is the current JSP page, which is also equivalent to the JSP compiled servlet. Looking at the Java code, you can see 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 pairs


<%
page.setAttribute("name","obma")
%>

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


<%
String value = (String)page.getAttribute("name");
%>

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

The above article (title) is the whole content shared by the editor. I hope to give you a reference, and I hope you will support developpaer more.

Recommended Today

Hot! Front and rear learning routes of GitHub target 144K

Hello, Sifu’s little friend. I’m silent Wang Er. Last week, while appreciating teacher Ruan Yifeng’s science and technology weekly, I found a powerful learning route, which has been marked with 144K on GitHub. It’s very popular. It covers not only the front-end and back-end learning routes, but also the operation and maintenance learning routes. As […]