Link to home
Start Free TrialLog in
Avatar of UseeMe
UseeMe

asked on

what is the best way to hold and retreive lists of data in a jsp

I have 3 lists of document titles and link pathes grouped by category. I need to be able to pass  a category parameter to a jsp file and have the list of documents displayed. My question is what is best way to hold and access these lists from within the jsp. I only have access to the jsp and can not add any tags or class files
here is an example of the data I have
category, document title, document path
Blue, "go blue" ,  "\main\blue\docs"
Blue, "how to" ,  "\main\blue\docs"
Blue, "find me" ,  "\main\blue\docs\team"

Red, "go Red" ,  "\main\Red\docs"
Red, "where to" ,  "\main\Red\docs\team3"
Red, "how its done" ,  "\main\Red\docs\team"

Green, "go Green" ,  "\main\Green\docs"
Green, "Why Me" ,  "\main\Green\docs\team2"
Green, "goin far" ,  "\main\Greendocs\team"

the faster, and easier to implement the better
Avatar of Murali Murugesan
Murali Murugesan
Flag of Australia image

i think u can have hashMap with document title (assuming its always unique) as key and category~document path as value. Use "~" to seperate the category and document path,

something like this,

 "go blue" ,  "Blue~\main\blue\docs"
 "how to" ,  "Blue~\main\blue\docs"
"find me" ,  "Blue~\main\blue\docs\team"

"go Red" ,  "Red~\main\Red\docs"
 "where to" ,  "Red~\main\Red\docs\team3"
"how its done" ,  "Red~\main\Red\docs\team"

"go Green" ,  "Green~\main\Green\docs"
"Why Me" ,  "Green~\main\Green\docs\team2"
 "goin far" ,  "Green~\main\Greendocs\team"


once u have the above format in a map iterate for the keys and for each key get the value of category  and document path by spliting using the "~" symbol.

-Murali*
Avatar of UseeMe
UseeMe

ASKER

Murali thank you for responding.
Let me clarify my request,  I  want to be able  to run a method like getCategory("Blue") and have that method return all the blue titles and coresponding paths.
few more questions for my clarification,

1. category,titles,path are hardcoded values?
2. In what u have stored these values at present?

Avatar of UseeMe

ASKER

1. yes they would all be hard coded values.
2. I do not have the values stored yet, I was going to store them as arrays but I could figure out a way to return the data based on category.

have a single map to hold all the values, and use the below method to get a map of titles and path.

<%
        Map map = new HashMap();

        map .put( "Blue~go blue" ,  "\main\blue\docs");
        map .put( "Blue~how to" ,  "\main\blue\docs");
        map .put("Blue~find me" ,  "\main\blue\docs\team");

        map .put( "Red~go Red" ,  "\main\Red\docs");
        map.put "Red~where to" ,  "\main\Red\docs\team3");
        map.put("Red~how its done" ,  "\main\Red\docs\team");

       map .put( "Green~go Green" ,  "\main\Green\docs");
       map.put("Green~Why Me" ,  "\main\Green\docs\team2");
       map.put( "Green~goin far" ,  "\main\Greendocs\team");
%>


<%!
         public Map getCategory(String color){
 
        Map resultMap= new HashMap();

          Iterator it = map .entrySet().iterator();
         while (it.hasNext()) {
              Map.Entry pairs = (Map.Entry)it.next();
              String[] key = pairs.getKey().split("~");
              if(key != null && key.length>1 && key[0].equals(color)){
                      resultMap.put(key[1],pairs.getValue());
              }                            
       }

        return resultMap;
        }
%>
Avatar of UseeMe

ASKER

thank you Murali,
I will try to implement this.
Avatar of UseeMe

ASKER

Hello I tried to implement this but I could not find a way to iterate through the hashmap in a way that would out put both title and path
you mean you couldn't iterate the resultMap as per my code above?
can u post wht u tried?
Avatar of UseeMe

ASKER

ok  please disregard my last post.  I was having difficulty retrieving key value. For a test I am going to try the following code now. I will let you know if it works

<%
HashMap objHashMap =getCategory("Blue");
for (Iterator it = objHashMap.keySet().iterator(); it.hasNext();) {
            String name = (String) it.next();
%>
            <td><%=name%></td>
           
<%
          for(String path : (String)objHashMap.get(name)) {
         
%>
                <td><%=path%></td>
<%
            }
           
        }
%>
I don't think you need a method or a map. Please try the following code.
<%
  String[][] blue = {{"go blue","/main/blue/docs"},
                          {"how to","/main/blue/docs"},
                          {"find me","/main/blue/docs/team"}};
  String[][] red = {{"go Red","/main/Red/docs"},
                         {"where to","/main/Red/docs/team3"},
                         {"how its done","/main/Red/docs/team"}};
  String[][] green = {{"go Green","/main/Green/docs"},
                           {"Why Me","/main/Green/docs/team2"},
                           {"goin far","/main/Green/docs/team"}};
 
for(int i = 0; i < blue.length; i++){
        out.print("<a href='" + blue[i][1] + "'>" + blue[i][0] + "</a><br/>");
}
%>

Open in new window

Avatar of UseeMe

ASKER

ok so with that code how would I display the correct string array if the parameter coming in is "red"
String Type = getParameter(request, "type");
Avatar of UseeMe

ASKER

to clarify, I have to change which array is displayed based on type parameter that is passed in the jsp.  what I am looking for is a way to do this with out a bunch of if statements
ASKER CERTIFIED SOLUTION
Avatar of Murali Murugesan
Murali Murugesan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UseeMe

ASKER

sorry I  accepted the wrong answer, will  contact an admin to fix
I think you should split the points.
Avatar of UseeMe

ASKER

thanks for all your help