File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Defference between request.getAttribute() & request.getParameter()" Watch "Defference between request.getAttribute() & request.getParameter()" New topic

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

Arun Karthik

Joined: Feb 26, 2004
Posts: 2

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?....

[ October 15, 2004: Message edited by: Arun Karthik ]

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

Joined: Feb 10, 2001
Posts: 642
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
[ 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

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!)
Himanshu Bisht
Ranch Hand

Joined: Aug 27, 2004
Posts: 96
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.

I agree. Here's the link:
subject: Defference between request.getAttribute() & request.getParameter()
It's not a secret anymore!