在JSP中迭代对象的arraylist时出现HTTP 500错误

问题描述 投票:0回答:2

我收到此错误:HTTP状态500 - 当我尝试迭代对象的arraylist(由我自己定义的类)时,处理JSP页面时发生异常。

这是jsp文件中的代码

 <%
    class postClass{
        String username; //with username and postid, can go to edit to query for that exact post in database
        Integer postid;
        String title;
        Date modified;
        Date created;
        }
    %>
    <%
       ArrayList<postClass> list = (ArrayList<postClass>) request.getAttribute("list"); 

        for(postClass x : list) {
            System.out.println(x);
        }
    %> 

这是错误信息

HTTP Status 500 - Unable to compile class for JSP:
type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 28 in the jsp file: /list.jsp
Syntax error on tokens, delete these tokens
25:     <%
26:        ArrayList<postClass> list = (ArrayList<postClass>) request.getAttribute("list"); 
27: 
28:        <% for(final postClass x : list) {
29:             out.println(x);
30:         } %>
31:    

编辑:更新的代码,以回答如下。仍然会产生错误

更新错误:

HTTP Status 500 - Unable to compile class for JSP:
type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [15] in the generated java file: [/var/lib/tomcat8/work/Catalina/localhost/editor/org/apache/jsp/list_jsp.java]
Syntax error on tokens, delete these tokens

Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:199)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:564)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:401)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:345)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Editor.doGet(Editor.java:238)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.14 (Debian) logs.
class jsp tomcat servlets arraylist
2个回答
0
投票

您已嵌套'<%'和'%>'。这在jsp中是不允许的。行上的标记语法错误:28表示第28行的'<%'标记。

应该是正确的代码

<%
class postClass{
    String username; //with username and postid, can go to edit to query for that exact post in database
    Integer postid;
    String title;
    Date modified;
    Date created;
    }
%>
<%
   ArrayList<postClass> list = (ArrayList<postClass>) request.getAttribute("list"); 
   for(final postClass x : list) {
        out.println(x);
    }
%> 

注意:如果为request.getAttribute("list")返回null,则此代码将抛出空指针异常。最好先进行空检查。

建议:java中的类名遵循以大写字母开头的约定。 postClass应更名为PostClass


0
投票

您的java代码中嵌套了<% ... ℅>。正如错误所示,删除这些令牌。

© www.soinside.com 2019 - 2024. All rights reserved.