• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Difference between getAttribute and getParameter

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Team

i have a very basic doubt in servlet. what is the differnce between getAttribute and getParameter.
what are all the situations where we are using both of them?
 
Marshal
Posts: 67496
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getAttribute() is used to fetch scoped variables.

getParameter() is used to fetch request parameters from the GET query string or POST body.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getAttribute() is used to retrieve objects which are stored in the request, session or application context scopes of the application using setAttribute()
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then, getAttribute() is used in the JSP page to get the results that are set into a scope(request, session...) and getParameter() is used to get the values we are getting from a JSP page.

Am I right?
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like Bear Bibeault said:

getAttribute() is used to fetch scoped variables.

How you scope them? In a serlvet you do request.setAttribute("attrName", attrValue); So you can only use it within the same request. For example, in a servlet you retrieve some data from database, a record, you "scope it" as request attribute, and if your servlet is forwarding the request to another servlet or jsp, you can continue using that scoped attribute.

getParameter() is used to fetch request parameters from the GET query string or POST body.

a) You use it when you need to retrieve params from the query string, for example: http://hellou.mx?param1=hola¶m2=amigos, request.getParameter("param1") will give you back "hola".
b) You can use it to retrieve params comming from a post (for example, when user submitted an application/x-www-form-urlencoded form).
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You all.
now I got an idea..
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You all.
now I got an idea..
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the explanation is really good
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic