So, I'm fairly new to java, fairly new to servlets and very new to jsp pages.
I've been given a project in which I need to utilise all 3 with a database to create what is effeectively an exchange website. It's intended to follow a MVC model.
I've had a read through some of the posts on here adn I'm getting a touch concerned.
For one of my main 'viewAll' pages I have somehting along the lines of
I realise that the last example is wrong and I'm working on amending that but I thoguht the first woudl be ok to use?!
For this example page (the code is somewhat replicated for other similar pages) I have:
JSP that displays a link to viewAllSomething
>viewAllSomething goes to servlet where the object is created and then passed as a bean
>Next JSP uses bean and displays relevant data.
i don't know how else I can loop through the arrayList without using the code above.
Could anyone perhaps say for definite whether it is bad pratice or not and if so, coudl you point me in the right direction for an acceptable solution please?
Also, any other advice relating to servlets etc is greatly appreciated.
JSP2 was introduced in 2002 with the JSTL and EL to replace discredited scriptlets. So your JSP looks like it was written in 2001! Time to catch up.
What's worse, you appear to be mixing scriptlets with the EL. That's the worst of all worlds.
To iterate over a collection, you'd use the JSTL's <c:forEach> tag, not scriptlets containing a Java for loop.
Thanks for the reply and your time.
I'm glad I asked. Although now worried about the amount of reworking. I think though that as I have the bean I shoudl be fine.
I'm at work at the moment and unable to test but as soon as I get back I will and post a response.
is the '<c:' a standard tag or would that need ot be defined someplace else?
With regards to the out.printlns that I foolishly added. (The learning curve of the project was such that at the the start there were outlns, in the middle there was the mess I posted earlier and at the end it will be as you've suggested) but, can I apply the same c:forEach loop to that as well?
And no, you should never be using out.println(). Within a forEach, or otherwise. No Java code in the JSP. None. Ever.
The JSTL and EL provide 99% of what you should be doing in a JSP. If you can't do it with that, it's likely something that you shouldn't be doing in a JSP at all. For the other 1%, there are custom tags and EL functions that can be created.
Bear Bibeault wrote:The JSTL consists of four libraries: the 'c' library stands for core. And yes, it's standard.
But you don't get it for free like you do with the jsp standard tags (e.g. jsp:useBean). You must add it as a taglib directive, and have the JAR files present for your web application.
I'm using netbeans and it seems the JSTL(?) is already included, I tested the forEach on a test page and it works a charm, however…
I have a class that contains all the methods etc required so a sample object, a property. I then have a separate ArrayList class of this object. So a user can have many properties etc. The class only has getters for each property via an index.
I'm fairly bad at explaining so I'm doing something like this:
I've removed most of the code but you get the idea. In this class I have a 'getNumberOf' that returns the size of the ArrayList and tin the JSP I was cycling through each of them
Again, I've removed a lot of code. What I want is a drop down list displaying each of the properties the user owns.
I cannot access the method getNumebrOf without using java (?) and I cannot get the index value of the ArrayList to loop through the data.
Could you explain what I need to do please?
Many thanks again
Steve Green wrote:In this class I have a 'getNumberOf' that returns the size of the ArrayList and tin the JSP I was cycling through each of them
Poor approach. You should never have to get the size of the collection, and your API shouldn't be doing the indexing. Rather, have a property that returns the collection itself and then the forEach should iterate over that.