File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes unable to pass value of resultset to servlet to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "unable to pass value of resultset to servlet to jsp" Watch "unable to pass value of resultset to servlet to jsp" New topic
Author

unable to pass value of resultset to servlet to jsp

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 755
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 ]

Jigar Naik


A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
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

Joined: Jan 01, 2007
Posts: 164

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...


Milan.<br />I can because I think I can...
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 755
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

Joined: Jan 01, 2007
Posts: 164
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Dec 12, 2006
Posts: 755
thanks all
thai le
Greenhorn

Joined: May 24, 2007
Posts: 1
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

Joined: Apr 20, 2006
Posts: 3344

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

thai le. please do not post your own questions in someone else' threads. Rather, please start your own topic for your question. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to pass value of resultset to servlet to jsp