//Java
List<String> cityList = new ArrayList<String>();
cityList.add("Washington DC");
cityList.add("Delhi");
cityList.add("Berlin");
cityList.add("Paris");
cityList.add("Rome");
request.setAttribute("cityList", cityList);
//JSP
<c:forEach var="city" items="cityList">
<b> ${city} </b>
</c:forEach>
Code language: Java (java)
But what if you want to iterate a Hashmap? Well that too is piece of cake. Here is the example: //Java
Map<String, String> countryCapitalList = new HashMap<String, String>();
countryCapitalList.put("United States", "Washington DC");
countryCapitalList.put("India", "Delhi");
countryCapitalList.put("Germany", "Berlin");
countryCapitalList.put("France", "Paris");
countryCapitalList.put("Italy", "Rome");
request.setAttribute("capitalList", countryCapitalList);
//JSP
<c:forEach var="country" items="${capitalList}">
Country: ${country.key} - Capital: ${country.value}
</c:forEach>
Code language: Java (java)
Happy iterating :-) Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
OOH that's great !!! surely this code will help me a lot ... thanks
What if you use in the jsp file? How will the code be altered then and how does that work.....?
What I meant was this:
What if you use jsp:useBean in the jsp file? How will the code be altered then and how does that work…..?
in first foreach loop u missed to items="${cityList}" k
I attempted using your hashmap example. In the jsp
//JSP
Country: ${entry.key} - Capital: ${entry.value}
However this does not work.
@Gura : add in jsp and try if it works
sorry i missed code ---- add
[code language="java"]
<%@ page isELIgnored="false" %>
<%@ page isELIgnored="false" %>
[/code]
Thanks
Thanks man..your blogs helped me a lot..thank you once again..
Thank you for this info! But you can actually get access to the value by just using the key, like if it was an array:
[code language="java"]
public class StmtController extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RequestDispatcher rd;
HttpSession s=req.getSession();
CustDTO cust=(CustDTO) s.getAttribute("cust");
int accn=cust.getAccno();
try {
ArrayList a=new ArrayList();
DesDAO d=new DesDAO();
a=d.getStmt(accn);
req.setAttribute("list", a);
rd=req.getRequestDispatcher("stmt.jsp");
rd.forward(req, resp);
} catch (SQLException e) {
resp.sendRedirect("error.html");
}
}
}
This is my servlet.
package com.pannagaOrg.mvcBankApp.dto;
public class DesDTO {
private String descr;
private String dt;
private int accno;
public int getAccno() {
return accno;
}
public void setAccno(int accno) {
this.accno = accno;
}
public String getDescr() {
return descr;
}
public void setDescr(String descr) {
this.descr = descr;
}
public String getDt() {
return dt;
}
public void setDt(String dt) {
this.dt = dt;
}
}
This is my DTO
package com.pannagaOrg.mvcBankApp.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.pannagaOrg.mvcBankApp.dto.CustDTO;
import com.pannagaOrg.mvcBankApp.dto.DesDTO;
import com.pannagaOrg.mvcBankApp.utility.DBSingleton;
public class DesDAO {
public ArrayList getStmt(int accn) throws SQLException
{
ArrayList desList=new ArrayList();
String query="SELECT * FROM bank.trans WHERE accno=?";
DBSingleton conObj=DBSingleton.getConnectionObject();
Connection con=conObj.getCon();
PreparedStatement pstmt=con.prepareStatement(query);
pstmt.setInt(1,accn);
ResultSet res = pstmt.executeQuery();
while(res.next())
{
String des=res.getString(3);
String dt = res.getString(4);
DesDTO d=new DesDTO();
d.setDescr(des);
d.setDt(dt);
desList.add(d);
}
return desList;
}
}
[/code]
This is my DAO
Insert title here
click here to see statement:
accn
description
date
Here is my jsp
But i am getting an exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /stmt.jsp at line 19
16:
17:
18: accn
19:
20:
21:
22:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
Please help me
Hi, it seems that you are using jetty, I propose you to switch to tomcat :) or move scriplet code in controller / servlet
Thanks...