Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is difference between setting a attribute in session and setting attribute with request?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have a question that If I have set a variable like this


request.setAttribute("attr1","This is Value -1");



and I can also do this like

request.getSession().setAttribute("attr1","This is Value-1");



I can access both varibles in JSP like

${attr1}


so what is difference and what is advantage of one over other?


Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37180
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor,
Good question. The difference is what happens if you try to do the same thing on a later page. The session attribute will still be available. The request attribute will be long gone as it only exists for one request/response cycle.

Why don't we put everything in the session you ask? It takes up memory and produces "clutter".
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeanne

I asked this question in this seneraio
I am using Spring Framework to design a Web Application there is a task controller in my web application which is receiving request from views after it has done its work then it need again to redirect to view.Problem is if I use something like

return ModelAndView("someName");



then "task Controller URL" is still on URL
although I can set objects in that
so what I am using


request.getSession().setAttribute("someAttr","someValue");
res.sendRedirect("fullPathofJSPFile");


and problem with this approach is If I am forwarding 200 requests then all those request are still on the session

So I am thing another appraoch for this
if it is possible
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!