Press enter to see results or esc to cancel.


Create Barcode in JSP page


How to Create and Shown bar code in JSP Page.
I have shown this using Eclipse IDE.
1. Project Structure


2. The jar used in the project are,
1. barcode4j.jar
2. itextpdf-5.1.0.jar
3. Mysql connector jar

3. Create Barcode in Static way using below Servlet
Create_Bar_Code.java

package com.chillyfacts.com;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
/**
 * Servlet implementation class Create_Bar_Code
 */
@WebServlet("/Create_Bar_Code")
public class Create_Bar_Code extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * Default constructor. 
     */
    public Create_Bar_Code() {
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/jpg");
        Code128Bean code128 = new Code128Bean();
		code128.setHeight(15f);
		code128.setModuleWidth(0.3);
		code128.setQuietZone(10);
		code128.doQuietZone(true);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/x-png", 300, BufferedImage.TYPE_BYTE_BINARY, false, 0);
		code128.generateBarcode(canvas, "123456789");
		canvas.finish();
        ServletOutputStream responseOutputStream = response.getOutputStream();
        responseOutputStream.write(baos.toByteArray());
        responseOutputStream.flush();
        responseOutputStream.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}

4. Create Barcode with Parameter passed from JSP Page
Create_Bar_Code_With_Parameter.java

package com.chillyfacts.com;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
/**
 * Servlet implementation class Create_Bar_Code
 */
@WebServlet("/Create_Bar_Code_With_Parameter")
public class Create_Bar_Code_With_Parameter extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * Default constructor. 
     */
    public Create_Bar_Code_With_Parameter() {
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String value=request.getParameter("value");
		response.setContentType("image/jpg");
        Code128Bean code128 = new Code128Bean();
		code128.setHeight(15f);
		code128.setModuleWidth(0.3);
		code128.setQuietZone(10);
		code128.doQuietZone(true);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/x-png", 300, BufferedImage.TYPE_BYTE_BINARY, false, 0);
		code128.generateBarcode(canvas, value);
		canvas.finish();
        ServletOutputStream responseOutputStream = response.getOutputStream();
        responseOutputStream.write(baos.toByteArray());
        responseOutputStream.flush();
        responseOutputStream.close();
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h3>Genearate barcode in JSP</h3>
Static Way <br>
<img alt="my Image" src="Create_Bar_Code">
<br><br>
Passing Parameters<br>
<table>
<tr>
<td>
<img alt="my Image" src="Create_Bar_Code_With_Parameter?value=111112222">
</td>
<td>
<img alt="my Image" src="Create_Bar_Code_With_Parameter?value=333334444">
</td>
</tr>
</table>
</center>
</body>
</html>

JSP page after running it on tomcat server
http://localhost:8080/Create_Bar_Code_JSP/index.jsp

5. DB Connection class to take data from MySQL table to create the bar code dynamically

package com.chillyfacts.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB_Connection {
	public static void main(String[] args) {
		DB_Connection ob_DB_Connection=new DB_Connection();
		System.out.println(ob_DB_Connection.getConnection());
	}
		public Connection getConnection(){
			Connection connection=null;
			System.out.println("Connection called");
			try {
				Class.forName("com.mysql.jdbc.Driver");
				connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
		 	} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return connection;
		}
}

6. MySQL Table data.


7. Take value from DB using the above db class and create the JSP page with dynamic bar codes in the page.

index_db.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.chillyfacts.db.DB_Connection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h3>Genearate barcode in JSP from MySQL DB</h3>
<br><br>
 <%
 try {
 DB_Connection obj_DBConnection = new DB_Connection();
 Connection connection = obj_DBConnection.getConnection();
 String query = "select * from links";
 Statement stmt = null;
 stmt = connection.createStatement();
 ResultSet rs = stmt.executeQuery(query);
 while (rs.next()) {
 %>
 <img alt="my Image" src="Create_Bar_Code_With_Parameter?value=<%=rs.getString("numbers")%>"><br><br>
 <% 
 }
 } catch (Exception e) {
 out.println(e);
 }
 %>
</center>
</body>
</html>

8. Dynamic JSP Page with Barcode created as per the database data
http://localhost:8080/Create_Bar_Code_JSP/index_db.jsp


Download the source code here.

Create_Bar_Code_JSP.rar

Comments

Leave a Comment