Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Difference between getParameter() and getAttribute()

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
whats the Difference between getParameter() and getAttribute() in Servlets??
Thanks,
Sajee Joseph
 
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
parameters are from the client hile attributes are from the server.
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Anselm says is correct, Sajee... The client has to specify the parameters in the browser, whether in the form or in the browser's address field... After that, servlets can fetch them by using getParameter method...
But for getAtribute method, they have to be set by the server-side components with setAttribute(String, Object)and later can be fetched by using getAttribute(String)...
Hope this helps you understand the concpet. For more understanding, you need to do hand-on practices as well...
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A parmeter is a String, whereas you can store objects in attributes. Besides if you have a look at ServletRequest interface, you'll find that you can use setAttribute but there is no setParameter().

Typically, parameters are send by the user browser via HTTP GET/POST, whereas the servlet/jsp sets and reads attributes for communicating among themseleves.

HTH,
- Manish
 
Author
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What's the Difference between getParameter() and getAttribute() in Servlets??


Big difference!
  • The getParameter method accesses HTTP form parameters that came from the client. Ie if there was a textfield in the form with a NAME of firstName and a value of Scott McNealy, then request.getParameter("firstName") returns "Scott McNealy" (not "Scott+McNealy", since parameters are automatically URL-decoded).
  • The getAttribute method accesses arbitrary Java objects that were stored by other server-side programs in the hash table that is attached to the ServletRequest object. This hash table is very commonly used in the MVC architecture: a servlet invokes code that returns some data, this data is stored in that hash table with request.setAttribute, RequestDispatcher.forward is used to transfer to a JSP page, and the JSP page uses jsp:useBean (with scope="request") and jsp:getProperty to output the values. (Unless you are using JSP 2.0, where the new expression language provides much more succinct access to these bean properties).


  • OK?
    Cheers-
    - Marty
     
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    getParameter - Is used for getting the information you need from the Client's HTML page
    getAtribute - This is used for getting the parameters set previously in another or the same JSP or Servlet page. Basically, if you are forwarding or just going from one jsp/servlet to another one, there is no way to have the information you want unless you choose to put them in an Object and use the setAttribute to store in a Session variable. Using getAttribute, you can retrieve the Session variable
     
    Ranch Hand
    Posts: 69
    Mac OS X Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Badri Viswanathan wrote:getParameter - Is used for getting the information you need from the Client's HTML page
    getAtribute - This is used for getting the parameters set previously in another or the same JSP or Servlet page. Basically, if you are forwarding or just going from one jsp/servlet to another one, there is no way to have the information you want unless you choose to put them in an Object and use the setAttribute to store in a Session variable. Using getAttribute, you can retrieve the Session variable



    in getAttribute() you are talking about the Object , where is the Object here ???
    please explain in brief , if possible
    thanks , in advanced ......


     
    Sheriff
    Posts: 67634
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It can be any object that you want to store in the context.
     
    munjal upadhyay
    Ranch Hand
    Posts: 69
    Mac OS X Oracle Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:It can be any object that you want to store in the context.


    I get it, thanks...

     
    The only cure for that is hours of television radiation. And this tiny ad:
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic