• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Best practice for comparing values in a JSP

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have to check if a specific attribute(variable) of the bean class to some constants in a JSP file. So i am using mulitple if and else-if conditions blocks to display a label accordingly. Instead of having multiple if-else blocks, i am looking for other options to perform this operation effectively in JSP. Below is one approach which i have found so far,

1) Create long array constant with all the values.


2) In JSP, convert this array to an ArrayList using Arrays.asList(Test.SPEC_TYPES)


3) In JSP, get the attribute from the java bean and check if ArrayList contains it.


Please suggest if the above approach if good or bad. Also please clarify if i can proceed with the if-else conditional block method or any other good practice. Thanks.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
before reaching jsp do all this condition check and set a flag to request scope in *servlet* , and in jsp display the content depends on request flag using JSTL if tag (or appropriate tags)
 
Balaji Krishnan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What if i have to do this check for more than one object, then in that case few may have the type available in the array and few may not have it. In such case a single flag can't help.

Thanks!
 
Sheriff
Posts: 67752
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
Use the Map, Luke!
 
Balaji Krishnan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestion Bear. Does map is a costly operation for this logic?
 
Sheriff
Posts: 28323
95
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
Searching a Map via its key is an O(1) operation. Searching a List is O(N) if you use the naive search method and O(log N) if you use binary search.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you worrying about performance at this stage and at this level? Please look up premature optimization. Choose what makes the most sense, not what you think might be most performant.

Optimization can be performed later, if and only if, a performance problem becomes evident.
 
Balaji Krishnan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul and Bear for the clarification. I will go with Map.
 
Don't touch me. And dont' touch this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic