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

Passing resultset from Servlet to JSP

 
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I paste my full code here for your verification
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic