Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getSession(false)

 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,


Came across this example in one of the mock tests ...





The option given were

1.) Throws NullpointerException
2.) Prints Hello;
3.)None of the above
The Answer is 2.) Prints Hello;


The example said that the page is being accessed for the first time ..

my thought was that request.getSession(false) would be retunning null as the session is not yet created .... is it that since the session attribute of the page directive being default true causes this behaviour.


any explanation to this behaviour ? is it different in JSP and servlet ??
In servlet code i believe request.getSession(false) would have thrown an NullPointerException for the session would not have been created for the first time untill the response is committed.


Thanks,
Reshma
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this thread.
 
Jan Kostelansky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP container provides for each JSP page, implicit variables, you can you in code. One of these variables is session. In order to get session, JSP container calls sesion = request.getSession(), before jsp is started to be executed.

This is fragment of translated jsp page. The attached code is placed before acutal JSP code

try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
.....
 
Reshma Shanbhag
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was a very quick reply ..


Got it now ..


Thanks & Regards,
Reshma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic