2
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?

Beginner
  • 51
  • 6

1 Answers1

1

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Ya but how can we get the map?Like suppose arraylist we get by <% ArrayList arr=(ArrayList)request.getAttribute("arr");%> .So similarly for map hows that possible – Beginner May 19 '20 at 09:29
  • I am facing this error:Multiple annotations found at this line: - Line breakpoint:Dashboard.jsp [line: 6] - Syntax error on token "<", ? expected after – Beginner May 19 '20 at 09:39
  • You need to import the `java.util` classes i.e. have you put `<%@ page import="java.util.*" %>` at the top. – Arvind Kumar Avinash May 19 '20 at 09:46
  • I have done that.Still error persists.This error remains:Syntax error on token "<", ? expected after this token – Beginner May 19 '20 at 09:49
  • It seems you have done some typo. Post the code of your JSP and I'll look into it. – Arvind Kumar Avinash May 19 '20 at 09:50
  • By the way, you haven't copied my code, `<% Map> map = (Map>)request.getAttribute("map");%>` correctly. Check it again. Let me check if there are any other problems. – Arvind Kumar Avinash May 19 '20 at 09:54
  • issue resolved .Thanks.One doubt , JSTL library and normal use of java in jsp which is better and why to go after one? – Beginner May 19 '20 at 09:55
  • Also when I return map; in java it gives me error:map cannot be resolved to a variable.So when I use return new HashMap<>(); it gices me no error.So is the output in both case remains same? – Beginner May 19 '20 at 09:58
  • 1
    Cool...you should strive to use JSTL instead of normal Java code in JSP because when you use Java code inside JSP, the JSP code looks ugly, hard to read and work with and error-prone. Check https://stackoverflow.com/questions/4535423/jstl-vs-jsp-scriptlets for more discussions. – Arvind Kumar Avinash May 19 '20 at 09:59
  • The error you are getting in your Java code is because you have put `HashMap> map = new HashMap<>();` inside the `try...catch` block and therefore its scope is limited to the `try...catch` block. Shift this line to the top inside the method(i.e. just above or below the line, `ArrayList dashRec=new ArrayList();`) and the error will go away. – Arvind Kumar Avinash May 19 '20 at 10:03