Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing variables from EL

 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Here are my doubts regarding the EL and scopes.

1.
- setting the following ?username=marcus to the URL
- displaying result on the jsp page:

${reuestScope.username} - it will not get "marcus" but why? In my understanding the username param is in the request scope for this page request.

${param.username} - works fine since it deals with parameters

2.
One more doubt. Consider the following scriptlet code:

<%
String testValue = "value";
request.setAttribute("str",testValue);
%>

and then try to access str variable via EL:

${str} will print "value". Why does it work? Should it be accessed via requstScope like {$requestScope.str} since the str has been assigned to the request object?

Thanks for help.

Regards,
Krzysztof
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${reuestScope.username} - it will not get "marcus" but why? In my understanding the username param is in the request scope for this page request.
No - request scoped attributes are Objects stored in a Map (with String keys) on the server-side by your code. Query parameters are those (String-only) variables submitted either in the URL or POST request body. Hence ${param.username} accesses the query string parameter called "username" so you get the output you expect.
${str} will print "value". Why does it work?
Because if you don't explicitly specify a scope for a variable, the scopes are searched in the order page, request, session, application. The value is determined by the first scope to contain an attribute of the given variable name - in your case you don't have an attribute called "str" in the page scope, so it retrieves the one in the request scope.

Hope that helps.
 
Krzysztof Koziol
Ranch Hand
Posts: 133
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles,

Thanks for the great explanation. As for the 2nd question the expression ${str} works similar to PageContext.findAttribute("str")?

Rgards,
Krzysztof
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${str} works similar to PageContext.findAttribute("str")?
That's exactly how it works!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic