Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6014521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:42:15+00:00 2026-05-23T02:42:15+00:00

My ImageDAO looks like this: public InputStream getPhotos(Long userid) throws IllegalArgumentException, SQLException, ClassNotFoundException {

  • 0

My ImageDAO looks like this:

public InputStream getPhotos(Long userid) throws 
  IllegalArgumentException, SQLException, ClassNotFoundException {

  Connection connection = null;
  PreparedStatement preparedStatement = null;
  ResultSet resultset = null;
  Database database = new Database();
  InputStream binaryStream = null;

  try {

    connection = database.openConnection();
    preparedStatement = connection.prepareStatement(SQL_GET_PHOTO);                  
    preparedStatement.setLong(1, userid);
    preparedStatement.executeUpdate();

    while(resultset.next()) {
      binaryStream = resultset.getBinaryStream(4);
    }

  } catch (SQLException e) {
      throw new SQLException(e);
  } finally {
      close(connection, preparedStatement, resultset);
  }
  return binaryStream;
}

My ImageServlet looks like this:

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

  // Getting user id from session
  HttpSession session = request.getSession(false);
  Long userid = (Long) session.getAttribute("user");    

  try {

      InputStream photoStream = imageDAO.getPhotos(userid);

      // Prepare streams.
      BufferedInputStream input = null;
      BufferedOutputStream output = null;

      try {

      // Open streams
      input = new BufferedInputStream(photoStream, DEFAULT_BUFFER_SIZE);
      output = new BufferedOutputStream(response.getOutputStream(),
                                                   DEFAULT_BUFFER_SIZE);

      // Write file contents to response.
      byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
      int length;
      while ((length = input.read(buffer)) > 0) {
          output.write(buffer, 0, length);
      }

      } finally {
          output.close();
          input.close();
      }

      //Redirect it to profile page
      RequestDispatcher rd = request.getRequestDispatcher
                            ("/webplugin/jsp/profile/photos.jsp");
      rd.forward(request, response);


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

}

How should my JSP image src look like

<img src="What to put here">

Disclosure:

The servlet code is copied from this link http://balusc.blogspot.com/2007/04/imageservlet.html

Questions:

  1. How to retreive image in JSP from ImageServlet. Someone in Stackoverflow said to put
    <img src="URL to Servlet" />. But I don’t what it means.
  2. Is the above method correct way to retreive image from database? Or is there better way.

EDIT: My Web.xml looks like this

<servlet>
  <servlet-name>Photo Module</servlet-name>
  <servlet-class>app.controllers.PhotoServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Photo Module</servlet-name>
  <url-pattern>/Photos</url-pattern>
</servlet-mapping>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:42:16+00:00Added an answer on May 23, 2026 at 2:42 am

    The src of the HTML <img> element should just point to an URL. An URL is a web address like as the one you enter in your browser address bar. Servlets can be mapped on certain URL patterns by web.xml so that when you invoke an URL which matches the servlet mapping, that the servlet will be invoked. See also our Servlets Wiki.

    You have mapped the servlet on an URL pattern of /Photos. Entering an URL like

    http://localhost:8080/YourContextPath/Photos

    in the browser address bar should display the image. So basically, assuming that the JSP runs in the same context path, this should do:

    <img src="Photos" />
    

    Or when you want to make it relative to the domain root, then you need to include the context path dynamically:

    <img src="${pageContext.request.contextPath}/Photos" />
    

    Said that, there are some problems in your servlet. You haven’t set the content type header. This way the browser won’t know what to do with the HTTP response. It’ll display a Save As popup when you enter its URL straight in address bar and it’ll display nothing when you call it in <img>. If it’s a JPG image, then add the following line before you call response.getOutputStream().

    response.setContentType("image/jpeg");
    

    This way the browser understands that it’s a JPG image and it’ll display it as such. See also the blog which you linked for the proper way of setting the headers.

    Another problem is that you’re calling request.getSession(false) which can potentially return null when there’s no means of a session. But you are not checking for that on the next line! So either use

    HttpSession session = request.getSession();
    

    so that it is never null, or add a

    if (session == null) {
        // Display some default image or return a 404 instead.
        return;
    }
    

    You would like to do the same for userId and photoStream. If it’s absent, then display a default image or return a 404.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.