File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Defference between request.getAttribute() & request.getParameter() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Defference between request.getAttribute() & request.getParameter()" Watch "Defference between request.getAttribute() & request.getParameter()" New topic
Author

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

Arun Karthik
Greenhorn

Joined: Feb 26, 2004
Posts: 2
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 ]

regards,<br />Arun Karthik R
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 637
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 ]

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Arun Karthik
Greenhorn

Joined: Feb 26, 2004
Posts: 2
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

Joined: Aug 27, 2004
Posts: 96
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Defference between request.getAttribute() & request.getParameter()