aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getSession(false) at first request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "getSession(false) at first request" Watch "getSession(false) at first request" New topic
Author

getSession(false) at first request

Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209
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
Pabak Nanda
Greenhorn

Joined: Nov 04, 2004
Posts: 24
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
Gabriel Forro
Ranch Hand

Joined: Apr 16, 2004
Posts: 59
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).


Gabriel
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
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.


SCJP,SCWCD(1.4)
saager mhatre
Ranch Hand

Joined: Dec 20, 2000
Posts: 61
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.
Nitish Bahadur
Ranch Hand

Joined: Aug 25, 2003
Posts: 118
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.
 
Consider Paul's rocket mass heater.
 
subject: getSession(false) at first request