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

Sending rows of data from Servlet to jsp

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a html page that has a drop down menu and a submit button. Once the item in the drop down is chosen and the submit button is clicked then the html page requests some information from the servlet (which accesses a database) and then it is supposed to send/display it to a jsp. I am unsure of how to send the information that is access by the servlet, to the jsp page. I have this block of code that I know must be included in order to forward the request.


The problem is that I have rows of data to send back to the jsp and I am unsure of how to do it. Thank you in advance!!
[ March 13, 2006: Message edited by: Jeanne Pierre ]
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Probably you can follow the ValueObject pattern. In which, create a javabean with all of the properties you need. In the servlet, traverse through the resultset and for each row, create an instance of that javabean value object class duly filled by the values you get for that row. Add such javabean instance in an arraylist object. After the traversing is done, put that object as an attribute in request object and then do the forward to the JSP. In the JSP, simple write the logic of traversing that ArrayList object to show the values for each row.

I hope this helps. I've tried to explain in detail but if you need an example on this, let me know.
 
Jeanne Pierre
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok cool thank you so much. I will try to see if this works and let you know/see what I did and let you know the outcome....Thank you so much for your idea!!!
 
Jeanne Pierre
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.... sorry I think I will need an example...I was trying to do a google search of how value object works but I was not very lucky. If you could give me an example of point me to a web site that has a detailed explanation and an example I would greatly appreciate it. Thank you
 
Jeanne Pierre
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would this be legal in the servlet?....


Thanks again in advance!!
 
Bimal Patel
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Servlet code


in JSP
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Pierre:
Would this be legal in the servlet?



Depends what you mean by "legal".

If you mean "best practice", then no. Anytime you import java.sql.* into a servlet, you can be sure that you are violating "separation of concerns". Database code has no place anywhere in the presentation tier.
 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben, what exactly is 'separation of concerns?' please let me know why it' s so bad to import java.sql.* into servlet. I seem to be doing that a lot.

with regards the initial question, one way I handle it is to return the ResultSet in the jsp page and basically use a loop right there. somehow i'm guessing its not the most elegant way to handle this
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kwame Iwegbue:
Ben, what exactly is 'separation of concerns?' please let me know why it' s so bad to import java.sql.* into servlet. I seem to be doing that a lot.



Look up the concept of the MVC pattern
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initially it is okay, while you are learning servlets/JSPs. Once you get the code working, refactor all your database calls to a DataAccesObject (google for DAO pattern) and move your Database connection code a another utility class called ConnectionManager. The xxxxDAO class will make use of this ConnectionManager class to get a connection and your servlet will use the xxxxDAO class to map relational data to objects (i.e value object) and return the value object to your servlet.
 
Jeanne Pierre
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is so cool....I thought I got the concept of what you were saying. For some reason I don't think the list contains anything. I am just not getting what I expect. I am trying to display one of the object to the screen but I get "cannot resolve symbol" errors. I don't even know if it the servlet that is not sending the information, and I'm pretty sure that my error occurs in the way I am accessing/handeling the objects/methods.

JSP:


Updated Servlet code:


Error:
 
Bimal Patel
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You need to import ArrayList and the other class for which you're getting such errors by <@ page import=...
 
Jeanne Pierre
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The errors I don't think are coming from the import they are coming from the way that I access the information from the list. I don't know how to reference one particular object in the list.
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic