title: Retrieving Inputstream image from servlet using jquery
body: <p>I want to retrieve an image from mysql/servlet out to jsp using jquery. I made servlet that retrieves object from mysql and parse it to json, and finally display object attributes using jquery.</p>
  
  <p>After run the jsp I got string attributes but unfortunately my problem with the image which is broken. I think I have problem of showing inputstream image or I miss something. help Please</p>
  
  <p>index.jsp</p>
  
  <pre><code>   &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;   
      &lt;/script&gt;
      &lt;script&gt;
      $(document).ready(function()
              {
  
          $.ajax
          ({
          type: "GET",
          url: "InfoServlet",
          dataType:"json",
          success: function(data)
          {
  
  
          $.each(data, function(i,data)
          {
              var msg_data= "&lt;div id='msgdiv"+data.Msg+"'&gt;"+data.IdInfo+" and "+data.Msg+"&lt;/div&gt;";
  
              $(msg_data).appendTo("#content");
              $('#thediv').prepend('&lt;img id="theImg" src='+data.Picture+' /&gt;');
  
  
          });
  
          }
          });
  
  
          return false;
          });
  
      &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  
  &lt;div id='content'&gt;&lt;/div&gt;
  &lt;div id='thediv'&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The servlet:</p>
  
  <pre><code>@WebServlet("/InfoServlet")
  public class InfoServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  
  public InfoServlet() 
  {
      super();
  
  }
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException
  {
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
  
      try
      {   
      Gson gson = new Gson();
  
      InfoService is = new InfoService();
      Info info = new Info();
  
      int idinfo = 2;
      info = is.queryById(idinfo);
      String gout= gson.toJson(info);
      System.out.println("{\"Messages\":"+gout+"}");
      out.println("{\"Messages\":"+gout+"}");
      //System.out.println(gout);
      }
      catch (Exception ex)
      {
          System.out.println("Error: "+ ex.getMessage());
      }
      finally
      {
          System.out.close();
      }
  }
  
  }
  </code></pre>
  
  <p>Info object:</p>
  
  <pre><code>package net.test.service;
  
  import java.io.InputStream;
  
  public class Info
  {
  private int IdInfo;
  private String Msg;
  private InputStream Picture;
  
  
  public int getIdInfo() {
      return IdInfo;
  }
  public void setIdInfo(int idInfo) {
      IdInfo = idInfo;
  }
  public InputStream getPicture() {
      return Picture;
  }
  public void setPicture(InputStream picture) {
      Picture = picture;
  }
  public String getMsg() {
      return Msg;
  }
  public void setMsg(String msg) {
      Msg = msg;
  }
  
  }
  </code></pre>