This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between getParameter() and getAttribute()

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
whats the Difference between getParameter() and getAttribute() in Servlets??
Thanks,
Sajee Joseph
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parameters are from the client hile attributes are from the server.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • 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...
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Marty Hall
Author
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • 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
     
    Badri Viswanathan
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • 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
     
    munjal upadhyay
    Ranch Hand
    Posts: 69
    Java Mac OS X Oracle
    • Mark post as helpful
    • send pies
    • 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 ......


     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64701
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It can be any object that you want to store in the context.
     
    munjal upadhyay
    Ranch Hand
    Posts: 69
    Java Mac OS X Oracle
    • Mark post as helpful
    • send pies
    • 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...

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic