Retrieving lists from other layer into a JSP page - Design Question
Hi all,
What should be a good design technique in order to allow a JSP page to present some list obtained from other layer of my application. I intend to leave only presentation logic in the JSP layer, while all data access will be in other layer.
For example, I have a JSP page that will show the results of a SQL query originated from an entry form in other JSP page. Let's name them 'search.jsp' and 'result.jsp'
I want that all JDBC access be done in some other layer.
Question 1:
What would be better for this task? A servlet or a bean? (A bean, I suppose?)
Then this bean (?) would have to find some way to pass all the result set into this 'result.jsp'. I think it would be very poor design if I used some Data Object, because it would have to store all rows and pass them at once.
I don't want, either, that the presentation (JSP) layer access directly the data access layer (JDBC, files...). This way, I don't want any JSP page accessing the Resultset object directly.
Question 2:
What do you think could be a good design technique?
I thought of some intermediate layer that provided data objects and data access interfaces. I mean, as my data will come from different sources, like some database (JDBC), some Electronic Document Manager, I thought of having a common access interface, that would provide methods for accessing the resultsets. This could be even a java.util.Enumeration that has a hasMoreElements() method that could be implemented as the next() method from JDBC result sets and the nextElement() method that would actually bring the next row of data.
This way the JSP layer would see only hasMoreElements() and nextElement() methods from the middle layer. On the other hand, the data layer would provide as many different implementations to this access interface (Enumeration?) as there were data sources (JDBC, Files, ...)
Question 3:
What do you think of this design?
Question 4:
Is there any flaw in my approach? Which?
Question 5:
Could you suggest other designs?
I really thank you all, forany kind of answer. I actually use this design for development, however I've never seriously discussed it with someone... this is going to be of great value for me.
Regards,
Filipe Fedalto
[2376 byte] By [
filcobra] at [2007-9-26 7:27:44]

1. I suggest that you use both servlet and bean. Use the service2worker pattern. Handle requests with a front controller servlet, do some initial validation such as authentication and authorization with the help of a helper or command.
The controller should use a command or business delegate to retrieve the data (a bean) or Resultset.
Dispatch to your view, perhaps using a Dispatcher.
Your view is the JSP.
The bean or Recordset (you can encapsulate the Recordset inside a bean-helper) can be used in your JSP page with the help of custom tags.
Forte comes with a taglib that has a tag that iterates over a Recordset.
You can buid a simple <yourtaglib:Iterate id="beanRecsetName"><yourtaglib:showProp name="fieldName" /></yourtaglib:Iterate>
or something similar.
If you use a pure bean you will have to write for or while code inside your JSP.
2. It is a good idea. But dont do it from inside your JSP.
What if something goes wrong in you helper bean (your "intermediate layer") ? Do you throw an exception and let JSP framework dispatch to a default error page ?
What if it is not a error but just a decison you should make based on the data retrived and this decision may change completly the layout of the page ?
In my opinion you may do this from the controller so the dispatcher can choose the apropriate view based on that data.
Also, if you don t want to put fors or while s code inside your JSP you will need to use a taglib. Alternatively you may put all the iteration code inside your bean and let it return the result already formatted but I dont like this approach.
3. A helper is a good design in this case
4. Just don t do it in the JSP.
5. see resp.1
Clovis
I forgot to mention that sometimes it is better to read all data you need, process it and return it at once using a Rowset or data object.
If you have your presentation tier and ejb tier in diferent servers reading one row at a time will cause 1 network call (2 messages) per row and maybe 1 select query per row.
Also, if you want to read needed rows at once you can use other performance techniques.
".... I think it would be very poor design if I used some Data Object, because it would have to store all rows and pass them at once."
> ".... I think it would be very poor design if I used
> some Data Object, because it would have to store all
> rows and pass them at once."
No design is perfect of course, but all the decisions you will take, will have pros and contras.
Depending on the amount of data you need to transfer, you could put it all in one object, but you could as well implement a paging system, which will only return you a specific number of rows.
Advantage here of course is that the user immediately gets a result. On the other hand, you will suffer a small network lag, and thus a little performance.
Overall, I think your design is rather good, but I should use both a JSP and a Bean.
pdbc at 2007-7-1 17:22:55 >
