Thursday, June 14, 2012

ajax call from java script....

var custID = document.getElementById("customerID").value;
        var custName = document.getElementById("customerName").value;
        var left = (screen.width/2) - (650/2);
        var top = (screen.height/2) - (450/2);
        if((document.getElementById("customerID").value == "") && (document.getElementById("customerName").value == "")){
            alert("Please enter Customer ID or Customer Name to search.");
            document.getElementById("customerID").focus();
            return false;
        } else {       
            //window.open("FileDocumentServlet?eid=<%=FileDocumentEventNames.BUSINESS_FORMATION_SEARCH_CUST%>&customerID="+custID+"&customerName="+custName, "customerWindow",
            //"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=850,height=430,top="+top+",left="+left);
            document.getElementById("button").click();
            var url = "FileDocumentServlet?eid=<%=FileDocumentEventNames.BUSINESS_FORMATION_SEARCH_CUST%>&customerID="+custID+"&customerName="+custName;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
            } else {// code for IE6, IE5     
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
              if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ 
                 var customerResult = xmlhttp.responseText;
                    document.getElementById("popupContact").innerHTML = customerResult;                 
                    return true;
                 } else {
                     document.getElementById("popupContact").innerHTML = document.getElementById("loadingMessageId").innerHTML;
                 }
             }  
             xmlhttp.open("POST",url,true);
          xmlhttp.send();
                 
        }

return value from ahref tag in javascript

<a href="#" onclick="return editRecord(<%=i%>)"><img src="images/icon2_edit.gif"></a>&nbsp;&nbsp;&nbsp;

java script array values sending to java

var columns =document.getElementById("columnSize").value;
        var columnsName = new Array();
        var columnsValue = new Array();
         for(var i=0 ;i<columns;i++){
            if(document.getElementById("insertRow"+i).value == ""){
                alert('Please fill the all fields');
                return false;
            }
        }
        var rows = document.getElementById("rowSize").value;
        for(var i=0 ;i<columns;i++){
            var character =  document.getElementById("insertRow0").value;
            if(document.getElementById("columnValue0"+i).value == character.toUpperCase()){
                alert('Entered value already avilable');
                return false;
            }
            if(document.getElementById("columnValue0"+i).value == character.toLowerCase()){
                alert('Entered value already avilable');
                return false;
            }
        }

        for(var i=0;i<columns;i++){
        var colname =document.getElementById("columnName"+i).value;
        columnsName[i]=colname;
        }
        for(var i=0 ;i<columns;i++){
        var rowValue =document.getElementById("insertRow"+i).value;
        columnsValue[i]=rowValue;
        }
        var tableName1 = document.getElementById("referenceTables").value;
        document.form1.eid.value= "9827" ;
        document.form1.tableName.value= tableName1 ;
        document.form1.action ="Admin?columnsName="+columnsName.join("&columnsName=")+"&columnsValue="+columnsValue.join("&columnsValue=");
          document.form1.submit();
          return true;

Wednesday, May 23, 2012

String util services for String handling in java


public static boolean equalsIgnoreCase(String str1, String str2) {
        return ((str1 == null) ? (str2 == null) : str1.equalsIgnoreCase(str2));
    }
 public static boolean equals(String str1, String str2) {
        return ((str1 == null) ? (str2 == null) : str1.equals(str2));
    }

    public static String trim(String str) {
        return ((str == null) ? null : str.trim());
    }

   public static boolean isNotEmpty(String str) {
        return ((str != null) && (str.length() > 0));
    }

  public static boolean isEmpty(String str) {
        return ((str == null) || (str.length() == 0));
    }


    public static boolean isBlank(String str) {
        int strLen;

        if ((str == null) || ((strLen = str.length()) == 0)) {
            return true;
        }

        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }

        return true;
    }

 public static boolean isNotBlank(String str) {
        int strLen;

        if ((str == null) || ((strLen = str.length()) == 0)) {
            return false;
        }

        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return true;
            }
        }

        return false;
    }
