• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best practice for comparing values in a JSP

 
Balaji Krishnan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the Map, Luke!
 
Balaji Krishnan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion Bear. Does map is a costly operation for this logic?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Paul and Bear for the clarification. I will go with Map.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic