Thursday, April 25, 2013

Display tag library 1.2


The display tag library is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model. The library provides a significant amount of functionality while still being easy to use.


  • Download the display tag libraries from site . or use maven dependency

  • In jsp file import taglib 
                  <%@taglib prefix="display" uri="" %>

Note : Display tag internally use the '' file. If we really want to customize the display tag we need create the '' file or add the properties in <display:set> tag in jsp . Put the '' file in your class path . If you want to customize the css of table that generate by the <display> tag , create a css file and name the css file and create the class 'displayTable' and set the class attribute of <display> tag is 'displayTable' . 

The example of display tag as below : 

<display:table id="user" name="${users}" pagesize="2" class="displayTable "requestRI="listUsers.htm">
     <display:column property="userinfo.firstname" title="First Name" />
     <display:column property="userinfo.lastname" title="Last Name" />
     <display:column property="userName" title="Login" />
     <display:column title="Email" >
<a href="mailto:${}">${}</a>
     <c:forEach var="roles" items="${roles}" >
<display:column title="${}">
  <c:forEach var="roleOfUser" items="${user.userRoles}">
    <c:set var="roleId" value="${roleOfUser.role}"></c:set>
          <c:when test="${ eq }">
    <i class="icon-ok"></i>
     <display:column title="Edit" > 
       <a href="#" onclick="javascript:window.location.href='editUser.htm?userName=${userList.userName}';">
  <i class="icon-pencil"></i>
     <display:column title="Delete">
        <a href="deleteUser.htm?id=${userList.userName}" onclick="return confirm('Warning: This deletes the data from everywhere! Are you sure?')">
  <i class="icon-remove"></i>
     <display:setProperty name="paging.banner.placement" value="bottom" />
     <display:setProperty name="basic.msg.empty_list"  value="No Records Found" />   </display:table>

Friday, April 12, 2013

Use Apache DBCP with Core Java

Apache DBCP : 

Many Apache projects support interaction with a relational database. Creating a new connection for each user can be time consuming (often requiring multiple seconds of clock time), in order to perform a database transaction that might take milliseconds. Opening a connection per user can be unfeasible in a publicly-hosted Internet application where the number of simultaneous users can be very large. Accordingly, developers often wish to share a "pool" of open connections between all of the application's current users. The number of users actually performing a request at any given time is usually a very small percentage of the total number of active users, and during request processing is the only time that a database connection is required. The application itself logs into the DBMS, and handles any user account issues internally.
There are several Database Connection Pools already available, both within Apache products and elsewhere. This Commons package provides an opportunity to coordinate the efforts required to create and maintain an efficient, feature-rich package under the ASF license.
The commons-dbcp package relies on code in the commons-pool package to provide the underlying object pool mechanisms that it utilizes.
DBCP now comes in two different versions, one to support JDBC 3 and one to support JDBC 4. Here is how it works:
  • DBCP 1.4 compiles and runs under JDK 1.6 only (JDBC 4)
  • DBCP 1.3 compiles and runs under JDK 1.4-1.5 only (JDBC 3)

Requirements : 

  • Download jars of DBCP commons-dbcp.jar and commons-pool.jar from here.
  • download Database dirver (in this example , we use MySQL so download mysql-connector-java-5.1.13-bin.jar) : 

import java.sql.Connection;
import java.sql.SQLException;

import javax.activation.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

public class DbcpTest{
private static String connectURI = "jdbc:mysql://localhost/dbcp_test";
private static Connection conn;
private static ConnectionFactory connectionFactory;
private static ObjectPool connectionPool;
private static PoolingDataSource dataSource;
static {
connectionFactory = new DriverManagerConnectionFactory(connectURI, "root","");
connectionPool = new GenericObjectPool<>();
new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false,true);
dataSource = new PoolingDataSource(connectionPool);
}catch(ClassNotFoundException ex){
public void shutdownDatasource(DataSource ds) {
BasicDataSource bds = (BasicDataSource) ds;
}catch(SQLException ex){

public Connection getConnection() {
conn = dataSource.getConnection();
if(conn != null){
return conn;
return null;
}catch(SQLException ex){
return null;

When the code is compile ,with the help of getConnection() method , we get the Connection object and perform action .