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

UseeMe
UseeMe used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Murali MurugesanFull stack Java developer

Commented:
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*

Author

Commented:
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.
Murali MurugesanFull stack Java developer

Commented:
few more questions for my clarification,

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

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
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.

Murali MurugesanFull stack Java developer

Commented:
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;
        }
%>

Author

Commented:
thank you Murali,
I will try to implement this.

Author

Commented:
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
Murali MurugesanFull stack Java developer

Commented:
you mean you couldn't iterate the resultMap as per my code above?
can u post wht u tried?

Author

Commented:
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>
<%
            }
           
        }
%>

Commented:
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

Author

Commented:
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");

Author

Commented:
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
Full stack Java developer
Commented:
below should be the code for iterating the result for getCategory.


<%HashMap objHashMap =getCategory("Blue");  

Iterator it = objHashMap .entrySet().iterator();
         while (it.hasNext()) {
              Map.Entry pairs = (Map.Entry)it.next();
              String title = pairs.getKey();
              String path = pairs.getValue();
   %>        
              <td><%=title%></td>
             <td><%=path%></td>
                                   
   <%    }%>


-Murali*
Commented:
I built on what you posted in your other question.  Please try the page below here.
<%
 String[][] docs =     {{"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/Green/docs/team"}};
String type = request.getParameter("type");
if(type == null || "".equals(type)) type = "Blue";  // or use whatever color you want for default
for (int i = 0; i < docs.length; i++) {
         if( type.equals(docs[i][0] ))       
                   out.print("<a href='" + docs[i][2] + "'>" + docs[i][1] + "</a><br/>");
}
%>
<html>
<body>
<form>
<input type="text" name="type"/>
<input type="submit" />
</form>
</body>
</html>

Open in new window

Author

Commented:
sorry I  accepted the wrong answer, will  contact an admin to fix

Commented:
I think you should split the points.

Author

Commented:
thanks for all your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial