This discussion is archived
12 Replies Latest reply: Nov 27, 2012 8:12 PM by 975050 RSS

Barcode Image display in email/jsp

975050 Newbie
Currently Being Moderated
Hi,

I want to display the barcode image in the email/jsp.

I have created one droplet. This will generate the barcode image and will return as a OutputStream.
I am trying to convert this OutputStream as a byte array and setting in a request param. When i access this param in the jsp and trying to display as a image using dsp:valueof or dsp:img tag, i am getting only the byte array whatever i have set in the droplet instead of image.

I have set the content type as image/jpeg also in the request object.

I have tried to write the converted byte array in a response object also and accessed the droplet in jsp.

But i am not getting the image in the jsp. I am getting only the byte array in the jsp.

Please suggest me any other solution to resolve this.

Thanks,
Saravanan
  • 1. Re: Barcode Image display in email/jsp
    975050 Newbie
    Currently Being Moderated
    I do not want to store the barcode image anywhere in the file. I just want to include the barcode in the jsp/email whenever the barcode has been generated.
  • 2. Re: Barcode Image display in email/jsp
    Girish V U Newbie
    Currently Being Moderated
    I assume you will be using some 3rd party Barcode lib (available as a war) e.g. http://www.onbarcode.com/purchase/purchase-java-barcode-generator.html

    make this barcode generator available to your application by making its entry in web.xml of your own web.xml (e.g. store.war)

    In your email template you need to write something like,
    <img src="${httplink}/xyz/myBarcodeDecryptor.jsp?encryptedCardNumber=${encryptedCardNumber}"/>

    (Note:myBarcodeDecryptor.jsp will not be present "physically" in your code base ,its just a trick to get out code ,i.e requestDispatcher to include barcode lib generator war in scene.)

    Now, write a InsertableServlet which will keep looking for path "/xyz/myBarcodeDecryptor.jsp" and if it finds it ,i.e., when this <img> tag is executed ,what this insertable servlet should do is (Pseudo Code),
    decryptedBarcodeNumber=decrypt(encryptedCardNumber);
    pRequest.getRequestDispatcher("/barcode?DATA="+decryptedBarcodeNumber+"&TYPE=CODEXXX").include(pRequest, pResponse);

    Note: /barcode , DATA and TYPE,CODE all these are onbarcodelib specific ,you can manipulate these but the basic logic remains the same.

    Let me know if in case you are not clear.

    Hope this helps,

    Girish
  • 3. Re: Barcode Image display in email/jsp
    975050 Newbie
    Currently Being Moderated
    Hi Girish,

    Thanks for your reply.

    I am using apache barcode4j api to generate the barcode.

    I will try your suggestion.

    I havnt tried to call the servlets in the jsp pages which is under the dsp:page tag. I am using the droplets instead of servlets.
    In the normal jsp pages i have configured the servlets in web.xml and whenever the requested url pattern matches, the servlets will get triggered and displayed the result.

    Also the email template(jsp page) will be parsed and added the required details when we try to send the email. Hence, i am not clear about if we add the url for the img src attribute, how the requested droplets/servlet will be called ?

    sorry if i misunderstood anything.
  • 4. Re: Barcode Image display in email/jsp
    Girish V U Newbie
    Currently Being Moderated
    The fact that we are using InsertableServlet (i.e. thru a Servlet pipeline and hence will be executed on all pages but will be functional only on specific page) should handle that and ur barcode generator code is wrapped in a primary condition of something like,
    if(pRequest.getRequestURI().contains("/xxx/myBarcodeDecryptor.jsp")){} will make sure that barcode generation happens only on this pseudo jsp reference given in <img> tag.

    Let me know if you need anything else from my side.

    Regards,
    GIrish
  • 5. Re: Barcode Image display in email/jsp
    Nitin Khare Expert
    Currently Being Moderated
    If you have a binary array for image, you can try to use a servlet for rendering the image directly from the byte array. Doing it through a separate servlet would ensure that we do not run into the issue of mixing character and binary based response streams. Here is the pseudo code for doing this.

    In Servlet:
    BarcodeImageHandlerServlet extends extends HttpServletService {
    ..
    ..
    public void service(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
    
      byte [] imageByteArr = getImage(); //your image byte array
      String name= "barcode-01";
    
      response.setContentType("image/jpeg");
      response.setContentLength(imageByteArr.length);
      response.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
    
      BufferedInputStream input = null;
      BufferedOutputStream output = null;
                   
      try {
        input = new BufferedInputStream(new ByteArrayInputStream(imageByteArr));
        output = new BufferedOutputStream(response.getOutputStream());
        output.write(imageByteArr);
      }
      catch (IOException e) {
      }
      finally {
        if (output != null) {
          try {
            output.close();
          }
          catch (IOException iex) {
          }
        }
        if (input != null) {
          try {
            input.close();
          }
          catch (IOException iex) {
          }
        }
      }
    }
    ..
    ..
    }
    Create following component configurations in your application's config layer to add your servlet using ServletPathServlet and ServletPathDispatcher:

    /atg/myapp/BarcodeImageServlet.properties:
    $class=com.myapp.BarcodeImageHandlerServlet
    $scope=global

    /atg/dynamo/servlet/pipeline/ServletPathDispatcher.properties:
    dispatcherServiceMap+=\
    /barcode-image-servlet\=/atg/myapp/BarcodeImageServlet

    /atg/dynamo/servlet/pipeline/ServletPathServlet.properties:
    servletPaths+=/barcode-image-servlet

    Then in your JSP you can call it as:
    <img src="<c:out value="${pageContext.request.contextPath}"/>/dyn/barcode-image-servlet" />
  • 6. Re: Barcode Image display in email/jsp
    975050 Newbie
    Currently Being Moderated
    Hi Nitin,

    Thanks for the reply.

    I tried the approach and able to call the droplet from jsp when the 'barcode-image-servlet' mentioned in the src attribute.
    I am able to get the byte array and set in the OutputStream of response.
    However, i am not able to get the image in the jsp/email. Earlier i was able to get the byte array in the jsp when i directly call the droplet in jsp using dsp:droplet. But now i am not able to get any result in the jsp/email.

    I have one doubt. I have given the alt name in the img tag. If the src attribute fails to display the result, the alt name should be displayed right. But in this case, the alt name also not displayed. Hence, i am not clear about, am i getting the output or there is a problem to display in the email?

    Please guide me to fix this issue.
  • 7. Re: Barcode Image display in email/jsp
    Nitin Khare Expert
    Currently Being Moderated
    Can you check the full url/path of the img src in your email output? I think you may need to add the host name and port in the url which is getting formed. You can always test by hitting the formed url directly from the browser and you should be able to see the image. Once you have verified the image in browser then use and form the same url for rendering the image in the email.
  • 8. Re: Barcode Image display in email/jsp
    975050 Newbie
    Currently Being Moderated
    I tried to use the full url/path in the src attribute.

    I have added the full path as follows,

    <img src="http://<dsp:valueof bean='/atg/dynamo/Configuration.siteHttpServerName'/>:<dsp:valueof bean='/atg/dynamo/Configuration.siteHttpServerPort'/><dsp:valueof value="${pageContext.request.contextPath}" />/dyn/barcode-image-servlet"></img>

    <img src="http://<dsp:valueof bean='/atg/dynamo/Configuration.siteHttpServerName'/>:<dsp:valueof bean='/atg/dynamo/Configuration.siteHttpServerPort'/><dsp:valueof value="${pageContext.request.contextPath}" />/emailTemplates/eGiftCardTemplate.jsp/dyn/barcode-image-servlet"></img>

    In both cases the droplet called successfully and the byte array generated. But while send the reponse from droplet, i am not able to get the output in jsp/email.

    Also, i am not able to check like to hit the full url in browser and see the image. Because, i have to pass the image number to the droplet, this will be generated only when we do the complete flow. Hence, when i enter the full path in browser, the number will be sent as empty.
  • 9. Re: Barcode Image display in email/jsp
    Nitin Khare Expert
    Currently Being Moderated
    I have used it many times and it works well. For testing purpose see if you can hardcode the image number or other required data just to verify the URL and to render the image from byte array. Otherwise you would have to post your droplet code here as nothing much can be suggested without actually looking at it.
  • 10. Re: Barcode Image display in email/jsp
    975050 Newbie
    Currently Being Moderated
    Here is my servlet code. Earlier i have used the droplet instead of normal servlet. But after using this servlet code also the image is not getting displayed.
    I have removed the output.close and input.close in finally block, because i am getting the IO Stream closed exception. So that the email not triggered. After removing this the email triggered.

    When i enter the jsp page url directly in the browser, the droplet getting called and when i open the View Source of this jsp page, the image byte array displayed in the image location. But in the jsp page nothing displayed at the <img src> location.

    public class BarcodeImageHandlerServlet extends HttpServletService {

    ..

    public void service(HttpServletRequest pRequest,
                   HttpServletResponse pResponse) throws ServletException,
                   IOException {
         String number = "123456789";
         ByteArrayOutputStream baos = getBarUtil().getBarcodeImg(number);
         byte [] imageByteArr = baos.toByteArray();
         String name= "barcode-01";
         
         pResponse.setContentType("image/jpeg");
         pResponse.setContentLength(imageByteArr.length);
         pResponse.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
         
         BufferedInputStream input = null;
         BufferedOutputStream output = null;
                        
         try {
         input = new BufferedInputStream(new ByteArrayInputStream(imageByteArr));
         output = new BufferedOutputStream(pResponse.getOutputStream());
         output.write(imageByteArr);
         output.flush();
         }
         catch (IOException e) {
         }
    }

    ...

    }
  • 11. Re: Barcode Image display in email/jsp
    Nitin Khare Expert
    Currently Being Moderated
    That code looks fine. You can remove all the BufferedInputStream part as it is not doing anything here. In my original code it was there for some other purpose but not required here. I have updated the code slightly with comments so you can check accordingly and see if you can get more information about your issue.
    public void service(HttpServletRequest pRequest, HttpServletResponse pResponse) throws ServletException, IOException {
    
    String number = "123456789";
    ByteArrayOutputStream baos = getBarUtil().getBarcodeImg(number);
    byte [] imageByteArr = baos.toByteArray();
    String name= "barcode-01";
    
    pResponse.setContentType("image/jpeg");  //check if we are setting the correct mime type based on the image format like image/jpeg or image/gif or image/png etc. (refer code snippet below for checking the image format)
    pResponse.setContentLength(imageByteArr.length);  //check the byte array size which would be same as the image size
    pResponse.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
    
    BufferedOutputStream output = null;
    
    try {
      output = new BufferedOutputStream(pResponse.getOutputStream());
      output.write(imageByteArr);
      output.flush();
    }
    catch (IOException e) {
      //check if there is any exception
      e.printStackTrace();
    }
    
    }
    Also, check if your byte array is properly representing an image. You can use following code snippet to test the image format which is being represented by the byte array (referenced classes are from javax.imageio and javax.imageio.stream packages):
    try
    {
      ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(imageByteArr));
      Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
      if (iter.hasNext()) {
        ImageReader reader = (ImageReader) iter.next();
        reader.setInput(iis);
        String formatName = reader.getFormatName();
        System.out.println("Image type is : " + formatName);
      }
    }
    catch (IOException e) {
      System.err.printf("Failed to determine image format %s", e.getMessage());
      e.printStackTrace();
    }
  • 12. Re: Barcode Image display in email/jsp
    975050 Newbie
    Currently Being Moderated
    Hi,

    Finally the issue has been resolved. We are able to get the barcode image in the email while sending from the servlet.

    Initially we were tried with the Droplet to include the byte array in the response stream. But this approach is not worked for us.
    Hence, we tried to create a normal HTTP servlet and made the entry in the web.xml while call from the jsp.
    The url-pattern of the servlet given in the img src attribute.

    Thanks Nitin and Girish for your valuable inputs.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points