• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

EL plus Scope confusion

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
permaculture is largely about replacing oil with people. And one tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic