Current Position:Home > Barcode Image display in email/jsp

Barcode Image display in email/jsp

Update:11-30Source: network consolidation
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.

The Best Answer

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.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
  BufferedInputStream input = null;
  BufferedOutputStream output = null;
  try {
    input = new BufferedInputStream(new ByteArrayInputStream(imageByteArr));
    output = new BufferedOutputStream(response.getOutputStream());
  catch (IOException e) {
  finally {
    if (output != null) {
      try {
      catch (IOException iex) {
    if (input != null) {
      try {
      catch (IOException iex) {
}Create following component configurations in your application's config layer to add your servlet using ServletPathServlet and ServletPathDispatcher:
Then in your JSP you can call it as:
<img src="<c:out value="${pageContext.request.contextPath}"/>/dyn/barcode-image-servlet" />