wood burning stoves 2.0*
The moose likes Servlets and the fly likes Passing resultset from 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 "Passing resultset from Servlet to JSP" Watch "Passing resultset from Servlet to JSP" New topic
Author

Passing resultset from Servlet to JSP

Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi friends,
I involved in a project and recently i get up from a big mess up in MySQL. After that,i do another mess up. There i need the resultset obtained in the servlet page(A bean file) to be passed to the Invoking Servlet file and then it can be accessed by JSP file. I tried so many ways i even use session to bind the resultset and pass it it JSP page it seems to be not a good one. Here i found the code and i need your help where i use it and how i access it in my JSP page..




Any help will be appreciated...


Never try to be a hard-worker. Be a smart-worker.
My Blog
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Where is the problem? You have shown us the code for extracting the values from the ResultSet, but how is this causing an issue?
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445



The code for store resultset in an ArrayList... Then the value is returned to a servlet file...


My question is how can i pass the resultset to my JSP page....
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

This is the bean file to store the resultset in an ArrayList...



Then the value is returned to a servlet file...
servletrs.java


My question is how can i pass the resultset to my JSP page....
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

I paste my full code here for your verification
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459



and then what?
To make the information available to the JSP I would expect to see something like this:

Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Yes i pass the resultset to JSP by the setAttribute() method but it fails to pass to the report.jsp page. May be i had some doubts on it. Will you please tell me how can i get that in my JSP page?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459


That's about it. The JSP gets called using the same Request object, the request contains the data, the JSP reads the data.

Where are you calling include or forward to the JSP? sendRedirect won't work, it causes a new request to be sent and hence loses the data you put on it.
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159
Hi I have done this thing which you are expecting Mr.Raj.

As David suggested... there I have added my object to a session object.

session.setAttribute("",) And further retrieved that in my JSP for display purpose it worked for me.

What I did in a glimpse:

1. Fetching the data from DB.
2. Added those record to ArrayList();// Here create a JavaBean with you Table format and add the object of that class in the arraylist.

eg: table--> name, no
class User{
getName()...}
ArrayList al=new ArrayList(new User());
3. Added that arraylist to session.
4. retrieved that object in JSP.


This is how I followed.


SCJP 1.4 & 1.5, SCWCD 1.5. Learn and Let Learn.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Would you please give me an example code 'cause i do accordingly to your instruction but i can't get it and i really messed things as a whole.
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
1. You have given us 2 methods named resultSetToArrayList() and arrSend(), I can see you are calling the latter from doPost(), where are you calling the other one? Should it be used?

2. Are you sure the list "blah" you want, has the correct results in the format you need?

3. In general, you might have to do these in doPost(), so the data correctly gets transferred to the JSP.
3.a. After you properly populate "mb", the bean, in doPost() of the servlet, you have to set it as an attribute in the request object

3.b. After this, you have forward the request to your JSP page.

3.c. You can then access this bean in report.jsp as follows:


Please note that the attribute will be available only if you forward/include using RequestDispatcher. If you use response.sendRedirect(), you would lose the request attributes.


keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi RajKumar,

The suggestions which the people have given so far would work for you. Not sure why you could not get the results into your jsp, unless you are actually creating a new request object (Not the one having hte resultset). This can happen if you do a redirect I believe, instead of RequestDispatcher forward or include. Try to check if the request objects are same on jsp page and in servlet. If they are same, then the Resultset should be available, if not obviously Resultset would not be available.


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Really my problem is i don't know how to retrieve values in the JSP page from the request object and i am still looking for an solution on that. And thanks for your replies...
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159
session.setAttribute("get",get");

ArrayList al=(ArrayList)session.getAttribute(get);

This is how we get the object and use accordingly.

Sorry Raj I lost the code now. But am sure this works.

Be patient is debugging the code.

Once again an overview...

Add it to a session object

session.setAttribute("get",get);

after forwarding I mean RequestDispathcer...

session attribute is always true by default in jsp.

so Use session.getAttribute(get);

and typecast the same to your own format and use it.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

I retrieve it by session.getAttribute("get") and how do i print my value in JSP file? Help me regards this.
Jetendra Ivaturi
Ranch Hand

Joined: Feb 08, 2007
Posts: 159
Suppose that get object you have added is an arraylist.

Then

ArrayList al=(ArrayList)session.getArrtribute("get");

and Iterate the ArrayList now.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

I am using AJAX along with JSP to pass the value of combo box to servlet and to fetch the value from DB without refreshing the page but it fails to works and the ajaxFunction is not triggered when i select a value from combobox. Here is the AJAX code for that



Will someone please tell me this function is to just pass the value to servlet. How can i fetch my data from server to AJAX. I right now use session to put my values and get it through in my JSP page.But it shows null values in the session field.Please give me a solution
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing resultset from Servlet to JSP
 
Similar Threads
Unique Values in ArrayCollection?
Converting ResultSet String to ArrayList
CallableStatement resultset returns only columns and no data, where data exists.
JSTL, JDBC, and Result Set
add all selected data to datalist and send back to servlet