Search code examples
javajspservletsforeachmultimap

how to Iterate multimap values in jsp


How do I iterate or receive the value from a multimap? I have one key and that is associated with some array value. I want the first key, then all values, then second key other values, but I don't know how to iterate over multimap.

Here is my code:

Here is my servlet where I am calling my business logic class method

    public class Emp_Mapping extends HttpServlet
    {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    HttpSession session = request.getSession(true); 
    Employee emp1 = (Employee)session.getAttribute("emp1");
    session.setAttribute("emp1",emp1);    
    EmployeeBean eb = new EmployeeBean();   
    List<Employee> detail2 = eb.emp_map_detail1(emp1);//here i am calling
         System.out.println(detail2);       
        request.setAttribute("detail2",detail2);
        request.getRequestDispatcher("/EmployeeMapping.jsp").forward(request, response);
    }}

Here is my method which of Employee class which I have kept as business logic class where all request coming after servlet to specific method by method call. Here it's working fine and it is holding the required value as I want:

      public List<Employee> emp_map_detail1(Employee emp1) {
      Connection con = null;
      Statement stmt2,stmt3,stmt4 = null;  
      ResultSet rs2 =null;
      ResultSet rs3 =null;
      ResultSet rs4 =null;
      int e1=0, e2=0,e3=0,e4=0;
      String n2=null,n1=null,n4=null;
      List<Employee> detail2 = new ArrayList<Employee>();
      Multimap<String, String> multimap1 = ArrayListMultimap.create();
      Multimap<String, String> multimap2 = ArrayListMultimap.create();  

      List<String> myList2 = new ArrayList<String>();          
      try{
      con = ConnectionManager.getConnection();
      stmt2 = con.createStatement();
      String Query12 = "select empId, empName from empinfo where access_type='manager'";
      System.out.println("Query1 is" +Query12);
      rs2 = stmt2.executeQuery(Query12);
      while(rs2.next())
      {
      e1 =Integer.parseInt(rs2.getString("empId"));
      n1 = rs2.getString("empName");
      myList2.add(n1);        
      stmt3 = con.createStatement();
      String Query13 = "select empId, empName from empinfo where empId in 
      (select distinct   employee_teamleader from employeegroup 
      where employee_manager='"+e1+"')  and access_type ='teamleader' ";
      System.out.println("Query1 is" +Query13);
      rs3 = stmt3.executeQuery(Query13);
      while(rs3.next())
      {
      e3 =Integer.parseInt(rs3.getString("empId"));
      n2 = rs3.getString("empName");    
       multimap1.put(n1,n2);
          stmt4 = con.createStatement();
       String Query14 = "select empId, empName from empinfo where
       empId in (select distinct employee_name from employeegroup 
       where employee_teamleader='"+e3+"') and access_type ='employee'";
       System.out.println("Query1 is" +Query14);
       rs4 = stmt4.executeQuery(Query14);
       while(rs4.next())
      {
       e4 =Integer.parseInt(rs4.getString("empId"));
       n4 =rs4.getString("empName");
       multimap2.put(n2,n4);
       System.out.println("multimap1" +multimap1);
        System.out.println("multimap2" +multimap2);
       }}}
        emp1.setEmpname2(multimap2);
        emp1.setManname1(myList2);
        emp1.setTlname2(multimap1);
       detail2.add(emp1);
       emp1.setValid(true);
      }  
      catch (SQLException  ex) {

      } finally {
          try {
              if (stmt4 != null) {
                  stmt4.close();
              }
              if (con != null) {
                  con.close();
              }
          } catch (SQLException ex) {
              Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
    return detail2;
    }

Here is employee bean class in which I have all associated methods to employee. Here I have provided only the required setter and getter method with the variable:

      public class Employee 
      {
      private Multimap<String, String> tlname2;

    public Multimap<String, String> getTlname2() {
    return tlname2;
     }
    public void setTlname2(Multimap<String, String> tlname2) {
    this.tlname2 = tlname2;
     }
     }

Here in jsp how to print. I don't know it's not getting iterate in this way:

<c:forEach var="temp" items="${emp1.tlname2}">
 ${temp}
</c:forEach>

Solution

  • Try jsp below:

    <c:forEach var="myMap" items="${emp1.tlnameMap}">
      <c:out value=" key is ${myMap.key}" />
      <c:forEach var="mapValue" items="${myMap.value}" varStatus="count">
            <c:out value=" value ${count.index} is ${mapValue}" />
      </c:forEach>
    </c:forEach
    

    And create a new method which will return map view of multiMap as below:

    public Map<String, Collection<String>> getTlnameMap() {
        return tlname2.asMap();
    }