wood burning stoves 2.0*
The moose likes JSP and the fly likes Passing ResultSet from a Servlet to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Passing ResultSet from a Servlet to JSP" Watch "Passing ResultSet from a Servlet to JSP" New topic
Author

Passing ResultSet from a Servlet to JSP

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Hi I actually wanted to pass Data from a servlet to a JSP form. I was thinking that even if I pass the ResultSet to a JSP I would still need to do a certain amount of scripting to display the contents in the JSP rite ?? I actually plan to keep my jsp script free - its only purpose would be to display data. Any idea on how could i accomplish this ??


Don’t look where you fall, but where you slipped
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

why do you are passing ResultSet? instead pass a DTO/Bean which contains the value of ResultSet via request object to the target jsp and display the values there
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Seetharaman Venkatasamy wrote:why do you are passing ResultSet? instead pass a DTO/Bean which contains the value of ResultSet via request object to the target jsp and display the values there


I have thought about that ,,,,,
Passing a bean which contains my ResultSet data ?? I guess that would work if the ResultSet had only one row ...
Suppose that the result set contained more than one row.. How would i accomplish this ?? will i have to create an array of object as a bean ?? Another question to display all these rows i would still need to write iteration code in the jsp to display all the rows rite ??
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Adam Zedan wrote: will i have to create an array of object as a bean ??

Yes, ArrayList woud be better choice!

Adam Zedan wrote:
Another question to display all these rows i would still need to write iteration code in the jsp to display all the rows rite ??

use *JSTL* for loop for that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Seetharaman Venkatasamy speaks wisely. The result set should get anywhere near the user interface. Database resources are precious and result sets and statements should be closed as soon as possible.

In a properly structures MVC app, the database constructs should never escape the Model.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:Seetharaman Venkatasamy speaks wisely. The result set should get anywhere near the user interface. Database resources areon precious and result sets and statements should be closed as soon as possible.

In a properly structures MVC app, the database constructs should never escape the Model.


Okay here is my initial .. attempt to access a database in the attempt of creating a well structured application.
I have kept a separate singleton class which is only responsible for dealing with the database so far
The class is

The servlet calls the DBInterface as such



Now i do have a question:

Q- Since i am passing an Arraylist to a JSP page and each entry in an array list is separated by a delimiter , wont the receiving JSP page have a lot of work to do (i.e separate the values from the delimiter then display them...)

Q-I would like to hear what you experts suggest... on how this design could be improved or if it is worth improving. I would specially love to hear about your suggestions on how and should values in arraylist be processed and split using a delimiter during the displaying process..??







Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Adam Zedan wrote:I have kept a separate singleton class which is only responsible for dealing with the database

A singleton? You expect this app to be only ever be used by one person?

Since i am passing an Arraylist to a JSP page and each entry in an array list is separated by a delimiter , wont the receiving JSP page have a lot of work to do (i.e separate the values from the delimiter then display them...)

Yes. That was not a good decision. Why did you make it?

I would like to hear what you experts suggest... on how this design could be improved

Get rid of the singleton. As a rule of thumb, the answer to "should this be a singleton?" is "no" (unless you are an expert and can write a 7-page dissertation justifying why to makes sesne for a class to be a singleton).

I would specially love to hear about your suggestions on how and should values in arraylist be processed and split using a delimiter during the displaying process..??

I'm still at a loss about the whole delimiter thing. It just seems totally bizarre. Why would you gang up separate values in a string separated by a delimiter rather than just put them into a collection?







Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

@Bear Bibeault

Yeah putting a singleton here is definitely a terrible idea... will get rid of it in a sec.

as for Arraylist... could you please suggest a collection container...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

What are you storing? A number of strings? Another List or an array would be fine. I'm still befuddled by the whole delimited string thing. What were you thinking?
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:What are you storing? A number of strings? Another List or an array would be fine. I'm still befuddled by the whole delimited string thing. What were you thinking?


Actually my table is something like this
ID | Name | Weight
12 Adam 80
45 Kenny 120
............................
What i was trying to do was pass the column title and results back to caller.
The string would contain String Column_Title = ID#Name#Weight
and Arraylist result = 12#Adam#80 ... and so on

these two will get passed to the jsp and the jsp will split the column title on the basis of delimiter and display the columns in the table
and the result will be splitted on the basis of its delimiter and will be populated accordingly...
Thats was my plan...

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

