Sunday, October 12, 2014

Difference between Struts1.X and Struts2.X

Difference Area
               Struts 1.X
               Struts 2.X
Action Class
In Struts 1 it's mandatory to extend org.apache.struts.action.Action and implement execute() method which returns ActionForward and accept HttpServletRequest and HttpServletResponse.
This is not the case with Struts 2, here Action class can be a simple POJO or Java object with execute() method. Also execute() method returns String rather than returning ActionForward object.  You can still use  ActionSupport class or Action interface but those are completely optional.
Configuration Files

Front Controller
In struts 1.x front controller is ActionServlet
In struts 2.x front controller is FilterDispatcher
Servlet Dependency
In action class, execute() method , it has HttpServletRequest and HttpServletResponse Object, both comes from servlet API
Not needed in struts 2 execute() method
No Action Form
Struts 1 uses an ActionForm object to capture input. Like Actions, all ActionForms must extend a base class. Since  other JavaBeans cannot be used as ActionForms, developers often create redundant classes to capture input. 
Struts 2 uses Action properties as input properties, eliminating the need for a second input object
Control Of Action Execution
Struts1 supports separate Request Processor (lifecycles) for each module, but all the Actions in a module must share the same lifecycle
Struts 2 supports creating different lifecycles on a per Action basis via Interceptor Stacks.

 Threading Model

Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. The singleton strategy places restrictions on what can be done with Struts 1 Actions and requires extra care to develop. Action resources must be thread-safe or synchronized.
Struts2, Action objects are instantiated for each request, so there are no thread-safety issues

Tuesday, October 7, 2014

Java 8 Features

Java 8 release e.g. lambda expressions, functional interface, stream API, default methods and new Date and Time API. 

1. Lambda Expressions 
One of feature, which is synonymous to this release, is lambda expressions, which will provide ability to pass behaviours to methods. Prior to Java 8, if you want to pass behaviour to a method, then your only option was Anonymous class, which will take 6 lines of code and most important line, which defines the behaviour is lost in between. Lambda expression replaces anonymous classes and removes all boiler plate, enabling you to write code in functional style, which is some time more readable and expression.

Lambda Expressions Syntax

The basic syntax of a lambda is either:
(parameters) ->expression      or         (parameters) ->{ statements; }
The following are examples of Java lambda expression:
1. () -> 5          // takes no value and returns 5
2. x -> 2 * x        // takes a number and returns the result of doubling it
3. (x, y) -> x – y         // takes two numbers and returns their difference
4. (int x, int y) -> x + y // takes two integers and returns their sum
5.  (String s) -> System.out.print(s)   // takes a string and prints it to console without returning anything

Basic Lambdas Examples

Example 1- In this section, we will see how lambda expressions affect the way we code. Having a list of players, the “for loop”, as programmers often refers to the, for statement, can be translated in Java SE 8 as below:

String[] studentNames = {"Jeet", "Harsh", "Merry", "David", "Roger", "Andy ", "Tomas", "John"};
List<String> students =  Arrays.asList(studentNames);
// Old looping
for (String student : students) {
     System.out.print(student + "; ");
// Using lambda expression and functional operations
students.forEach((student) -> System.out.print(student + "; "));

// Using double colon operator in Java 8

Example 2As you saw, lambda expressions can reduced our code to one single line. Another example is in a graphical user interface application, when anonymous classes can be replaced with lambda expressions:

// using anonymous innerclass
button.setOnAction(new EventHandler<ActionEvent>() {
                       public void handle(ActionEvent event) {
                                      System.out.println("Hello World!!"); 
// using lambda expression
button.setOnAction(event -> System.out.println("Hello World!!"));

Example 3 - Implementing the Runnable interface, see below how we can write Runnable using lambda expression:

// Using anonymous innerclass 
new Thread(new Runnable() {
        public void run() {
               System.out.println("Hello world !!");
// Using lambda expression
new Thread(() -> System.out.println("Hello world !!")).start();
// Using anonymous innerclass
Runnable run1= new Runnable() {
        public void run() {
               System.out.println("Hello world !!");
// Using lambda expression
Runnable run2= () -> System.out.println("Hello world !!"); 
// Run em!;;

Friday, October 3, 2014

10 Frequently asked SQL Queries In Interviews

          In this article I am giving example of some SQL query which is asked in most the of interviews who is having one or  two year experience on this field .whenever you go for java developer position or any other programmer position  interviewee expect that if you are working from one or two years on any project definitely you come across to handle this  database query, so they test your skill by asking this type of simple query.

 Que 1: SQL Query to find second highest salary of Employee

  Ans : There are many ways to find second highest salary of Employee in SQL, you can either use SQL Join or Subquery     to solve this problem. Here is SQL query using Subquery :

 select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );

 Que 2: SQL Query to find Max Salary from each department.

 Ans :

 SELECT DeptID, MAX(Salary) FROM Employee  GROUP BY DeptID.

 Que 3: Write SQL Query to display current date.

  Ans: SQL has built in function called GetDate() which returns current timestamp.

 SELECT GetDate();

 Que 4: Write an SQL Query to check whether date passed to Query is date of given format or not.

  Ans: SQL has IsDate() function which is used to check passed value is date or not of specified format ,it returns 1(true)   or 0(false) accordingly.

 SELECT  ISDATE('1/08/13') AS "MM/DD/YY";

 It will return 0 because passed date is not in correct format.

 Que 5: Write a SQL Query to print the name of distinct employee whose DOB is between 01/02/2012 to 31/12/2013.

 SELECT DISTINCT EmpName FROM Employees WHERE DOB  BETWEEN ‘01/02/2012’ AND31/12/2013’;

 Que 6:Write an SQL Query find number of employees according to gender  whose DOB is between 01/01/1960 to  31/12/1975.

  Ans : SELECT COUNT(*), sex from Employees  WHERE  DOB BETWEEN ‘01/01/1960 ' AND  ‘31/12/1975’ GROUP BY sex;

 Que 7:Write an SQL Query to find employee whose Salary is equal or greater than 20000.

 Ans : SELECT EmpName FROM  Employees WHERE  Salary>=20000;

 Que 8:Write an SQL Query to find name of employee whose name Start with ‘S’

 Ans: SELECT * FROM Employees WHERE EmpName like 'S%';

 Que 9: find all Employee records containing the word "John", regardless of whether it was stored as JOHN, John, or  john.

 Ans : SELECT  * from Employees  WHERE  upper(EmpName) like upper('john%');

Que 10: Write a SQL Query to find year from date.

Ans :  SELECT YEAR(GETDATE()) as "Year";