Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request & session scope

 
Vivek Alampally
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read Head first JSP & Servlets. But I am implementing stuff related to servlets & JSP now? I have a question on following snippet of code?

String customerNumber = req.getParameter("customerNumber");
HttpSession session = req.getSession();
if (customerNumber == null)
customerNumber = (String) session.getAttribute("customerNumber");
else
session.setAttribute("customerNumber", customerNumber);
 
Vivek Alampally
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the following statement is needed?
if (customerNumber == null)
customerNumber = (String) session.getAttribute("customerNumber");

We can directly put customerNumber in session scope.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vivek Alampally:
Why is the following statement is needed?
if (customerNumber == null)
customerNumber = (String) session.getAttribute("customerNumber");

We can directly put customerNumber in session scope.


That bit of code is getting customerNumber from the session, not putting it in session scope.
 
Vivek Alampally
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that bit of code is getting a CustomerNumber from session scope. My question was why should we get it from session? why is that if statement is needed? Here we can directly get customerNumber from request scope and
put in session scope, if we want to implement session management. I dont understand the need of the if statement?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably not all requests contain that parameter?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic