This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ArrayList/List and EL

 
Ranch Hand
Posts: 39
Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

So I have a method that returns a List to a servlet. The servlet sets the List in the request for use on a JSP.

Now my problem is this, I use EL to print the values of the List using



Now this prints [Accommodation Name, Accommodation Town, Accommodation County, Accommodation Postcode]

Is there a way to access the elements within that row individually(using EL)? So I can print "Accommodation Name(As a href to another page), Accommodation Town, Accommodation County, Accommodation Postcode" without the square brackets

If not, I assume that because its a List I can't access the individual elements of each row? Does this mean I need to use ArrayList instead?

Regards

TH

 
Tom Hilliard
Ranch Hand
Posts: 39
Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I may have figured it out.

I have changed my methods to return ArrayList instead of List.

Then changed my El to



Which results in Accommodation Name being printed by itself :)

TH
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not need to return the specific List implementation. In fact, doing so is a rather poor practice. DId that actual end up making a difference?
 
Tom Hilliard
Ranch Hand
Posts: 39
Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You should not need to return the specific List implementation. In fact, doing so is a rather poor practice. DId that actual end up making a difference?



It makes no difference whether returning a List or ArrayList, I get the same result for either.

So what your saying is I don't need to return specifically an ArrayList, just a List?

EDIT: I have changed my methods to return a List and I still get exactly the same result, is this better practice? And why is it?

Regards

TH
 
Bear Bibeault
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Hilliard wrote:It makes no difference whether returning a List or ArrayList, I get the same result for either.


That's what I thought would happen.

So what your saying is I don't need to return specifically an ArrayList, just a List?


Absolutely. There is no need to reveal the List implementation being used. All that matters is that it is a List.

is this better practice? And why is it?


This is a general Java concept. Revealing the List implementation is a poor practice because it locks the implementation to an ArrayList rather than any List implementation.

As to the original issue, we need more info. What exactly is owneraccomms, and how is it being established as a scoped variable?

(Also, I advise to rethink your naming. "owneraccomms" not so good.)
 
Tom Hilliard
Ranch Hand
Posts: 39
Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

As to the original issue, we need more info. What exactly is owneraccomms, and how is it being established as a scoped variable?

(Also, I advise to rethink your naming. "owneraccomms" not so good.)



Sorry Bear, I obviously didn't make myself clear in saying I've solved my issue. I thought that a List was like an array (1 column). What I didnt realise is that I could call and element index. So I am now doing this


Which prints this output


Instead of

which I was getting before.

Interms of the Attribute name owneraccomms, what is wrong with that and can you suggest something better if you know it is a List of accommodations an owner owns, each accommodation in the List has a name, city, county and postcode. The List is set in the request using setAttribute()

Regards

TH
 
Bear Bibeault
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ownerAccomodations would be better.

Bear's rules for names:
  • Follow conventions. (In the case of owneraccoms, you failed to use camel case)
  • Words are good; no need to freeze-dry them.
  •  
    Sheriff
    Posts: 26948
    83
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Actually I wouldn't have returned any kind of List for that requirement. You need something with a name, a city, a county, and a postcode? Then create a Java bean class which has those properties, and just use the properties in your EL. Using a List (or an array) is what beginners do before they learn that Java is an object-oriented language.
     
    Bear Bibeault
    Sheriff
    Posts: 67618
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Paul is wise. Heed his words.
     
    Tom Hilliard
    Ranch Hand
    Posts: 39
    Netbeans IDE Postgres Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:Actually I wouldn't have returned any kind of List for that requirement. You need something with a name, a city, a county, and a postcode? Then create a Java bean class which has those properties, and just use the properties in your EL. Using a List (or an array) is what beginners do before they learn that Java is an object-oriented language.



    I need a list of those things mentioned, an owner can have may accommodations. Is that possible using beans? I've heard of them but don't know exactly what they do!

    TH
     
    Paul Clapham
    Sheriff
    Posts: 26948
    83
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Then here's a link to the Wikipedia article: JavaBeans. Skip over the section about the JavaBeans API (not relevant to web applications) and go to the section about conventions and the examples following.

    The EL is designed to understand JavaBeans which are written using those conventions. So if you had an Accomodation class with a getCounty() method, and you had a scoped variable named "accomodation" which referred to one of those objects, then you could get its county like this:
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic