Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Bad Pratice? Have I been doing this project 'all' wrong?

 
Steve Green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

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

or


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.

uber thanks!



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, you're doing it wrong. Unless you are working on older legacy pages you should not be putting Java code into your JSPs.

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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple example:
 
Steve Green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

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?

Thanks again
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSTL consists of four libraries: the 'c' library stands for core. And yes, it's standard.

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.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, I missed that aspect of the question.

Best bet is to grab the JSTL Specification and give it a read.
 
Steve Green
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


where "currentUserProperties" refers to that ArrayList<PropDetails> object which you will have put into request scope in your controlling servlet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!