title: Dispaly image through servlet on jsp
body: <p>From the Servlet I am generating JPEG image and writing in the outputstream of that servlet.By jsp i am calling this Servlet URL and displaying the image as similar to user profile with photo.
  Here the problem is,When first time login it will generate the image dynamically and display but next time if I login with out closing the browser first it will display the privies picture and then it will display the current picture.</p>
  
  <p>JSP:</p>
  
  <pre><code>     &lt;div class="sortable"&gt;
  &lt;div class="box span5" style="margin-left: 50px;"&gt;
   &lt;div class="box-header well"&gt;
   &lt;h2&gt;&lt;i class="icon-th"&gt;&lt;/i&gt;Employee Attendance&lt;/h2&gt;
  &lt;div class="box-icon"&gt;
  &lt;a href="#" class="btn btn-minimize btn-round"&gt;&lt;i class="icon-chevron-up"&gt;&lt;/i&gt;&lt;/a&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="box-content"  style="height:230px;" &gt; 
  &lt;img border="0" src="admissionenquirylist.do?method=image" alt="Pulpit rock" width="370" height="240"/&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Servlet:</p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package com.treamis.admission.process;
  
  import com.google.gson.Gson;
  import com.treamis.entity.Academicyearmaster;
  import com.treamis.entity.AdmissionenquiryStudentdetails;
  import com.treamis.entity.EmployeeEntity;
  import com.treamis.hr.employee.PaginationClass;
  import com.treamis.hr.employee.SetPaginationRecords;
  import java.io.OutputStream;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.struts.actions.LookupDispatchAction;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.action.ActionForward;
  import java.util.*;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import javax.servlet.ServletOutputStream;
  
  /**
   *
   * @author ranjeeth.g
   */
  public class AdmissionEnquiry extends LookupDispatchAction {
  
      /* forward name="success" path="" */
      private final static String SUCCESS = "success";
  
      /**
       * Provides the mapping from resource key to method name.
       *
       * @return Resource key / method name map.
       */
      protected Map getKeyMethodMap() {
          Map map = new HashMap();
          map.put("button.admissionEnqiryList", "admissionEnqiryList");
          map.put("button.image", "image");
          map.put("button.delete", "delete");
          return map;
      }
  
      /**
       * Action called on Add button click
       */
      public void admissionEnqiryList(ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response) {
          // TODO: implement add method
          try {
              String stdutendserch = request.getParameter("admissionenquirysearch");
              System.out.println("stdutendserch = " + stdutendserch);
  
              Admissionservices as = new Admissionservices();
              List&lt;Enquirylistbean&gt; stdserc = as.getStudentEnquirySerch(stdutendserch);
              if (stdserc != null) {
                  response.setContentType("application/json");
                  String json = new Gson().toJson(stdserc);
                  System.out.println("json = " + json);
                  response.getWriter().print(json);
  
              } else {
                  response.setContentType("application/json");
                  String json = new Gson().toJson(null);
                  response.getWriter().print(json);
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  //        return mapping.findForward(SUCCESS);
  //        return null;
      }
  
      /**
       * Action called on Edit button click
       */
      public void image(ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response) {
          try {
              System.out.println("Inside the image responce action");
              response.setContentType("image/jpeg");
              Academicyearmaster academicyearmaster = (Academicyearmaster) request.getSession().getAttribute("academicyear");
  //            String ss = getServlet().getServletContext().getRealPath("\\");
  //            String filePath = ss + "img\\paichart.png";
              ServletOutputStream out = response.getOutputStream();
  //             System.out.println("out = " + out);
  //            String filePath2 = ss + "img\\paichart1.png";
  //            ExecutorService executor = Executors.newFixedThreadPool(2);
              com.treamis.hr.employee.Sendded sendded = new com.treamis.hr.employee.Sendded(out, academicyearmaster);
              sendded.image();
  //            executor.execute(sendded);
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          // TODO: implement edit method
  //        return mapping.findForward(SUCCESS);
      }
  
      /**
       * Action called on Delete button click
       */
      public ActionForward delete(ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response) throws java.lang.Exception {
          // TODO:implement delete method
          return mapping.findForward(SUCCESS);
      }
  
      /* And your JSP would have the following format for submit buttons:
  
       &lt;html:form action="/test"&gt;
       &lt;html:submit property="method"&gt;
       &lt;bean:message key="button.add"/&gt;
       &lt;/html:submit&gt;
       &lt;html:submit property="method"&gt;
       &lt;bean:message key="button.edit"/&gt;
       &lt;/html:submit&gt;
       &lt;html:submit property="method"&gt;
       &lt;bean:message key="button.delete"/&gt;
       &lt;/html:submit&gt;
       &lt;/html:form&gt;
       */
  }
  </code></pre>
  
  <p>Java code to generate image:</p>
  
  <pre><code>   try{
   ChartUtilities.writeChartAsJPEG(out, chart, 600, 400, info);
  //            System.out.println("file2 = " + file1);
          } catch (Exception e) {
              e.printStackTrace();
              return "success";
          } finally {
              out.close();            
          }
  </code></pre>