I have configured mysqlDatasource in tomcat from this and this. I have followed steps specified there. I have used following lines of code
class DataConnection {
    private static DataSource dataSource;
    public DataConnection() {
        try {
            Context ctx = new InitialContext();
            dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
        } catch (NamingException e) {
           e.printStackTrace(); 
        }
    }
    public static Connection getConnection() throws SQLException {
        new DataConnection();
          Connection con=dataSource.getConnection();
          return con;
    }
}
But I still get a NullPointerException when connecting database.
- Copied jar file into mysql/libfolder
- Creating context.xmlfile and mention connection property.
- In - WEB-INF/web.xmlI have mentioned- jdbc/db javax.sql.DataSource 
What am I doing wrong?
Note:I have created context.xml and mention the connection property. Finally manually copied into META-INF/context.xml.
 
     
    