XZing is a very powerful api that enables you to create and read a wide range of barcode formats such as 1D or 2D barcodes.

In this tutorial we will see how to  integrate barcode creation into java web applications and as an example format we will use  PDF147.

In the below snipet you can see the servlet for creating the barcode image:

package com.javaonly.barcode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.pdf417.encoder.PDF417Writer;
import com.google.zxing.qrcode.QRCodeWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "BarcodeServlet", urlPatterns = {"/barcode.png"})
public class BarcodeServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {

            String text = "BC1234567890";

            int width = 200;
            int height = 99;

            String imageFormat = "png";

            BitMatrix bitMatrix = new PDF417Writer().encode(text, BarcodeFormat.PDF_417, width, height);
            MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, response.getOutputStream());

        } catch (WriterException ex) {
            Logger.getLogger(BarcodeServlet.class.getName()).log(Level.SEVERE, null, ex);

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


 After creating  the png image you can access from your jsp image as shown below

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
       <img src="http://localhost:8080/WebApplication12/barcode.png"/>