• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in request.getSession(boolean) !

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My knowledge was:
i) request.getSession() or request.getSession(true) returns either the session associated with the request or creates a new session
ii) request.getSession(false) returns either the session associated with the request or NULL if no session exists

Until I came across this in one of the mock tests, which confused me



The question was:

What would happen when the JSP is requested for the first time and this is the first request in the application?

I answered that it will throw 'NullPointerException' when trying to perform 'Integer count = (Integer) sessionx.getAttribute("count");', as the session does not exist.

But the correct answer is that it works fine and prints 'Hello!' (also setting the attribute in session).
Also this works as per the answer in Tomcat.

Anybody can provide an explanation for this?
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd agree with you.
Looks like a mistake to me.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

By default the session attribute in the page directive is true.So every jsp page contains session object.


Thanks

Anil Kumar
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's it! As Anil has pointed out, a session gets created for every JSP page that don't have a session attribute set to "false" in the page directive.
 
Ashwin Kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification! I think this is a very tricky point
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Important thing,

Lookout for this directive on top.

<%@ page session="false" %>



by default its true, but if you set it to false there is no session implicit variable in the page, now if you do request.getSession(false) it will return null.
 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I really don't like questions like this.

I think there's a difference between testing candidates and deliberately trying to trick them, and in my mind, this question crosses the line.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think there'd be such a question at the exam
 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd be really disappointed if one did.

When studying for an exam, there is an argument that these type of questions serve some purpose. i.e. the person who got this question wrong will probably always check that the session has been declared in the page directive, and will probably know the default from this point forward. In this instance, this example has served some purpose.

However, I found that when my exam was looming, confidence was a big issue for me. There are even studies that show that when a candidate feels that they have done badly in a question (denting confidence), they are far more likely to perform badly in subsequent questions. So, when I reached the end of a chapter, having read it several times and trying to convince myself that "I am ready for this" and that "I can do this", to then come out with a failing score because the authors put in some trick questions, was very disheartening and seriously damaging.

MG
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic