This is my function in the bean class-- product.java Im selecting the certain details from database and putting the values from database in a hashmap. HashMap is a type of Class Products.
public HashMap<String,Products> showProducts()
{
HttpServletRequest request = null;
PreparedStatement preparedStatement;
HashMap<String,Products>productMap=new HashMap<String,Products>();
try
{
preparedStatement = con.prepareStatement("select * from productdetails where producttype='toy'");
ResultSet resultSet=preparedStatement.executeQuery();
Products toy=new Products();
while(resultSet.next()){
toy.setProductId(resultSet.getInt(1));
toy.setProductName(resultSet.getString(2));
toy.setProductPrice(resultSet.getInt(3));
productMap.put("toy",toy);
request.setAttribute("productSessionMap",productMap);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return productMap;
}
This is the jsp page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome!!!! <c:out value="${sessionScope.loginBean.userName}"></c:out>
<jsp:useBean id="loginBean" class="com.training.entity.ShoppingCart"
scope="session"></jsp:useBean>
<jsp:setProperty property="*" name="loginBean"/>
<c:set var="status" value="${loginBean.showProducts()}"></c:set>
<c:set var="keys" value="${status.keySet()}"></c:set>
<c:out value="${status.toString()}"></c:out> <!-- This line displays last value of hashmap-!>
<c:forEach var="type" items="${productSessionMap}">
<c:out value="${type[keys]}"></c:out>
</c:forEach>
</body>
</html>
I want to iterate and display each value and key in the hashmap in jsp withous using scriptlet tags. Please help me with this.
Even i tried using scriplet tag.. But i am only getting the last value in the hashmap when iterating..
<%
ShoppingCart ob=new ShoppingCart();
HashMap<Integer,Products>newproductMap=new HashMap<Integer,Products>();
newproductMap=ob.showProducts();
Set<Integer>set = newproductMap.keySet();
for(Integer ent:set){
String name=newproductMap.get(ent).getProductName().toString();%>
<%=name%>
<%-- <%String value = ent.getValue().toString();%>
<%=value%>
--%>
<%}%>