使用c:foreach of JSTL将列表值从servlet打印到JSP

时间:2014-12-21 04:14:56

标签: jsp servlets jstl

这是我想要发送列表中存在的值的servlet:

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();
        }
    }
}
}

这是我用来获取值的JSP代码,它是在servlet的request属性中设置的。 问题是我无法获得价值。我在请求中设置列表值的servlet代码是错误的,或者JSP c:foreach中的代码是错误的:

<c:forEach var="a" items="${web}" >
<c:out value="a.web"/>
</c:forEach>

1 个答案:

答案 0 :(得分:2)

你的逻辑错了。更改servlet和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>