-1

I am very new to java web applications. I am working on project from where I have to receive a specific set of images from mysql and display in jsp.

Below are my servlet code (Where I am setting my attribute, toppings contains a path of all images which are stored in my computer) and when I tried to retrive them on .jsp page it gives me an error as below:

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

Please help here as it is very important .. please ..

user3022426
  • 49
  • 2
  • 8

2 Answers2

0

I have couple of queries here : 1. I assume you need to send response with an attachment ( i.e; image ). But the code says you are storing string :

String[] toppings = new String[size];

                    while(rs.next())
                    {  
                        toppings[i] = rs.getString(5); 
                        i++;
                    }
  1. Could you please add a debug pointer to toppings[] to check its content. I believe it may contain a negative value.
Ravi Sinha
  • 39
  • 1
  • 9
  • hello sir, i am getting the path of images from the database not the image itself. – user3022426 Apr 27 '16 at 00:21
  • Could you please check which value is coming as null in CustomerBrowseScreen_jsp.java at line number : 193 – Ravi Sinha Apr 27 '16 at 00:23
  • is there a way to somehow send this path to an image tag on a jsp page from servlet.. i have multiple paths in toppings. – user3022426 Apr 27 '16 at 00:23
  • [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 When I applied a debug pointer in servlet I am getting exact values what I am looking for .I think something is worng when string of array is transferred from servlet to jsp don't know what. – user3022426 Apr 27 '16 at 00:44
0

You're using

request.getAttribute("images")

in the JSP and

request.getSession().setAttribute("images", toppings);

in the Servlet. Change the JSP to be

session.getAttribute("images")

or

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

This is a very easy thing to get confused about.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • Hi, I made two changes as below: <% if (display != null && display.length != 0) {for(int i=0; i and request.setAttribute("images", toppings); now error ois gone. But i am getting these values in jsp instead of stringsinstead of what i am looking for All Just Arrived Must Listen Artist Photos Store Timings Map [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 – user3022426 Apr 27 '16 at 00:37
  • [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 [Ljava.lang.String;@76c12282 When I applied a debug pointer in servlet I am getting exact values what I am looking for .I think something is worng when string of array is transferred from servlet to jsp don't know what. – user3022426 Apr 27 '16 at 00:43
  • Perhaps in the JSP you want to iterate instead of printing the array every time - i.e. display[i]. – stdunbar Apr 27 '16 at 00:46
  • ohhh I am very stupid .. thanks a ton Sir. – user3022426 Apr 27 '16 at 00:47
  • Next question is: How can we use in scriplet. I tried below but giving an error: if (display != null && display.length != 0) { for(int i=0; i } } – user3022426 Apr 27 '16 at 00:49
  • Why don't you open up a new question - don't overload this one with that. We'll need to know what is in the array of Strings. – stdunbar Apr 27 '16 at 00:50