To print list values from servlet to JSP using c:foreach of JSTL
10,743
Your logic is wrong. Change below parts of your servlet and jsp:
servlet:
List<String> urls = new ArrayList<String>();
for (int i = 0; i < webList.size(); i++) {
ursl.add(webList.get(i).getUrl());
}
request.setAttribute("webURL", urls);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/weblink.jsp");
rd.forward(request, response);
Jsp:
<c:forEach var="url" items="${webURL}" >
${url}
</c:forEach>
Author by
Sumit Sharma
Updated on June 04, 2022Comments
-
Sumit Sharma almost 2 years
This is the servlet from where I want to send the values which are present in the list:
public class SearchServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String searchKey=request.getParameter("searchKey"); String value = null; Document doc; String url = null; Map map = new HashMap(); int id = 0; int count; int mapKey=0; Conectiondb db = new Conectiondb(); Connection con = db.getconnection(); try { PreparedStatement ps = con .prepareStatement("select id, url from search "); ResultSet rs = ps.executeQuery(); WebLink webLink=null; while (rs.next()) { webLink=new WebLink(); mapKey++; id = rs.getInt("id"); url = rs.getString("url"); doc = Jsoup.connect(url).timeout(60*100000).get(); Elements searchText = doc .getElementsContainingOwnText(searchKey); count=0; for (Element link : searchText) { count++; } webLink.setCount(count); webLink.setUrl(url); map.put(mapKey, webLink); } Collection mp = map.values(); List list = new ArrayList(mp); Collections.sort(list,new WebLink()); List<WebLink> webList=new ArrayList<WebLink>(); for (Iterator it = list.iterator(); it.hasNext();) { WebLink link = (WebLink)it.next(); // System.out.println("weblink count : "+link.getCount()); // System.out.println("weblink url : "+link.getUrl()); webList.add(link); } for (int i = 0; i < webList.size(); i++) { if(i==0){ RequestDispatcher rd = getServletContext().getRequestDispatcher("/weblink.jsp"); rd.include(request, response); } request.setAttribute("web",webList.get(i).getUrl() ); // this is the result I want to post on my jsp with the help of c for each method of jstl } } catch (Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
This is the JSP code I am using to get the values, which was set in request attribute in the servlet. The problem is that I am not able to get the values. Either the servlet code where I have set the list values in request is wrong, or the code in the JSP
c:foreach
is wrong:<c:forEach var="a" items="${web}" > <c:out value="a.web"/> </c:forEach>
-
Sumit Sharma over 9 yearsI know that i was doing it wrong because i was directly adding the list values to the request. Your idea is good to add the result urls in a new list. Thanks a lot its working .