• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

getSession(false) at first request

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.,

this qn is from enthuwhere sample mock i am little bit confused with the answer given them what will be the answer for this qn?

Consider the following JSP code (See exhibit).

What will it print for the very first request to this page as well as the web application that contains this page?



Code



Options

Select 1 correct option.

1) It will print Hello!


2) It will print Hello and will set the count attribute in the session.


3) It will throw a NullPointerException at request time.


4) It will not compile.

the answer given is option 2
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the second getSession method should take "tru" as the argument. Otherwise this will throw NullPointerException.

The code could be like


<html><body>
<% Integer count = (Integer) request.getSession(false).getAttribute("count");
if(count != null )
{
out.println(count);
}
else
request.getSession(true).setAttribute("count", new Integer(1));%>
Hello!
</body></html>



Thanks,
Pabak
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

the option 2 is correct.
The explanation is as follows:

1. It is a JSP file

2. If there is no <%@ page session="false" %> explicitly, then session is set to true by default. It means, that the session object is created before the very first line of the JSP page (in the jsp's servlet class definition).
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question befuddles me too...I tried it out in tomcat and it printed Hello the first time. So, I then printed the session attribute count. So, it prints Hello 1 ! everytime. Does it mean that
1. there already is a session existing since request.getSession(false) doesn't create a session if there isn't one already ?
2. ok, assuming there is a session already, the value of getAttribute would be null becuase otherwise, it would come to the out.println statement. But its going to the setting the attribute else.
But aren't we typecasting a null value to an Integer. How is that not throwing an exception ?

Hopefully, I have made my doubts clear...Could someone help us out ? Thanks.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assertion (1) is right; since the 'session' attribute of the 'page' directive defaults to 'true', the implicit variable session was initialized with a new ly created session.

WRT the NullPointerException, if you're talking about the following statement...

... that's an assignment, not a downcast.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saager, It is very much a cast.(Yes there is assignment too) I think Ritu meant ClassCastException.

Ritu, casts are allowed from null to any class, interface, or array type. Basically, the verifier checks if myInt can be assigned 'null'. Sure, all objects can. Hence there is no ClassCastException.
 
reply
    Bookmark Topic Watch Topic
  • New Topic