My first exposure to servlets was a little over 2 years ago and to JSP even more recently than that. I think I've picked up a lot about what constitutes best practice in constructing a web app today but I'm interested to know how things have developed historically.
I've come across code using servlets exclusively to serve content; JSP with scriptlets but no custom tags; JSP with custom tags but no JSTL; JSP with JSTL; and lastly JSP with Struts. I've not yet cast more than an interested glance at JavaServer Faces.
Once JSTL enters the fray best practice is pretty clear to me. However, I'd like to know what went before. Specifically what was considered the cleanest way to present the contents of a SQL ResultSet in a JSP page. I guess looping through some sort of Collection of beans is the best I can come up with. Presumably with the addition of taglibs in JSP 1.1 you could define your own forEach tag if you felt so inclined.
I'd appreciate it if anyone can point me to a reference example or, if sufficiently confident, provide some of their own code as an illustration of best practice with JSP 1.0 or 1.1. I've trawled around the web a bit but I've not found anything that looks particularly slick.
Having been working with JSP since 0.92, I've been through it all. From putting all the Java, SQL and all, on the JSP page -- boy, I learned not to do that quickly, through migrating the Java into beans (a la Model 1), through migrating the Java into Servlet controllers (aka Model 2), through present-day JSTL/EL scriptless pages.
What do you want to know? I'm not in a position to post older code, either because I no longer have access to it, it no longer exists, or is proprietary.
I guess my question is what was considered the cleanest way to present the contents of a SQL ResultSet in a JSP page prior to the advent of JSTL and taglibs? Can anyone offer a code example or a link to one?
Perhaps I should tidy up my effort and post it for review.
What I did in the Model 2 phase, which is where I think your question lies on my "timeline", was roughly:
1) Servlet controller obtains POJO collection from model classes -- all SQL and result set handling hidden by UI-agnostic model. 2) Controller places collection on request and forwards to JSP 3) JSP uses scriplets and scriplet expressions to iterate over collection and display values
The only chnage I've made is that now the scriptless JSP pages use JSTL and EL (as well as app-specific custom tags as necessary) to replace the scriplet iteration and conditionals as well as the scriplet expressions. In many cases I've had to adjust the API of the POJO value objects being sent to the page to make them "EL-friendly" (in other words, strictly bean-patterned or an implementor of Map). [ September 08, 2004: Message edited by: Bear Bibeault ]
Joined: Aug 02, 2004
Thanks Bear. That's the architecture I had in mind. I'm a little unsure of my terminology here; does your Collection contain value objects or is it a value object itself? Also, what would it be a collection of? I'm thinking of a List of beans; I don't quite see how a Map fits for a ResultSet with multiple rows and columns.
I think I'll complete my example and post it here as I feel there are quite a few Ranchers who aren't yet using JSTL or even MVC who would hopefully benefit from a simplified example.
I'm a little unsure of my terminology here; does your Collection contain value objects or is it a value object itself? Also, what would it be a collection of?
Depends upon circumstances. It could be as generic as Object (as I just posted in another topic) when the JSP page doesn't need to know anything about the data, but is frequently either a List or array of POJO value objects (to preserve order).
I find the scriptless pages much easier to extend, maintain and refactor than even I expected. I have noticed no discernable performance difference.
Joined: Jan 30, 2003
With jsp2.0 I should be able to do away with everything, is that correct?
but I'm worried about performance.
I would take one of your more complex pages and convert it. Measure the performance to see if it suits your needs. I have done no quantitative measuring since I have not felt the need to, having noticed no qualitative performance issues.
The JSP2.0 import a new language(EL),you can implement all works by it,you must use scriptle to finish logic handle before jsp1.2 appeared,it isn't easy maintain,readability,refactory,so if you will using taglibs and JSTL,that's a good idea! [ September 12, 2004: Message edited by: dennis he ]