public class LoginAction extends ActionSupport {
private String username;
private String password;
@Override
public String execute() throws Exception {
ActionContext ctx = ActionContext.getContext();
Integer counter = (Integer)ctx.getApplication().get("counter");
// put counter into application
ctx.getApplication().put("counter", counter);
// put username into session
ctx.getSession().put("user", username);
if (getUsername().equals("crazyit.org")
&& getPassword().equals("leegang")) {
ctx.put("tip", "Login Success! ");
return SUCCESS;
}
else {
ctx.put("tip", "Login Falied!");
return ERROR;
}
}
}
I put "counter"
in application "user"
in session and "tip"
in ActionContext
. In JSP I can use ${session.user}
and ${sessionScope.user}
to reference the "user" property. ${request.tip}
and ${requestScope.tip}
to reference tip
.
My questions:
- Are session, request, application the same as
sessionScope
,requestScope
,applicationScope
in EL? - What's the relationship between
ActionContext
andrequest(requestScope)
?
P.S.:
I test ${request == requestScope}
which is true, this means they are the same?
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.
So session and sessionScope are same but differs in context they are used.More specifically
session is object
andsessionScope 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.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 ofrequest
,session
, andapplicaton
. And where you using it in the application these objects are referenced. To access the servlet stuff use theServletActionContext
that helps to retrieve the appropriate resources. Struts2 also wraps theServletRequest
to provide access to the action properties andvalueStack
from the EL expressions.sessionScope
,requestScope
, andapplicationScope
used with EL expressions to evaluate to the servlet stuff attributes. That are the differences.HttpSession
,HttpServletRequest
andServletContext
objects whilesessionScope, requestScope and applicationScope
provide access to all the session, request and application scoped attributes.You can say that applicationScope > sessionScope > requestScope.
Have a look over below given code I tried.
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}
givesfalse
becauserequestScope
is client's request object whenrequest
will causeEL
to search for object maped withrequest
name in variousscopes
. But in your case it istrue
because ofstruts2
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:
You can also reference it implicitly by omitting the scope:
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.