title: image file not loading in JSP from servlet
body: <p>I have a java program that generates a valid BufferedImage bi based on parameters that are sent to the program.  I want to call this program from a servlet, and have the servlet send the user a jsp containing an image that fits the parameters that the user inputs.  The code that I wrote below generates a null message instead of the requested image.  </p>
  
  <p>I should add that get_bi() sometimes takes a few hundred milliseconds to produce the BufferedImage on my development computer.  Not sure if part of the problem is a time delay thing.  </p>
  
  <p>Can anyone show me how to alter the code below so that it outputs an actual image in the user's web browser instead of a null message?  </p>
  
  <p>Here is the code for ImageServlet:  </p>
  
  <pre><code>import java.awt.image.BufferedImage;
  import java.io.File;
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Map;
  import javax.imageio.ImageIO;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletConfig;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  //import org.apache.log4j.Logger;
  import myownpackage.GetBI;
  
  public class ImageServlet extends HttpServlet{
  //   private Logger logger = Logger.getLogger(this.getClass());
     private RequestDispatcher jsp;
  
     public void init(ServletConfig config) throws ServletException {
        ServletContext context = config.getServletContext();
        jsp = context.getRequestDispatcher("/WEB-INF/jsp/send-image-into-html.jsp");
     }
  
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
         throws ServletException, IOException {
      //      logger.debug("doGet()");
            String imageParams = req.getParameter("imageParams");
            jsp.forward(req, resp);
     }
  
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
         throws ServletException, IOException{  
         Map&lt;String, String&gt; errors = validate(req);
         if (!errors.isEmpty()){
            //         logger.debug("validation errors");
            jsp.forward(req, resp);
         return;
         }
  
        resp.setContentType("image/gif");
        BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
        GetBI fetchBI = new GetBI();
        bi = fetchBI.get_bi(req.getParameter("imageParams"));
        File imgFile = new File("imgFile");
        ImageIO.write(bi, "gif", imgFile);
        resp.sendRedirect("send-image-into-html");
     }  
  
     public static Map&lt;String, String&gt; validate(HttpServletRequest req){
     HashMap&lt;String, String&gt; errors = new HashMap&lt;String, String&gt;();
     req.setAttribute("errors", errors);
     String imageParams = req.getParameter("imageParams");
     if (imageParams == null || imageParams.trim().length() == 0){
        errors.put("imageParams", "imageParams required.");
     }
     return errors;
   }
  }
  </code></pre>
  
  <p>The code for send-image-into-html.jsp is:  </p>
  
  <pre><code>&lt;jsp:useBean id="errors" scope="request" type="java.util.Map" class="java.util.HashMap" /&gt;
  &lt;%@ include file="top.inc" %&gt;
  &lt;%@ include file="middle.inc" %&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td width=350&gt;
              &lt;%=request.getParameter("imgFile")%&gt;
              &lt;img src="&lt;%=request.getParameter("imgFile")%&gt;"&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;form method="post"&gt;
                  &lt;table&gt;
                      &lt;tr&gt;
                          &lt;td&gt;Image Parameters: &lt;/td&gt;
                          &lt;td&gt;&lt;input type="text" name="imageParams" value="some parameters" size="50" /&gt;  
                              &lt;% if (errors.containsKey("imageParams")) {
                              out.println("&lt;span class=\"error\"&gt;" + errors.get("imageParams") + "&lt;/span&gt;");
                              }%&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;tr&gt;
                          &lt;td&gt;
                              &lt;input type="submit" name="submit-button" value="Click Here" /&gt;
                          &lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/form&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;%@ include file="bottom.inc" %&gt;
  </code></pre>
  
  <p><strong><em>EDIT:</em></strong>  </p>
  
  <hr>
  
  <p>I have edited my code based on what I understand of Joop's suggestions, but it still does not work.  Can you show me how to further edit this so that it works?  </p>
  
  <p>Here is the code for FirstServlet:  </p>
  
  <pre><code>public class FirstServlet extends HttpServlet{
      //   private Logger logger = Logger.getLogger(this.getClass());
      private RequestDispatcher jsp;
  
  public void init(ServletConfig config) throws ServletException {
      ServletContext context = config.getServletContext();
      jsp = context.getRequestDispatcher("/WEB-INF/jsp/send-image-into-html.jsp");
  }
  
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
  throws ServletException, IOException {//      logger.debug("doGet()");
      jsp.forward(req, resp);
  }
  
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
  throws ServletException, IOException{
      Map&lt;String, String&gt; errors = validate(req);
      if (!errors.isEmpty()){//         logger.debug("validation errors");
          jsp.forward(req, resp);
          return;
      }
      resp.sendRedirect("/send-image-into-html");
  }
  
  public static Map&lt;String, String&gt; validate(HttpServletRequest req){
      HashMap&lt;String, String&gt; errors = new HashMap&lt;String, String&gt;();
      req.setAttribute("errors", errors);
      String imageParam1 = req.getParameter("imageParam1");
      if (imageParam1 == null || imageParam1.trim().length() == 0){  
          errors.put("imageParam1", "imageParam1 required.");
      }
      return errors;
  }
  }  
  </code></pre>
  
  <p>Here is the code for SecondServlet:  </p>
  
  <pre><code>public class GetGraphServlet extends HttpServlet{
      //   private Logger logger = Logger.getLogger(this.getClass());
      private RequestDispatcher jsp;
  
  public void init(ServletConfig config) throws ServletException {
      ServletContext context = config.getServletContext();
      jsp = context.getRequestDispatcher("/WEB-INF/jsp/send-image-into-html.jsp");
  }
  
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
  throws ServletException, IOException {//      logger.debug("doGet()");
      // Create the BufferedImage, using any parameters and
      // possibly the exact request URI:
      String requestURI = req.getRequestURI();
      String imageParam1 = req.getParameter("imageParam1");
      resp.setContentType("image/gif");//256 colors
      BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
      GetBI fetchBI = new GetBI();
      bi = fetchBI.get_bi(req.getParameter(imageParam1));
      ImageIO.write(bi,"gif",resp.getOutputStream());
      resp.setContentType("image/gif"); // 256 colors.
      ImageIO.write(bi, "gif", resp.getOutputStream());
      jsp.forward(req, resp);
  }
  
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
  throws ServletException, IOException{}
  }
  </code></pre>
  
  <p>Here is the code for send-image-into-html.jsp   </p>
  
  <pre><code>&lt;jsp:useBean id="errors" scope="request" type="java.util.Map" class="java.util.HashMap" /&gt;
  &lt;%@ include file="top.inc" %&gt;
  &lt;%@ include file="middle.inc" %&gt;
      &lt;form method="post"&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td width=350&gt;
                      &lt;img src="&lt;%="/get-graph.gif?imageParam1="%&gt;"&gt;
                  &lt;/td&gt;
                  &lt;td&gt;
                      &lt;table&gt;
                          &lt;tr&gt;
                              &lt;td&gt;imageParam1: &lt;/td&gt;
                              &lt;td&gt;&lt;input type="text" name="imageParam1" value="samplevalue" size="50" /&gt;
                              &lt;%if (errors.containsKey("imageParam1")) {
                                  out.println("&lt;span class=\"error\"&gt;" + errors.get("imageParam1") + "&lt;/span&gt;");}%&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;input type="submit" name="submit-button" value="CreateImage"/&gt;
                              &lt;/td&gt;
                          &lt;/tr&gt;
                      &lt;/table&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
  &lt;%@ include file="bottom.inc" %&gt;  
  </code></pre>
  
  <p>Here are the relevant tags from web.xml:    </p>
  
  <pre><code>    &lt;servlet&gt;
          &lt;servlet-name&gt;FirstServlet&lt;/servlet-name&gt;
              &lt;servlet-class&gt;mypackage.FirstServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;FirstServlet&lt;/servlet-name&gt;
              &lt;url-pattern&gt;/html-page-url&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;SecondServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;mypackage.SecondServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;SecondServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/get-graph&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  </code></pre>