• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confused between attributes and parameters

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain to me the difference between Attributes and Parameters?
I keep getting confused and use getAttribute and getParameter in the wrong manner at times.
Thanks,
Amit
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Amit,

I will try to explain you the difference between getAttribute and getParameter.

(1) getParameter:

For E.g. There is one html page named Test.html and servlet named Test
Test.html contains few parameters for user registration on
web site and those parameters you want to store in database.

Sample Code for Test.html

<html>
<body>
<form name="reg" method="post" action="/servlet/Test">
<table>
<tr>
<td>
<input type="text" name="FIRSTNAME" size="25">
</td>
</tr>
<tr>
<td>
<input type="Submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>

When user will enter value in the text field and press submit button, it will call Test servlet. Now you want the value entered in textfield by user in servlet so there you use getParameter method.

String lFirstName = request.getParameter("FIRSTNAME");
(This you will write in your servlet)

This method is responsible to take values from prevoius page to the forwarded page. Using this method you can retrive only one value. This method i.e. getParameter method is in ServletRequest interface which is part of javax.servlet package.


(2) getAttribute :

You want to take values from database in servlet and display them in jsp. Now you have resultset filled with data ready in servlet then you use getAttribute method and send this resultset to jsp where it can extracted and result is shown to the user.

in servlet -->

ResultSet lResultSet = // code to get data.
request.getAttribute("EMPDATA",lResultSet);

in jsp -->

ResultSet lResultSet = (ResultSet)request.getAttribute("EMPDATA");

So using getAttribute one can retrive resultset or java bean etc. getAttribute method is present in Servlet Interface which is a part of javax.servlet package.


I hope this will help you.

regards,
Amit Kulkarni
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic