Saturday 31 December 2016

2016: a memory

Another year comes to end here, last day of the 2016, time to introspect to conclude another year with lots of ups and downs.

Life keeps on moving, so we should be!

"Whether you choose to move on and take a chance in the unknown,
or stay behind, locked in the past,
thinking what could have been.."

The choice is very simple, lets forget what happened and concentrate on what can happen.

Though introspecting and analyzing the previous days always helps to make great decisions.

2016 had been full of ups and down. Here are few snapshots:

The precious gift of the God, that became source of our happiness, my niece,

Inaya Ashraf.




Every time I look at her, gives immense happiness. Her cuteness and ... is growing faster than her. The way she mimics crow, goat and grand father is literally amazing. Though I hardy spent time with her in the past one year, I never missed to listen to her voice and and watching her dance skill over the phone. On January 3rd she is completing her fist birthday.












After spending a golden period of time leaving a place and people associated with that is difficult. But when you have to make a move for your career you are left with no choice. As life a concept of moving on, we are also following the same:
Though it was painful but every pain gets covered with time..

When you move to new place, a new working culture, new faces. In a few day you meet with a set new people. And then you start heeling from the previous pain.

People are amazing, they make you laugh, they helpful, and you find
another set of your buddies with whom you can roam, party hard.













Thank you 2016 for adding another wonderful year to the life..



Friday 4 March 2016

Creating and Deploying Servlet without using Eclipse

This tutorial will guide you how to create and deploy Servlet without using Eclipse IDE.

Step 1. Download Tomcat : Tomcat is an application server from the Apache Software Foundation that executes Java servlets and renders Web pages that include Java Server Page coding.
Download latest release of Tomcat https://tomcat.apache.org/download-80.cgi
Step 2. Configure Tomcat : Unzip the tomcat package to C (or other) drive in my case it is D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65

Now setup the JAVA_HOME, CATALINA_HOME, PATH environment variable:

Creating JAVA_HOME :
Click on -> Control Panel -> System -> Advanced->Environment Variable in User variables tab add new variable name:  JAVA_HOME and variable value :  C:\ "top level directory of your java install"
eg: variable name JAVA_HOME
      variable value :  C:\Program Files\Java\jdk1.8.0_40
      Note: Separate variable values by semicolon, if other value already exists(;)

Creating CATALINA_HOME :
Click on -> Control Panel -> System -> Advanced->Environment Variable in User variables tab add new variable name:  CATALINA_HOME and variable value :  C:\ "top level directory of your Tomcat install"
eg: variable name CATALINA_HOME :
      variable value :  D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65
      Note: Separate variable values by semicolon, if other value already exists(;)

Appendng PATH :
Click on -> Control Panel -> System -> Advanced->Environment Variable in User variables tab select path variable and click on edit button. In variable value section give a semicolon and at the end paste the following text : %PATH%;%JAVA_HOME%\bin;%CATALINA_HOME%\bin

Step 3. Starting Tomcat: Go to the location of startup.bat D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\bin and double click on startup.bat
Now go to the browser and give url as localhost:8080 it should bring the apache tomcat home page.


Step 4. Creating Directory structure;
Step 5. Creating Servlet:

import javax.servlet.http.*;  
import javax.servlet.*;  
import java.io.*; 

public class DemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");
pw.close();
}
}

Step 6. Compiling Servlet:
Go the directory containing DemoServlet and compile DemoServlet using servlet-api.jar.
eg: javac -classpath "D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\lib\servlet-api.jar" "DemoServlet.java"
Copy the DemoServlet.class file to the classes directory.

Step 7. Creating Deployment Descriptor:

<web-app>
  
<servlet>
     <welcome-file-list>
    <welcome-file>index.html</welcome-file>
   <welcome-file>default.html</welcome-file>
    </welcome-file-list>  
    <servlet-name>demo</servlet-name>  
    <servlet-class>DemoServlet</servlet-class>  
</servlet>  
  
<servlet-mapping>  
    <servlet-name>demo</servlet-name>  
    <url-pattern>/demoPage</url-pattern>  
</servlet-mapping>  
  
</web-app> 


Step 8. Deploying application to server:

Method 1-> Copy the application folder (here it is context root) to the apache tomcat's webapps directory for example I copied inside D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\webapps

Method 2->  Create WAR of your application and deploy it to tomcat through browser.
eg: Creating WAR, in cmd do  D:\myPrograms\servlets\web-app>jar -cvf myProject.war *
      Upload myProject.war and deploy in Tomcat server.

Step 9. Access the Servlet through URL: http://localhost:8084/myProject/demoPage



The caveman who didn't say anything to you

Today I watched  caveman "romantic film written and directed by Herschel Faber and starring Skyler Astin, Camilla Belle. I found myself in the character of Dean, one of the cavemen who had many things to say and finally said them all. But the difference between Dean and me was I did not say..

I did not say that..
I noticed your each and everything from very first day you entered to our zone..
I did not say that..
Your presence made me alive, I could finish my work before deadlines so that I'll find sometime to stay around you..
I did not say that..
Your every glimpse made me so  energetic I could happily survive in all the odd circumstances..
I did not say that..
My heart pounded out of my chest, every time, every time I saw you smiling ..
I did not say that..
I wanted to get down to the knee and tell you what I feel for you and assemble the courage to be the happiest man of the universe..
I did not say that..
I wanted to sing for you every morning when the sun rises..
I wanted to hold your hands tight in your good/bad times..
I wanted to drink with you as hard as possible..
I wanted to dance with you as no one is watching us..
I wanted to be a hardcore veggie for you..
I really wanted to be part of your .. !!!

I wish life could have been like the movie, a happy ending, where a caveman finds his true mate and becomes the most luckiest man for forever. I wish there wouldn't have been a wall of religion between us.  I wish I would have met you way back when you weren't with someone else ..

And last but not the least.. I will let you know that.. "You say it best, When You Say Nothing At All."

God Bless ya  bebe!!

Sunday 28 February 2016

Understanding hashCode and equals contract

When it comes to storing objects in a hashMap, we must follow the contract between hashCode() and equals(). The contract says:

  1. If two objects are equal, then they must have the same hash code.
  2. If two objects have the same hash code, they may or may not be equal.
or in other words
If equal, then same hash codes too.
Same hash codes no guarantee of being equal.

Lets see the scenarios what happens when we deal with object in hashMap:
import java.util.HashMap;

// Scenario 1 : Neither of  equals() or hashCode() is overridden 
import java.util.*;
 class Apple {
private String color;
public Apple(String color) {
this.color = color;
}
  public static void main(String[] args) {
Apple a1 = new Apple("green");
Apple a2 = new Apple("red");
//hashMap stores apple type and its quantity
HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
m.put(a1, 10);
m.put(a2, 20);
System.out.println(m.get(new Apple("green")));
}
}

The out put is simply "null" (of course which is not expected)
The reason is when we are calling m.get(new Apple("green")), the hash created by hashCode() would be different as its will hold a different memory location. So it will not be available in the hash table bucket.

// Scenario 2 : Overriding equals() 
import java.util.*;
 class Apple {
private String color;
public Apple(String color) {
this.color = color;
}
      //overriding equals
public boolean equals(Object obj) {
if(obj==null) return false;
if (!(obj instanceof Apple))
return false;
if (obj == this)
return true;
return this.color.equals(((Apple) obj).color);
}

  public static void main(String[] args) {
Apple a1 = new Apple("green");
Apple a2 = new Apple("red");
//hashMap stores apple type and its quantity
HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
m.put(a1, 10);
m.put(a2, 20);
System.out.println(m.get(new Apple("green")));
}
}

Still the out put is  "null" (again which is not expected)
Reason: Here we broke the contract of hashCode() and equals(). We must override hashCode() when we override equals(). Because the default behavior of equals() is to compare memory of the objects. When we are overriding the behavior of equals this will change the default behaviour..which leads to breaking the contract between hashCode() and equals(){If two objects are equal, then they must have the same hash code.}

// Scenario 3 : Overriding both equals() and hashCode() {Correct scenario}

