This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes EL plus Scope confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "EL plus Scope confusion" Watch "EL plus Scope confusion" New topic
Author

EL plus Scope confusion

Pakna Zed
Greenhorn

Joined: Mar 02, 2009
Posts: 4
Hello everyone, I am preparing for exam and would ask you some help in clarifying a few issues. But, before anything, some code is inbound first - it's a really simple login example I've constructed.
Let's assume correct username and pass combination is entered; questions are at the bottom.

LoginPage.jsp (entrypoint - view)


LoginServlet.java (controller)


UserDAO.java ("service" class)


UserBean.java (model, a classic POJO/bean used as a DTO)


userLogged.jsp (exitpoint - view) --never mind the div elements, they're there to center the view, but I won't spam you needlessly with CSS, it's irrelevant anyhow--


Webpage output is as follows (c/p directly from FireFox):
Welcome, USER_X
********
Test 0 ->
Test 1 ->
Test 2 ->
Test 3 ->
Test 4 ->
Test 5 ->
Test 6 -> USER_X
*******
Test 7 -> localhost:8080
Test 8 -> localhost:8080
Test 9 -> GET


1) My first question is regarding the scope - which scope is actually applicable?? If you checkout userLogged.jsp, lines 13 and 22, you'll see my dilemma - if I use any other scope than "application" in L13, line 14 returns null value. On the other hand, should I use applicationScope on L22, it returns null (as it damn well should, since I am setting a SESSION attribute, not a context attribute!).
So, the question is - why should I use application scope on L13 anyway?? I'd expect nothing other than session scope, as can be seen from my controller.

2) The other question is regarding EL - why can't I fetch request parameters in Tests 0-5? Other stuff works fine (as can be seen from output), but I can't understand how to make these request parameters printed out as I inteded (via request EL implicit objects).

Thank you very much in advance
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
The below are my findings:
To answer the second part
2) response.sendRedirect("userLogged.jsp");
and
${param.name} in userLogged.jsp

Here the response is already comitted and you are trying to fetch the parameters(request).

Description:
request,response objects are thread safe and synchronous in nature(Apache tomcat6.0).Once response is
given to the client we cann't retrive.

(go through sendRedirect/forward)

EL:Elmination to java syntaxs in JSP

It checks the syntax vaildity not the existence of variable.
-->Translation (which involves compilation):
The abstract class javax.servlet.jsp.el.ExpressionEvaluator has a method evaluate(java.lang.String, java.lang.Class,
javax.servlet.jsp.el.VariableResolver, javax.servlet.jsp.el.FunctionMapper) checks for syntax error.
--->RunTime:
At runtime the String representing the expression is sent to a method called resolveVariable()
which uses javax.servlet.jsp.JspContext 's public abstract java.lang.Object findAttribute(java.lang.String);

the returned object is sent to the outputstream via an out.print();
Note:Even if the variable doesn't exists sensible defaults are provided.

Adding on this finAttribute checks through the scopes in the following order.
page,request,session,application.


To answer the first part about scopes:
1)session.setAttribute("currentSessionUser", user);
and
<jsp:useBean id="currentSessionUser" class="examplePackage.UserBean" scope="application">

${param["username"]}-->Map of ServletRequest paramter.(You have already comitted response check forward/sendredirect you will get solution.)

${sessionScope.currentSessionUser.username}-->you are targeting the session scope,Which you have already set so is the result.


Conclusion:
-->sendRedirect will evades up the request objects.

Please let me know if I am missing something.




 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EL plus Scope confusion
 
Similar Threads
JSP <a href> tags breaks page functionality
My login application accepts any and all username/passwords.
Always responding invalidLogin.jsp
Form processing using register.java but it doesn't show my hashmap errormessages
Login servlet not redirecting to JSP