gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:14:03 24 4
gpt4 key购买 nike

这是我要发送列表中存在的值的 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 的请求属性中设置的。问题是我无法获得这些值。要么我在请求中设置列表值的 servlet 代码错误,要么 JSP c:foreach 中的代码错误:

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

最佳答案

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

服务小程序:

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>

关于jsp - 使用 c :foreach of JSTL 将列表值从 servlet 打印到 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27586676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com