Monday, July 20, 2009

Java Script :Trim and MoneyOnly

function trimThis(value1)
{
value1= value1.replace(/^\s+|\s+$/, '');
return value1;
}



function moneyonly(e)
{
var unicode=e.charCode? e.charCode : e.keyCode;
var st = document.getElementById("amount").value;
//alert(unicode)
if (unicode== 8)
{
return true;
//if the key isn't the backspace key (which we should allow)
}
else if(unicode == 46)
{

if(st.toString().indexOf(".",0) != -1)
{
return false;
}

}
else if (unicode<48||unicode>57) //if not a number
{

return false //disable key press
}
else if(st.toString().indexOf(".",0) != -1)
{
if(st.toString().length - st.toString().indexOf(".",0) > 2)
{
return false;
}
}

return true;
}

Saturday, May 23, 2009

AJAX SIMPLE EXAMPLE


function XYZ() /////call this function
{
url = "Dog" //Servlet Name
if(window.XMLHttpRequest)//non IE
{
req = new XMLHttpRequest();
try
{
req.open("GET",url,true);
req.onreadystatechange=all_list_show;
}
catch(e)
{
alert("cannot connect to server");
}
req.send(null);
}
else if(window.ActiveXObject)//IE
{
try
{ req=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{ req=new ActiveXObject("Microsoft.XMLHTTP");
}
if(req)
{ req.open("GET",url,true);
req.onreadystatechange=all_list_show;
req.send(null);
}
}
}
function XYZOUT()
{ if(req.readyState==4)
{ if(req.status==200)
{
document.getElementById('div_id').innerHTML=req.responseText;
}
}
}

Saturday, May 2, 2009

Adding Files in Jar Files

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

public class CreateJarFile {
  public static int BUFFER_SIZE = 10240;
  protected  void createJarArchive(File archiveFile, File[] tobeJared) {
    try {
      byte buffer[] = new byte[BUFFER_SIZE];
      // Open archive file
      FileOutputStream stream = new FileOutputStream(archiveFile);
      JarOutputStream out = new JarOutputStream(stream, new Manifest());

      for (int i = 0; i <>
        if (tobeJared[i] == null || !tobeJared[i].exists()
            || tobeJared[i].isDirectory())
          continue; // Just in case...
        System.out.println("Adding " + tobeJared[i].getName());

        // Add archive entry
        JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
        jarAdd.setTime(tobeJared[i].lastModified());
        out.putNextEntry(jarAdd);

        // Write file to archive
        FileInputStream in = new FileInputStream(tobeJared[i]);
        while (true) {
          int nRead = in.read(buffer, 0, buffer.length);
          if (nRead <= 0)
            break;
          out.write(buffer, 0, nRead);
        }
        in.close();
      }

      out.close();
      stream.close();
      System.out.println("Adding completed OK");
    } catch (Exception ex) {
      ex.printStackTrace();
      System.out.println("Error: " + ex.getMessage());
    }
  }
  public static void main(String args[]) {

   File f=new File("c:\\abc");
   File af=new File("c:\\xyz.jar");
   new createJarFile().createJarArchive(af,f.listFiles());
    
}
  
}

Monday, April 27, 2009

Add Tray Icon in System Tray

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;

/////////////////////////////////////////////////////////

PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
MenuItem restart = new MenuItem("Restart");
defaultItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
});
restart.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
System.out.println("Restart...");
System.gc();
log.setVisible(true);
}
});
popup.add(defaultItem);
popup.add(restart);



SystemTray tray = SystemTray.getSystemTray();
Image ico = Toolkit.getDefaultToolkit().getImage("services_gen.jpg");
tric=new TrayIcon(ico, "NEWS UPDATE",popup);

try
{
tray.add(tric);
tric.displayMessage("anil", "Hiiiiiii",MessageType.INFO);
}
catch (AWTException ex)
{
System.out.println(ex);
}

Friday, April 24, 2009

Get Page Content from Urls

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
class xyz_abc_anil{

 public String getUrl_Content(String http_path) {
        String page_content = "";
        try {
            URL url = new URL(http_path);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line_content = "";
            while ((line_content = br.readLine()) != null) {
                page_content = page_content + line_content;
            }
        } catch (Exception e) {
        }
        return page_content;
    }
}

Type 4 Sql Connection

 public  Connection getconnection()
    {
        Connection conn=null;
        try
        {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String machinename="comp name";    
String DatabaseName="test";
String dsn="jdbc:sqlserver://"+machinename+":1433;databasename="+databasename;
            conn = DriverManager.getConnection(dsn,"username","password");
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
        return conn;
    }