Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Defference between request.getAttribute() & request.getParameter()

 
Arun Karthik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Can somebody tell me the difference between
"request.getAttribute()" & "request.getParameter()", apart from .getAttribute() gives us an OBJ & .getParameter() gives a String.

The Problem that I am facing is as follows:
I have a servlet, at the end of the servlet I am saying:

request.setAttribute("someName", "SomeStringToSendToScreen");

and am forwarding to a jsp. In the jsp I am accesing as follows:

String someName = request.getParameter("someName");
String someOtherName = (String)request.getAttribute("someName");

Now <someName> is NULL & <someOtherName> is "SomeStringToSendToScreen".

Why is there a difference? What am I not understanding here? This still in the same request scope?....

-Arun.
[ October 15, 2004: Message edited by: Arun Karthik ]
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arun ,

Herez my understanding about two methods to access request parameters.
apart from .getAttribute() gives us an OBJ & .getParameter() gives a String.


This is the main difference between both methods.
setAttribute and getAttribute is provided to pass and retrive an java Object between servlets and jsps. where as getParameter is usee to fetch the parameters submited by user either by submiting form or by query string.

Now coming to your problem as you have set parameter using setAttribute
you MUST fetch it using getAttribute.

Hope this helps
Cheers
Praful
[ October 15, 2004: Message edited by: Praful Thakare ]
 
Arun Karthik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Praful,

I think that is it! Thx!

But just in case, if anyone else thinks there may be more to this, please enlighten me!!

(I Love Java!)
-arun
 
Himanshu Bisht
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
ya tha basic diference is what is told above.
you can also understand it like

.getAttribute is used to get values from sessions whereas .getparameter()
is used to get values from jsp or html.


himanshu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic