Beginner
Beginner

Reputation: 51

Getting Hashmap in jsp

public class DBConnection {
public HashMap<Object, List<Dashboard>>  getStoreResult() {
    ArrayList<Dashboard> dashRec=new ArrayList<Dashboard>();
    try{
        Class.forName("");
        Connection con=DriverManager.getConnection("");
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("");
        HashMap<Object, List<Dashboard>> map = new HashMap<>();
        while (rs.next()) {
            }
         return map;

Servlet code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HashMap<Object, List<Dashboard>> map = new DBConnection().getStoreResult();
        request.setAttribute("map",map);
        RequestDispatcher rd=request.getRequestDispatcher("Dashboard.jsp");  
        rd.forward(request, response);

JSP Code:

%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import ="java.util.*"%>
<%@ page import="com.org.myprj.Dashboard" %>  
<% Map<Object, List<Dashboard>> map = (HashMap<>)request.getAttribute("map");%>

I have created Hashmap with key as object and value as Arraylist.Till servlet,it works fine.I want it in jsp page .How to get this Hashmap in jsp page?

Upvotes: 2

Views: 1443

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79620

Use JSTL library and iterate the map as follows:

<c:forEach var="vmap" items="${map}">
    Key is ${vmap.key} and  Value is ${vmap.value}<br>
</c:forEach>

If you can not use the JSTL library, you can access and iterate is as follows:

<% Map<Object, List<Dashboard>> map = (Map<Object, List<Dashboard>>)request.getAttribute("map");%>

and then iterate map as you do normally in Java.

Upvotes: 1

Related Questions