But why? I'd create a bean that contains the data for each row and pass the data as a List of such beans. Converting data into strings and packing them with delimiters is just, well, bizarre.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:But why? I'd create a bean that contains the data for each row and pass the data as a List of such beans. Converting data into strings and packing them with delimiters is just, well, bizarre.


In that case wont each row in the table act as a separate bean ... and if there are lots of rows wont that result in a lots of beans ???
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Sigh. So what? Have you experienced performance issues? Demonstrable issues?

Please avoid premature optimization. Nothing is as damaging as using weird and bizarre techniques to do things because you think that the straight-forward means will "make a lot of beans" or some other such premature concern.

The rule: always do things in the most straight-forward and clear manner. And then, and only then, if (and only if) there are demonstrable performance issues, start profiling to determine optimization techniques.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:Sigh. So what? Have you experienced performance issues? Demonstrable issues?

Please avoid premature optimization. Nothing is as damaging as using weird and bizarre techniques to do things because you think that the straight-forward means will "make a lot of beans" or some other such premature concern.

The rule: always do things in the most straight-forward and clear manner. And then, and only then, if (and only if) there are demonstrable performance issues, start profiling to determine optimization techniques.


Great Message well Received .. thanks for the advice...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Another piece of unsolicited advice: you have written your code using fake generalization. In other words, you decided not to have a Weight bean to represent a row of your "weights" table, but instead to use something "general". This turned out to be simply a bunch of strings, regardless of whether strings were the best way to represent the data.

If you're writing an application for a particular purpose, then a generalized class which can return a list of lists of strings for any query at all is overkill. You should write specific code which returns a list of beans tailored for each query you're going to use in your application. (Or else it isn't overkill, it isn't good enough generalization. But good-enough generalization is Hibernate and you shouldn't try to write that over again.)

You haven't got there yet, but using a generalized class to which you pass your query as a string prevents you from using PreparedStatements to run the query and forces you into building the query statement via concatenation. Not only is this cumbersome (you have to be careful with quotes around string constants and you have to make your date formats match what the database demands), it also puts your database at risk of SQL injection attacks.

And as soon as I started reading the thread I knew, without looking at the code, that you weren't closing your ResultSets. You should do that immediately after you finish using them, so you don't make your database hang onto resources that you don't need any more. (It's hard to do that if you're passing the ResultSet around to various classes, which is another reason not to do that.)

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:
You should write specific code which returns a list of beans tailored for each query you're going to use in your application.


I guess That means the class methods should be able to accommodate any type of a Query...




Paul Clapham wrote:
You haven't got there yet, but using a generalized class to which you pass your query as a string prevents you from using PreparedStatements to run the query and forces you into building the query statement via concatenation. Not only is this cumbersome (you have to be careful with quotes around string constants and you have to make your date formats match what the database demands), it also puts your database at risk of SQL injection attacks.

I completely agree with this , that why i usually prefer stored procedures ... less cumbersome and more secure. Here i am just practicing...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Adam Zedan wrote:
Paul Clapham wrote:
You should write specific code which returns a list of beans tailored for each query you're going to use in your application.


I guess That means the class methods should be able to accommodate any type of a Query...


No, that's premature design. That method you displayed in your last post should handle ONE query. So you don't pass the query as a string from somewhere else, you pass nothing. Or perhaps if the query has parameters, you pass them in so they can be used properly in the PreparedStatement. The query string which you're going to use in the PreparedStatement should appear in the code of that method.

If after a while you find you have several different methods which look very similar because they are all doing something very similar with a query about movies, then refactor at that point. Don't write generalized code at the beginning, you ain't gonna need it.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:
Adam Zedan wrote:
Paul Clapham wrote:
You should write specific code which returns a list of beans tailored for each query you're going to use in your application.


I guess That means the class methods should be able to accommodate any type of a Query...


No, that's premature design. That method you displayed in your last post should handle ONE query. So you don't pass the query as a string from somewhere else, you pass nothing. Or perhaps if the query has parameters, you pass them in so they can be used properly in the PreparedStatement. The query string which you're going to use in the PreparedStatement should appear in the code of that method.

If after a while you find you have several different methods which look very similar because they are all doing something very similar with a query about movies, then refactor at that point. Don't write generalized code at the beginning, you ain't gonna need it.



Thanks for the advice will keep that in mind..
 
GeeCON Prague 2014
 
subject: Passing ResultSet from a Servlet to JSP