• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt is session

 
thilakk kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will it print for the very first request to this page as well as the web application that contains this page?


<html><body>

<%

Integer count = (Integer) request.getSession(false).getAttribute("count");

if(count != null )

{

out.println(count);

}

else request.getSession(false).setAttribute("count", new Integer(1));

%>

Hello!

</body></html>

Select 1 correct option.
A.It will print Hello!
B.It will print Hello and will set the count attribute in the session.
C.It will throw a NullPointerException at request time.
D.It will not compile.


Answer is B. But i8 thought as the request.getSession(false) will always return the existing session this will raise a null pointer exception.can anyone clear my doubts
 
Amit Prakash Singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
reason why this will not throw exception is, a implicit session object is available to the jsp by default. So second time when your code calls for the getSession(false), there will be a session available.

look at the container generated java file for your jsp, you will find conatiner has already called the getSession.

tricky question!!! if you set the session to be not available, by setting session="fales" in the page directive then it will throw error.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Amit

I am not able to understand your explanation. I tried printing
request.getSession(false).getAttribute("count") in the console

it gave me the value 1. But there is no attribute in the session named "count". So it should return null right. And in the statement

Integer count=(Integer)request.getSession(false).getAttribute("count");

does it mean type cast the value returned by getSession(false) to Integer then you call the getAttribute() on that object.

Correct me if I am wrong

Thanks
Srividhya
 
Amit Prakash Singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!!!

My explanation is simple : what all implicit objects are available to a jsp page?

out, request, response, session, config, application, pagecontext, page and exception

right???

now see, session is already made available to you by default by the conatiner.

when you called the request.getSession(false) : it gives you the session as it is available.

if you want to turn off implicit session, use this:

<%@ page import="" session="false" %>

let me know if there is any confusion
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit, is absolutely correct.

By default, every JSP is given an implicit HttpSession object. This is in stark contrast with the servlet model in which the developer must actively create the session object. This is one of the things I don't like about the JSP specification.

If you do not want a JSP to include a session object, then you must include the following page directive:


Now, if you add this directive to Thilakk's original JSP code, then a NullPointerException will be thrown because the request.getSession(false) result will be null and calling the getAttribute method on a null object throws an NPE.

Make sense?

Cheers,
Bryan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic