Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to pass value of resultset to servlet to jsp

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi



the bellow code is in my function which is in java class

want to pass the value return by the resultset to servlet and than servlet will pass all this value to jsp

How do i get that ???

its little urgent..please
[ February 13, 2007: Message edited by: Bear Bibeault ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take the result set ...and store it as an attribute to the request object(if thats the scope you would like to choose) using request.setAttribute(....) and then access the resultset in you jsp...using request.getAttribute(....)

Then when the resultset is in your jsp....loop through and get the individual rows..
 
Milan Jagatiya
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

want to pass the value return by the resultset to servlet and than servlet will pass all this value to jsp


better... wrap resultset data in Collection and set with any scope you want.... and access within your servlet and iterate in jsp...
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which collection class should i use ???

there are 10 fields in my table.

and suppose if i use array list. than do i need 10 arraylist for each field ???
 
Milan Jagatiya
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can have any Collection ....

suppose you are getting users details in resultset . userid, username, password and firstname etc.
then create a bean for user with setter getter methods for userid, username, password and firstname.

fill this objects by fetching each row from resultset. refer below code...


now set "userList" in scope you want and iterate wherever you need....
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In short,

- get a bean (Value/Transfer Object)
- populate your bean
- add that to any preferred collection, typically an ArrayList
- do it for each row in resultSet

See the code posted by, Milan.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Milan Jagatiya:
while (resultSet.next()) {
userID = resultSet.getLong("userID");
userName = resultSet.getString("userName");
password = resultSet.getString("password");
firstName = resultSet.getString("firstName");
//constructor in User bean to set each property.
user = new User(userID, userName, password,firstName);
userList.add(i, user);
user = null;
i++;
}

[/CODE]


P.S. Its better to use the setter method instead of constructor arguments.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all
 
thai le
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Milan Jagatiya:
while (resultSet.next()) {
userID = resultSet.getLong("userID");
userName = resultSet.getString("userName");
password = resultSet.getString("password");
firstName = resultSet.getString("firstName");
//constructor in User bean to set each property.
user = new User(userID, userName, password,firstName);
userList.add(i, user);
user = null;
i++;
}
---------------------------------
Then from jsp, i can access the collection from <%%> tag, right? How about the user bean inside the collection? Can i access the bean by <jsp:useBean ..>?
I have

<jsp:useBean id="user" class="Login.UserBean" scope="session"/>
<%
Vector v = (Vector)request.getAttribute("userList");
Iterator i = v.iterator();
while (i.hasNext())
{
user = (UserBean)i.next();
%>

<li>
<a href="CORE/UserManager?cmd=get&id=
<jsp:getProperty name="user" property="id" /> ">
<jsp:getProperty name="user" property="pass" />
<jsp:getProperty name="user" property="name" />
</a>
<% } %>
But the output is always empty, the bean used in <jsp:getProperty ...> is an empty bean instead for a user bean as expected.
did i miss anything?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

just check the scope to which you set the attribute and the scope from which you get are the same.

You are getting the userbean in the session scope. Is it the same session scope you have set?
[ May 24, 2007: Message edited by: Raghavan Muthu ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thai le. please do not post your own questions in someone else' threads. Rather, please start your own topic for your question. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic