aspose file tools*
The moose likes Servlets and the fly likes Difference between getParameter() and getAttribute() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Difference between getParameter() and getAttribute()" Watch "Difference between getParameter() and getAttribute()" New topic
Author

Difference between getParameter() and getAttribute()

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hello,
whats the Difference between getParameter() and getAttribute() in Servlets??
Thanks,
Sajee Joseph
Anselm Paulinus
Ranch Hand

Joined: Sep 05, 2003
Posts: 389
parameters are from the client hile attributes are from the server.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Jan 02, 2003
Posts: 111
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


    Java training and consulting<br /><a href="http://www.coreservlets.com/" target="_blank" rel="nofollow">http://www.coreservlets.com/</a>
    Badri Viswanathan
    Greenhorn

    Joined: Jan 06, 2003
    Posts: 4
    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

    Joined: Sep 18, 2010
    Posts: 69

    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

    Joined: Jan 10, 2002
    Posts: 60053
        
      65

    It can be any object that you want to store in the context.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    munjal upadhyay
    Ranch Hand

    Joined: Sep 18, 2010
    Posts: 69

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

    I get it, thanks...

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Difference between getParameter() and getAttribute()
     
    Similar Threads
    difference between getParameter() and getAttribute()
    Difference between getParameter and getAttribute
    What is the diff b/w getAttribute and getParameter
    getParameter()
    getParameter,getAttribute