Are session and sessionScope the same in JSP EL?

扶醉桌前 提交于 2019-11-27 12:27:35
Russell Shingleton

With expression language (EL), the scope items are value maps of attributes in the objects that they refer to. For instance, the requestScope is a map representation of values in the request object. This is explained in pretty clear detail on this page: Java Servlet and JSP. If you read through the EL sections, you'll notice a point about request vs request scope here: The requestScope is NOT request object.

I would recommend reading through this page to get a better understanding of servlet/jsp in general.

As far as how the ActionContext relates to these items, it is really a wrapper used by struts to encapsulate the servlet. You can read more specifics about it here: Accessing application, session, request objects.

There have been some references to implicit values given here, but I feel like just saying it's implicit doesn't really explain much. When you are using EL to access servlet variables, you can explicitly declare which scope you want to reference, such as:

 ${requestScope.myVariable}

You can also reference it implicitly by omitting the scope:

 ${myVariable}

Now, the problem that can arise here is that variables with the same name can cause collision. EL will check implicit values in a specific order: pageScope, requestScope, sessionScope, and applicationScope, param, paramValues, header, headervalues, initParam, cookie, pageContext. What this means is that if you have a variable in the request scope with the same name as a variable in session or application scope for instance, the request scoped value will be referenced.

By default page, request, session and application objects are available to JSP pages. So you can access then using EL syntax.

And following table shows IMPLICIT objects available to EL.

       Implicit object            Description
1.     pageScope        Scoped variables from page scope
2.     requestScope     Scoped variables from request scope
3.     sessionScope     Scoped variables from session scope
4.     applicationScope Scoped variables from application scope
5.     param            Request parameters as strings
6.     paramValues      Request parameters as collections of strings
7.     header           HTTP request headers as strings
8.     headerValues     HTTP request headers as collections of strings
9.     initParam        Context-initialization parameters
10.    cookie           Cookie values
11.    pageContext      The JSP PageContext object for the current page

So session and sessionScope are same but differs in context they are used.More specifically session is object and sessionScope is map (key, value) of Attribute and its value.

  • If you say ${session.sessionAttr} it refers to session object available to JSP page.

  • If you say ${sessionScope.sessionAttr} it refers to IMPLICIT session object available to EL.

  • Or if you just say {attrName} it will search attrName in all scope from page to application scope.
  1. session, request, application are the actually HttpSession,HttpServletRequest and ServletContext objects while sessionScope, requestScope and applicationScope provide access to all the session, request and application scoped attributes.

You can say that applicationScope > sessionScope > requestScope.

  • applicationScope attributes will be accessible to all the sessions, all the requests across the web applications. These attributes stay alive as long as application is alive
  • sessionScope attributes will be accessible to all the requests across the current HttpSession. These attributes stay alive as long as session is alive
  • requestScope attributes will be accessible from the current request only. Once the response is completed, they are gone.

The ActionContext is a Struts2 thing, and it's created in every request that is handled by the framework. When it's created the framework populates it along with the servlet stuff with it's own implementations of request, session, and applicaton. And where you using it in the application these objects are referenced. To access the servlet stuff use the ServletActionContext that helps to retrieve the appropriate resources. Struts2 also wraps the ServletRequest to provide access to the action properties and valueStack from the EL expressions. sessionScope, requestScope, and applicationScope used with EL expressions to evaluate to the servlet stuff attributes. That are the differences.

Have a look over below given code I tried.

<body>
<%
FirstServlet first=new FirstServlet();  // it has a fileName property and getter setter for it
%>
<%
session.setMaxInactiveInterval(10); //jsp's implicit session object
request.setAttribute("session", first); //jsp's implicit request object
session.setAttribute("hello", "this worked!");
pageContext.getSession().setAttribute("hello", "now this also worked!");%> // same as above

${pageContext.session.maxInactiveInterval } // client's session object
${sessionScope.maxInactiveInterval}
${session.fileName } // FirstServlet Object and gives the fileName I gave.
${sessionScope.hello } // OP: "now this also worked!
${session==sessionScope } // gives false. If using struts2 than true
${request==requestScope } // gives false. If using struts2 than true
</body>

in EL as stated by Prasad and Captain when you use ${sessionScope} it only maps session-scoped variable names to their values.

if you want to get client's session object than you should use pageContext.session

but when you use ${session}, el searches for attribute maped with session name in order: page->request->session->application scopes starting from left to right.

${request == requestScope} gives false because requestScope is client's request object when request will cause EL to search for object maped with request name in various scopes . But in your case it is true because of struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!