Monday 24 February 2014

Java String Objects and object creation JVM

Java Object toString()  method:

This method is used to view the text format of object created in heap;

class ToStringTest{
  public static void main(String[] ags){   
    ToStringTest tst = new ToStringTest();
     System.out.println("Object value"+tst);
     System.out.println("Hash Code"+tst.hashCode());

  
  }
}
 
When JVM executing  this line (ToStringTest tst = new ToStringTest())
1)The tst reference variable created in the stack ,
 
2)By new operator object creation in heap.(memory space allocating for the object)
 
3)Assignment operator =  is used for the assign the created object to reference variable 
 
 
 
output is Object value  ToStringTest@35c41b
                                     Hash Code   32429958
32429958 is interger value for object creatd location heap


after the @ symbol. hex decimal value of object .that created in heap


Tuesday 18 February 2014

Java Loosely and tightly coupled code or Spring Dependency Injection

Following post will explain the java tightly and loosely coupled code.And  How to avoid the tightly coupled code By using Spring Dependency Injection 

Following link have war file example. play it

Download Dependence Injection Example (War)

This is a web project war import this war to your eclipse and  right click on Employee class run the java
To view change the id ref to oracle to view change

Tightly Coupled Code:

If the one java class  completely  using the other java class's logic   means collaboration.

Example:

class SunMicrosystems {
 public void showCompanyName() {
  System.out.println("SunMicrosystems");

 }
}


/**
Employee  calls the  SunMicrosystems's   logic . If we  change the logic.we must change the java file also.
Employee class always depends the SunMicrosystems is called tightly coupled
*/


class Employee {
 SunMicrosystems sun = new SunMicrosystems();

 public void workCompany() {
  sun.showCompanyName();
 }

 public static void main(String[] arg) {
  Employee emp = new Employee();
  emp.workCompany();

 }

}

Let We See How to avoid tightly coupled code by Spring Dependency Injection.

By Introduce the Interface  we can avoid the tightly coupled code

Loosely Coupled Code: 

package com;

interface Company {
 public void showCompanyName();

}



package com;

class Oracle implements Company {
 public void showCompanyName() {
  System.out.println("Oracle");

 }
}


package com;

class SunMicrosystems implements Company {
 public void showCompanyName() {
  System.out.println("SunMicrosystems");

 }
}


package com;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

class Employee {

 private Company c;

 public void setC(Company c) {
  this.c = c;
 }

 public void showCompany() {
  c.showCompanyName();
 }

 public static void main(String[] arg) {
  Resource r = new ClassPathResource("applicationContext.xml");
  BeanFactory factory = new XmlBeanFactory(r);

  Employee emp = (Employee) factory.getBean("emp");
  emp.showCompany();
 }

}

/**
In the employee class class the method showCompany(), It in company  Interface , Following xml file we can change the ref attribute itself can desired ,the method call from which  class
*/


<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="sun" class="com.SunMicrosystems" />
<bean id="oracle" class="com.Oracle" />
  <bean id="emp" class="com.Employee">
    <property name="c" ref="sun" />
  </bean>

</beans>

Saturday 15 February 2014

Run Java Program With Out Main Method

Here We going see . How run the  java program without main method.

program

class WithOutMainMethod {
 static {
  System.out.println("Am run with out main method");
  System.exit(0);
 }
}

Explanation:

In Java program static block only execute first before main method.
Then JVM will search main method, Before that we terminating the JVM By
using System.exit(0);

zero--- normal termination
nonzero -abnormal nermination

Tuesday 11 February 2014

Firefox offline detection

In Firefox find is in offline.

In chrome , We can use  following code to detect the user in offline or online

if(navigator.onLine ){
 alert('online');
}else{
alert('offline');
}

But above code not work in firefox.For the purpose we can use dummy ajax request . Based on response time
We can say  user in online offline.

For Java application the code script as follow.

var run = function(){
  $.ajax({
    url: "/RedirectPage",
    type: "POST",
   cache: false,
   contentType:false,
              processData: false,
              timeout: 3000,
    success: function(data){
   
     if(data!='y'){
      
      alert("Your Internet Connection Lost."); 
      
       
     }
    },
   error: function(XMLHttpRequest, textStatus, errorThrown) {
           if(textStatus == 'timeout') {
              
         alert("Your Internet Connection Lost.");
           }
       }
   });
 };
 
 
 
  setInterval(run, 5000);//I will call for every 5 seconds.






In Java Code as follow.

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RedirectPage
 */
public class RedirectPage extends HttpServlet {
 private static final long serialVersionUID = 1L;

 @Override
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  response.setContentType("text/plain");
  response.setCharacterEncoding("UTF-8");
  response.getWriter().write("y");
  return;
 }
}

web-xml

    <servlet>
    <description></description>
    <display-name>RedirectPage</display-name>
    <servlet-name>RedirectPage</servlet-name>
    <servlet-class>RedirectPage</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RedirectPage</servlet-name>
    <url-pattern>/RedirectPage</url-pattern>
  </servlet-mapping>
  <servlet>

Monday 10 February 2014

Java- Exception

Servlet ,JSP IllegalstateException:

The IllegalstateException will occur if some code is exit after the response code.

Error Occur Code:
   if(username.equals("bala2e")){
      respons.sendRedirect("bala2e.blogspot.com");
}else{
       respons.sendRedirect("google.com");
}

To avoid  IllegalstateException by using following code.

By using  the return statement we can avoid the this exception


 if(username.equals("bala2e")){
      respons.sendRedirect("bala2e.blogspot.com");
   return;//to avoid  IllegalstateException 
}else{
       respons.sendRedirect("google.com");
return;
//to avoid  IllegalstateException 
}

Friday 7 February 2014

Java Static Block , Init Block,Constructor execution order

Java Static Block , Init Block,Constructor execution order in java program.

Execution order.

First - static block will execute only one time for program execution.
Second -Init block - will call each time object creation . execute before the constructor call.
Third - Constructor will execute.

Example Program.

/**
 * This program is explains the execution order of Static,Init,constructor
 * @author Balamurugan.B
 *
 */
public class StaticBlockEx{
 //init Block
 {
  System.out.println("Am init ");
  
 }
 //static Block
 static{
  System.out.println("Am static");
 }
 //Constructor 
 public StaticBlockEx() {
  System.out.println("Am Constructor");
 }
 
 
 
 public static void main(String[] args) {
  //Object Creation
   StaticBlockEx obj = new StaticBlockEx();
   
  
  
 }
}

Output:

Am static
Am init 
Am Constructor


Sunday 2 February 2014

Html , javascript,jquery UseFull Links

File Property  in HTML
Following link contains the file properties related to File object in Html.

http://help.dottoro.com/ljuelxgf.php

jquery  alerts

http://labs.bigroomstudios.com/libraries/Apprise-v2

nicEdit
http://nicedit.com/demos.php

http://wiki.nicedit.com/w/page/521/Javascript%20API


http://www.encodedna.com/2013/05/enter-only-numbers-using-jquery.htm