0

I had uploaded an image in my postgresql database, the uploading is successful. Now I want to view this image in JSP, I was able to get it into the database but I don't know how can I pass it down into the JSP in order for it to view the image.

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

        Connection connect;
        String userName = null;
        byte[] image = null;
        String imageName = null;
        try {
            connect = DriverManager.getConnection(databaseUrl, databaseUsername, databasePassword);
            PreparedStatement select = connect.prepareStatement("SELECT username,image,image_name FROM users WHERE username = ?;");
            select.setString(1, "steve");
            ResultSet selectSet = select.executeQuery();

            while(selectSet.next()) {
                userName = selectSet.getString(1);
                image = selectSet.getBytes(2);
                imageName = selectSet.getString(3);
            }

            request.setAttribute("username", userName);
            response.setContentType(getServletContext().getMimeType(imageName));
            response.setContentLength(image.length);
            response.getOutputStream().write(image);


        } catch (SQLException e) {
            e.printStackTrace();
        } 


    }

I tried using a requestDispatcher to pass down the image into the JSP

request.getRequestDispatcher("viewImage.jsp").include(request, response);

but I encountered a IllegalStateException(getOutputStream() is already been called).

This is my JSP code

<c:out value='${requestScope.username }' />
    <img src="${pageContext.request.contextPath}">
  1. Are there other ways to call the JSP from the servlet to pass the image?
  2. By setting the content type to a image mime type, would that mean that the 'username' won't be displayed because of the content type? how do I fix it?

I had been looking for this post since morning, and I'm lost on how it was able to pass down the image into the JSP How to retrieve and display images from a database in a JSP page?

EDIT: The purpose of the application is to view a user's profile (image and text information)

Community
  • 1
  • 1
zbryan
  • 805
  • 2
  • 12
  • 24
  • As explained in duplicate, `` URL has to invoke the servlet. Yours doesn't. For some unclear reason you've removed the servlet URL and image's filename/idenfitier from your ``. I strongly recommend to **exactly** take over the kickoff example and then continue with it. – BalusC May 23 '16 at 10:04
  • should my be – zbryan May 23 '16 at 10:27
  • Follow the example in the duplicate and read the text around the code snippets too. – BalusC May 23 '16 at 10:43
  • By carefully following your example, I was able to view the image. Since the would call the servlet does it mean that I would have a separate servlet for Images and Text informations? I tried this approach and it was working. Text Servlet -> view jsp -> Image Servlet = JSP has the text information and image. I was wondering if this is the standard. – zbryan May 24 '16 at 02:56
  • It's all explained in the introduction of the answer. – BalusC May 24 '16 at 06:33

2 Answers2

1

You don't need to pass down the image to the JSP.

  1. First the browser calls the URL of your JSP page and renders the img element.
  2. Then the browser calls the server again using the URL of the src attribute of the image.
  3. This invokes your servlet (right?) and the servlet just needs to stream the image bytes back to the browser, as you already do.
wero
  • 32,544
  • 3
  • 59
  • 84
  • I don't get it, can you please tell me the flow for my application? this is what I'm trying to accomplish JSP(upload file) -> Servlet (get image from DB) -> JSP(view image). Because in step 1, you have said that the img element would be rendered, but in my application, there are no img element on my 1st jsp(upload file). If I call directly with my 2nd jsp(view image) there would be nothing to view since I haven't retrieve the image from the DB – zbryan May 23 '16 at 09:54
0

You can either:

Stream image as of MIME-TYPE of, let's say, "image/gif" from your servlet and specify servlet path as "src" attribute of "img" tag like

<img src="/servlets/imgdata.do?id=1111">

or save image data to file within your context root and pass relative path to JSP

UPD: just redirect like:

    request.getSession().setAttribute("imgsrc", "myservlet.do?id=" + imgId);
    request.getRequestDispatcher("my.jsp").forward(request, response);

you may also use

request.setAttribute(name, val);

for request (not whole session) scope.

S. Kadakov
  • 861
  • 1
  • 6
  • 15
  • I'm sorry but I got confused with your answer and it lead me to my first question, "How can the servlet call the JSP to pass the image?". I don't get the flow – zbryan May 23 '16 at 09:52