使用JSTL的c:foreach将列表值从servlet打印到JSP [英] To print list values from servlet to JSP using c:foreach of JSTL

查看:399
本文介绍了使用JSTL的c:foreach将列表值从servlet打印到JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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>

推荐答案

您的逻辑是错误的.更改servlet和jsp的以下部分:

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>

这篇关于使用JSTL的c:foreach将列表值从servlet打印到JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