import java.util.*;
 class Apple {
private String color;
public Apple(String color) {
this.color = color;
}
      //overriding equals
public boolean equals(Object obj) {
if(obj==null) return false;
if (!(obj instanceof Apple))
return false;
if (obj == this)
return true;
return this.color.equals(((Apple) obj).color);
}
     //overriding hashCode
public int hashCode(){
return this.color.hashCode();
}

  public static void main(String[] args) {
Apple a1 = new Apple("green");
Apple a2 = new Apple("red");
//hashMap stores apple type and its quantity
HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();
m.put(a1, 10);
m.put(a2, 20);
System.out.println(m.get(new Apple("green")));
}
}

Now hope you hape a better understanding why we should override hashCode() and equals() while dealing with hashMap and objects.
References:

Thursday 11 February 2016

Why Should you buy a DSLR

This is the first question should come in your mind when you are thinking to buy a DSLR.
Let's play a Devil's advocate to reconsidering your decision.
Why do you really need a DSLR? People will tell you about the absolute advantages of a DSLR but you don't buy a Harley Davidson just because its features and specs are awesome(I know some idiots do it, I'm the one who is planning to do the same).
Most first time user/photography enthusiasts need a good camera for capturing family and general travel photos. They don't intend to freeze themselves in mountains trying to get that perfect night shot, or spend 50% of their honeymoon time trying to setup tripods (which btw, is a bad idea).The

DSLRs are awesome only if :
  • Passionate about photography
  • Need specific kind of pictures
  • Intend to devote some part of your life to do something which brings happiness to you
  • Have a one who considers you as an object when hand over your DSLR and brings best out of you.
Now lets see some of the comparative advantages I've collected to have a DSLR

  • Bigger sensor - Sensor is where all the data shot by a camera gets recorded. Imagine rain drop falling from sky as light. Keeping a bucket and keeping a glass outside, the bucket will gather more water from the same source in same time. Big sensor = Big bucket. Is that better? Yes, if you are willing to exploit that data (aka process pictures in an editing software).


In the picture above you can see how it differentiates one object from the others.

  • Night photography :If you have a DSLR you could be the king or queen of photography  where others have a normal digital camera, There you would realize it's worth having one. DSLRs are the Rocky Balboas of night photography.

  • Depth of Field : One of the things I love about my DSLR is the versatility that it gives in many areas, especially depth of field. I guess this is really an extension of it’s manual controls and ability to use a variety of lenses but a DSLR can give you depth of field that puts everything from foreground to background in focus through to nice blurry backgrounds.

There are plenty of advantages you will have when you have one have one in your hand, specially the one when people will come around and give an exclamation that you are holding a DSLR :P.

Well, well well enough for advantages, now lets have a small note on the disadvantages you could have:
Expensive : Obviously difficult to afford for the people like me :P
Weight : I can carry it easily but some of you might feel a bit heavy.
Ease of use : Initially have to struggle to make angle and take proper shots.

Have a nice photography with your digital single-lens reflex....Saifu :)

Sunday 3 January 2016

Welcome little Princes : Inaya



A baby is the cutest gift of God, when I got a call from my younger sister saying "bhayya aap chacha ban gye hain". A sensation of strange feeling ran in my body.Could not respond to my emotion, whether to lough or cry. Just asked her saying send some pics of her I want to see my little angel.

In hurry-burry manner I opened the WhatsApp and kept on waiting for sister's ping. Few seconds later my phone vibrates and there she is.
The beauty which blew my mind, a cuteness which didn't allow my eyes to move away.
Just kept on staring the little princes with a mixed emotions, smile on face and tears in eyes. My fingers were roaming around her face through touch screen of the mobile. Zoomed inn and zoomed out. I just wanted to run like anything and catch flight to reach as soon as possible to have a glimpse of my little angel. The concentration broke when I saw Mom's call on mobile screen.
All the conversation went about her only. At the end I guided my Mom to "kan ke neeche Kajal laga dena" warna meri beti ko Nazar lag jaegi (:P).

This is the one of the most beautiful gifts I have received from God.
Wish she will stay healthy and happy..