public static boolean equalNullOrTrimEmpty(String str1, String str2){
        return (str1 == null
                    ? (str2 == null
                            ? true
                            : (str2.trim().equals("")
                                ? true
                                : false))
                    : (str2 == null
                            ? (str1.trim().equals("")
                                ? true
                                : false)
                            : (str1.trim().equals(str2.trim()))
                        )
                );                        
    }

public static boolean equalNullOrTrimEmptyIgnoreCase(String str1, String str2){
        return (str1 == null ? (str2 == null ? true : (str2.trim().equals("") ? true : false)): (str2 == null? (str1.trim().equals("")? true: false): (str1.trim().equalsIgnoreCase(str2.trim()))));                        
    }


public static String getFormattedAmount(double param) {
        if(param == 0.0)
            return "$0.00";
        return NumberFormat.getCurrencyInstance().format(param);   
    }
   
    public static String getFormattedAmount(String param) {
        if(isEmpty(param))
            return "";
        if (param.startsWith("$")){
            return param;
        }
        try{
            double f1=Double.parseDouble(param);
            return NumberFormat.getCurrencyInstance().format(f1);   
        }catch(NumberFormatException e){
            return param;
        }
    }
   
    public static String getPercentage(double val){
        return NumberFormat.getPercentInstance().format(val);
    }
   
    public static String getPercentage(int actualVal, int totalVal){
        return getPercentage((1.0*actualVal)/(1.0*totalVal));
    }   



 public static String getAlpha(String str) {
        if ((str == null) || isEmpty(str)) {
            return null;
        }

        StringBuffer buf = new StringBuffer(str.length());
        int sz = str.length();

        for (int i = 0; i < sz; i++) {
            if (Character.isLetter(str.charAt(i)) == true) {
                buf.append(str.charAt(i));
            }
        }

        return buf.toString();
    }


public static String getAlphaNumeric(String str) {
        if ((str == null) || isEmpty(str)) {
            return null;
        }

        StringBuffer buf = new StringBuffer(str.length());
        int sz = str.length();

        for (int i = 0; i < sz; i++) {
            if (Character.isLetterOrDigit(str.charAt(i)) == true) {
                buf.append(str.charAt(i));
            }
        }

        return buf.toString();
    }

 public static String getAlphaNumericStar(String str) {
        if ((str == null) || isEmpty(str)) {
            return null;
        }

        StringBuffer buf = new StringBuffer(str.length());
        int sz = str.length();

        for (int i = 0; i < sz; i++) {
            if ((Character.isLetterOrDigit(str.charAt(i)) == true) ||
                    (str.charAt(i) == '*')) {
                buf.append(str.charAt(i));
            }
        }

        return buf.toString();
    }

How to genarate Random values/Password in java



import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.Random;

public class RandomTest {

    public static void main(String argv[]) throws IOException {
        System.out.println(" getRandomPassword :"+getRandomPassword(20));

  }
        public static String getRandomPassword(int maxLength)throws IOException {
            String[] array = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0"};
             Random generator = new Random();
            StringBuffer sbuff = new StringBuffer();
            int d = 0;
            for(int i = 0; i < maxLength ; i++){
                  d = generator.nextInt(array.length);
                  sbuff.append(array[d]);
            }
            return sbuff.toString();
      }

}

Monday, May 21, 2012

how to call a method from command prompt in java

package com.jnet.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException {
        Test t=new Test();
        Method m = t.getClass().getMethod(args[0], new Class[] {});
        Object ret = m.invoke(t, new Object[] {});
        System.out.println((String)ret);
            }

    public static String m1(){
        return "sudheer";
    }
}

Saturday, May 19, 2012

A pseudo attribute name is expected solution

A pseudo attribute name is expected

If you get the message "A pseudo attribute name is expected" when validating your xml file you have probably done a mistake typing your encoding declaration.
The culprit code should look like :

1.<?xml version="1.0" encoding="UTF-8">
instead of
1.<?xml version="1.0" encoding="UTF-8"?>
There is a very small difference and it has nothing to do with the description of the error message. You have forgotten a question mark at the end of your encoding declaration.