aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Accessing variables from EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Accessing variables from EL" Watch "Accessing variables from EL" New topic
Author

Accessing variables from EL

Krzysztof Koziol
Ranch Hand

Joined: Nov 19, 2006
Posts: 133

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


SCJP 5.0, SCWCD 5.0, SCBCD 5.0, SCEA/OCMJEA 5.0
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
${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.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Krzysztof Koziol
Ranch Hand

Joined: Nov 19, 2006
Posts: 133

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

Joined: Mar 27, 2003
Posts: 836
${str} works similar to PageContext.findAttribute("str")?
That's exactly how it works!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing variables from EL