aspose file tools*
The moose likes Servlets and the fly likes Sending rows of data 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 "Sending rows of data from Servlet to jsp" Watch "Sending rows of data from Servlet to jsp" New topic
Author

Sending rows of data from Servlet to jsp

Jeanne Pierre
Greenhorn

Joined: Feb 23, 2006
Posts: 19
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 ]
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
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.


Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
Jeanne Pierre
Greenhorn

Joined: Feb 23, 2006
Posts: 19
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

Joined: Feb 23, 2006
Posts: 19
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

Joined: Feb 23, 2006
Posts: 19
Would this be legal in the servlet?....


Thanks again in advance!!
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi,


Servlet code


in JSP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
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


Who dares, wins! (SAS motto)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61206
    
  66

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
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Jeanne Pierre
Greenhorn

Joined: Feb 23, 2006
Posts: 19
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

Joined: Aug 29, 2003
Posts: 130
Hi,

You need to import ArrayList and the other class for which you're getting such errors by <@ page import=...
Jeanne Pierre
Greenhorn

Joined: Feb 23, 2006
Posts: 19
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending rows of data from Servlet to jsp