title: Display image in jsp using servlet
body: <p>I am working on an application where the information of users gets added and modified(updated). </p>
  
  <p>In <strong>add module</strong>, admin enters user details and unique-id(abc001) gets generated at "add" button. and admin also saves the image/picture(name : abc001) of the user in server location <strong>(//some-location-ip address/D drive/images).</strong></p>
  
  <p>In <strong>"update" module</strong>, admin can modify the user details, but can not modify id. </p>
  
  <p>I need some direction in couple of scenarios. </p>
  
  <p>If an Admin "updates" a particular user, the image of that user which is present in the server should gets displayed on the page as soon as the admin hit the update button. </p>
  
  <p>Image code in JSP : </p>
  
  <pre><code>&lt;img height="100px;" width="100px;" src="........." alt="Candidate Image"&gt;&lt;/img&gt;
  </code></pre>
  
  <p>I have written a servlet, but don't know how to call different images corresponding to different users and display the image on the profile page. </p>
  
  <p>user A profile will display user A image
  user B profile will display user B image
  and so on</p>
  
  <p>Servlet code Snippet</p>
  
  <pre><code>public class UpDatePhoto extends HttpServlet {
  
      public UpDatePhoto () {
          super();
          // TODO Auto-generated constructor stub
      }
  
     private static final long serialVersionUID = -8071854868821235857L;
     private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
     private String imagePath;
  
     *public void init() throws ServletException {
          this.imagePath = "D:\\photo_not_available_large.png";
      }*
  
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException
      {
          String requestedImage = request.getPathInfo();
          if (requestedImage == null) {
              response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
              return;
          }
  
          File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));
  
          String contentType = getServletContext().getMimeType(image.getName());
  
          if (contentType == null || !contentType.startsWith("image")) {
              response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
              return;
          }
  
          response.reset();
          response.setBufferSize(DEFAULT_BUFFER_SIZE);
          response.setContentType(contentType);
          response.setHeader("Content-Length", String.valueOf(image.length()));
          response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");
  
          BufferedInputStream input = null;
          BufferedOutputStream output = null;
  
          try {
  
              input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE);
              output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
  
              byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
              int length;
              while ((length = input.read(buffer)) &gt; 0) {
                  output.write(buffer, 0, length);
              }
          } finally {
  
              close(output);
              close(input);
          }
      }
  
  
      private static void close(Closeable resource) {
          if (resource != null) {
              try {
                  resource.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
  } 
  </code></pre>
  
  <p>The image is not http accessible but is only accessible as a file, the servlet would have to open the image file, read in the contents and place those in the response buffer" ....not sure if i am correct. </p>
  
  <p>Can somebody guide me to the direction and help me out as how to fetch the image from the server directory location and display the correct image for a user. </p>