简体   繁体   English

从Servlet无法在JSP中打印ArrayList

[英]Cant print ArrayList in JSP from Servlet

So I have this simple list made in a java servlet, and I would like to display it within a JSP page. 因此,我有一个用Java servlet制作的简单列表,我想在JSP页面中显示它。 Servlet code: Servlet代码:

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{


            ArrayList<String> myList = new ArrayList<String>();

            myList.add("cat");
            myList.add("dog");
            myList.add("frog");
            request.setAttribute("list", myList);
            String nextJSP = "/index.jsp";
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
            dispatcher.forward(request,response);
}

} }

However it won't print in the following JSP file: 但是,它不会在以下JSP文件中打印:

<%@page import="java.io.*" %>
<%@page import="java.net.*" %>
<%@page import="java.util.*" %>
<%@page import="java.util.List" %> 
<%@page import="java.util.ArrayList" %>
<%@page language="java" import="myPackage.*" %>

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

<% List<String> myList = (ArrayList<String>) 
 request.getAttribute("list"); %>
<% out.println(myList); %>


 </body>

 </html>

Any help would be appreciated! 任何帮助,将不胜感激!

The HTTP 500 error is come from this line: HTTP 500错误来自以下行:

<% List<String> myList = (ArrayList<String>) 
 request.getAttribute("list"); %>

It has extra () ,remove it and make it as below,then the HTTP 500 will diappear,instead you will got a warning Type safety:Unchecked cast from Object to ArrayList<String> ,but it doesn't matter 它具有多余的() ,将其删除,并使其如下所示,然后HTTP 500就会消失,相反,您会收到警告Type safety:Unchecked cast from Object to ArrayList<String>强制转换Type safety:Unchecked cast from Object to ArrayList<String> ,但这没关系

For how to print the ArrayList,if you have import JSTL tag in your jsp page,you can do it as below,previous answer has metioned: 对于如何打印ArrayList,如果您在jsp页面中导入了JSTL标记,则可以按照以下步骤进行操作,前面的答案已提及:

<c:forEach var="li" items="${list}">
 <c:out value="${li}"/> 
</c:forEach>

If you do not want to use JSTL , you can use java code to print it: 如果您不想使用JSTL ,则可以使用Java代码来打印它:

 <% for(int i=0;i<myList.size();i++){
   out.println(myList.get(i));
 } %>

Okay, your are having a HTTP 500 error message and I would suspect that it is coming from the following part of your code: 好的,您收到的是HTTP 500错误消息,我怀疑它来自代码的以下部分:

<% out.println(myList); %>

Keep in mind that the out used here is not System.out and does not behave the same way. 请记住,此处使用的输出不是System.out,并且行为方式也不相同。 Instead try (replace the line mentionned above by the following): 而是尝试(用以下内容替换上面提到的行):

<%  Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {    
       out.println(<iterator.next());
    }
%>

You could use core tag foreach as 您可以使用核心标记foreach作为

<c:forEach var="list" items="${YourList}">
 <c:out value="${list}"/> 
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM