Monday, June 11, 2012

Numbers Only logic


Hi,

Usage : onkeypress ="return  numbersonly(event);"

function numbersonly(e) {
 var keynum
 var keychar
 var numcheck


 if(window.event) { // IE
  var code = window.event;
    keynum = code.keyCode
 } else if(e.which) {// Netscape/Firefox/Opera
  keynum = e.which
 }

 if (keynum == 99) return true;
 if (keynum == 118) return true;
 //if (keynum == 42) return true;
 if (keynum == 8 || keynum == 13 ) return true;
 if (keynum < 48 || keynum > 57 ) return false;
}

Saturday, June 9, 2012

How to get the session contents of a web applications.

Hi Guys,

            Here is a sample code which is used to get the session contents of any web container.

<!-- Displays the session content in any of the web application.
@author  : Rajesh
-->
<body >
<style>
    body{ font-family:Tahoma, sans-serif, Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0px; padding:0px; background-color:#A7C71A; color:#FFFFFF;}
    .actionButton{border:1px solid #d7e3ef;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;background: -moz-linear-gradient(top,  #fbbc79,  #f98f15);background: -webkit-gradient(linear, left top, left bottom, from(#fbbc79), to(#f98f15));filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fbbc79', endColorstr='#f98f15');display:inline-block;color:#333;padding:4px 10px;font-weight:700;text-shadow:1px 1px #f1f1f1;text-decoration:none;margin:2px;font-size:13px;}
</style>
<center>
    <u>
        <h2>
        </h2>
    </u>
    <table><tr><td>
                <form name="frm1" method="POST" action="./test.jsp?clearsession=true">
                    <input class="actionButton" type=submit value="Clear Session">
                </form>

            </td>
            <td>
                <form name="frm2" method="POST" action="./test.jsp">
                    <input class="actionButton" type=submit value="Get Session">
                </form>
            </td>
        </tr>
    </table>


</center>
<%@page import= "java.util.*" %>
<%
            if (request.getParameter("clearsession") != null && request.getParameter("clearsession").equals("true")) {
                session.invalidate();
                return;
            }
            String[] keys = (String[]) session.getValueNames();
            int i = 1;
%>
<table border="1" align="center" cellpadding="4" cellspacing="4">
    <tr>
        <th> Sl.No </th>
        <th> Attribute Name </th>
        <th> Attribute Value </th>
    </tr>
    <% for (String key : keys) {
    %>
    <tr>
        <td> <%=i%></td>
        <td><%=key%></td>
        <td><%=session.getAttribute(key)%></td>
    </tr>
    <%
                    i++;
                }
    %>
</table>

Thanks,

Rajesh Kumar Yuvaraj

Thursday, May 24, 2012

Tuesday, May 22, 2012

Tuesday, May 1, 2012

JAVA XML Parser Program

Hi Guys,
            Here is a sample Java XML Perser Program.


Knowledge Base: http://www.rgagnon.com/javadetails/java-0573.html


import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;

public class ParseXMLString {

  public static void main(String arg[]) {
     String xmlRecords =
      "<data>" +
      " <employee>" +
      "   <name>John</name>" +
      "   <title>Manager</title>" +
      " </employee>" +
      " <employee>" +
      "   <name>Sara</name>" +
      "   <title>Clerk</title>" +
      " </employee>" +
      "</data>";

    try {
        DocumentBuilderFactory dbf =
            DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlRecords));

        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("employee");

        // iterate the employees
        for (int i = 0; i < nodes.getLength(); i++) {
           Element element = (Element) nodes.item(i);

           NodeList name = element.getElementsByTagName("name");
           Element line = (Element) name.item(0);
           System.out.println("Name: " + getCharacterDataFromElement(line));

           NodeList title = element.getElementsByTagName("title");
           line = (Element) title.item(0);
           System.out.println("Title: " + getCharacterDataFromElement(line));
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    /*
    output :
        Name: John
        Title: Manager
        Name: Sara
        Title: Clerk
    */    
    
  }

  public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
       CharacterData cd = (CharacterData) child;
       return cd.getData();
    }
    return "?";
  }
}