aspose file tools*
The moose likes JSP and the fly likes Best practice for comparing values in a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Best practice for comparing values in a JSP" Watch "Best practice for comparing values in a JSP" New topic
Author

Best practice for comparing values in a JSP

Balaji Krishnan
Greenhorn

Joined: Dec 14, 2011
Posts: 13
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Dec 14, 2011
Posts: 13
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

Joined: Jan 10, 2002
Posts: 60049
    
  65

Use the Map, Luke!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Balaji Krishnan
Greenhorn

Joined: Dec 14, 2011
Posts: 13
Thanks for the suggestion Bear. Does map is a costly operation for this logic?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18120
    
    8

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

Joined: Jan 10, 2002
Posts: 60049
    
  65

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

Joined: Dec 14, 2011
Posts: 13
Thanks Paul and Bear for the clarification. I will go with Map.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best practice for comparing values in a JSP
 
Similar Threads
Which Design Pattern and how to design using OOP this scenaria
JAXB attribute change
Sending a jsp page as the content of an email
How to render different pages from the SelectOneMenu
javafax, button action..NEED HELP PLEASE!!!