从servlet在jsp中显示图像

时间:2016-04-26 23:51:30

标签: java jsp servlets scriplets

我是java web应用程序的新手。我正在开发项目,我必须从mysql接收一组特定的图像并在jsp中显示。

下面是我的servlet代码(我设置我的属性,toppings包含存储在我的计算机中的所有图像的路径),当我尝试在.jsp页面上检索它们时,它给出了如下错误:< / p>

  HTTP Status 500 - An exception occurred processing JSP page /CustomerBrowseScreen.jsp at line 115

    type Exception report

    message An exception occurred processing JSP page /CustomerBrowseScreen.jsp at line 115

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

    exception

    org.apache.jasper.JasperException: An exception occurred processing JSP page /CustomerBrowseScreen.jsp at line 115

    112:             <%
    113:                 String[] display = (String[])request.getAttribute("images");
    114:                 
    115:                for(int i=0; i<display.length; i++)
    116:                {
    117:                    out.println(display);
    118:                }


    Stacktrace:
        org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:575)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    root cause

    java.lang.NullPointerException
        org.apache.jsp.CustomerBrowseScreen_jsp._jspService(CustomerBrowseScreen_jsp.java:193)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        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/7.0.69 logs. 

     - .jsp Code
<%
                String[] display = (String[])request.getAttribute("images");

               for(int i=0; i<display.length; i++)
               {
                   out.println(display);
               }
%>

 - Servlet

                    String[] toppings = new String[size];

                    while(rs.next())
                    {  
                        toppings[i] = rs.getString(5); 
                        i++;
                    }

                    request.getSession().setAttribute("images", toppings);
                     request.getRequestDispatcher("CustomerBrowseScreen.jsp").forward(request, response);

请帮助,因为它非常重要..请..

2 个答案:

答案 0 :(得分:0)

我在这里有几个问题: 我假设您需要发送带附件的回复(即图像)。但是代码说你正在存储字符串:

String[] toppings = new String[size];

                    while(rs.next())
                    {  
                        toppings[i] = rs.getString(5); 
                        i++;
                    }
  1. 你能不能给toppings []添加一个调试指针来检查它的内容。我认为它可能包含负值。

答案 1 :(得分:0)

您正在使用

request.getAttribute("images")
JSP中的

request.getSession().setAttribute("images", toppings);
在Servlet中

。将JSP更改为

session.getAttribute("images")

request.getSession().getAttribute("images")

这很容易让人感到困惑。