Saturday, 31 March 2012


 AJAX with JAVA


Description:
Everybody till now must have atleast heard about AJAX (Asynchronous JavaScript And XML). This example will give you an idea about how you can implement simple AJAX interaction in your web application.
Purpose of using AJAX
The main purpose of using AJAX is to asynchronously get the information from the server and update only that area of the web page where this information fetched needs to be displayed, avoiding refresh of the entire page. The other advantage of this is, the web page can hold minimal required data and retrieve rest as needed based of events.
How is all this possible.
Its possible by writing a JavaScript code in your web application, which uses XMLHttpRequest object to communicate with the server and get the data asynchronously.

Following figure shows the interaction.


The first step is to create and configure the XMLHttpRequest object.
Currently there are two implementations of this object, ActiveXObject is Internet Explorer specific, and XMLHttpRequest works with other browsers. So a check is made before creating the object, like this,

var httpRequest;
if (window.ActiveXObject) // for IE
{
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) // for other browsers
{
    httpRequest = new XMLHttpRequest();
}
Now configure the object by setting the HTTP method, GET or POST, the URL of the server side element, like a servlet or cgi script, which this object will communicate with, and the third parameter is a boolean value which decides whether the interaction should be synchronous or asynchronous, where true means asynchronous.

httpRequest.open("GET", url, true);
Then set the name of the JavaScript function, which will handle the callback from the server side element.

httpRequest.onreadystatechange = function() {processRequest(); } ;
Now make the call,

httpRequest.send(null);
Second step is to handle the response from the server which is always in XML form.
The server element can return the actual data in XML form or the formatted HTML code. This processing is done in the JavaScript function, which is specified as the callback function. The first thing to do in this method is to check the XMLHttpRequest object's readyState, value 4 indicates that the call is complete. Then check for the status, which is the HTTP status code, value 200 means HTTP is successful. Following is how the method looks,

function processRequest()
{
    if (httpRequest.readyState == 4)
    {
        if(httpRequest.status == 200)
        {
            //process the response
        }
        else
        {
            alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText);
        }
    }      
}
The third step is to process the response received.
The response is stored in responseText of the XMLHttpRequest object. The response is always in the XML form and the object representation of this XML is stored in responseXML of the XMLHttpRequest object. This XML can be parsed in the JavaScript itself using the DOM API to obtain the actual data.
Following code shows how this is done.

//The method getElementsByTagName, gets the element defined by the given tag
var profileXML = httpRequest.responseXML.getElementsByTagName("Profile")[0];

//The node value will give you actual data
var profileText = profileXML.childNodes[0].nodeValue;
Once we get the actual data, now the last step is to update the HTML.
The way to do that is by modifying the DOM (Document Object Model) of the HTML page. This can be done within JavaScript using the DOM API. JavaScript can get access to any element in the HTML DOM and modify it after the HML is loaded. This ability to dynamically modify the HTML DOM object within JavaScript plays very important role in AJAX interaction. The document.getElementById(id) method is used to get reference of the element in DOM. Where id, is the ID attribute of the element you want to modify. In this example the element is DIV with the ID attribute "profileSection".
Following code shows how to modify this DOM element with the data received,

//Create the Text Node with the data received
var profileBody = document.createTextNode(profileText);
                    
//Get the reference of the DIV in the HTML DOM by passing the ID
var profileSection = document.getElementById("profileSection");
         
//Check if the TextNode already exist
if(profileSection.childNodes[0])
{
    //If yes then replace the existing node with the new one
    profileSection.replaceChild(profileBody, profileSection.childNodes[0]);
}
else
{
    //If not then append the new Text node
    profileSection.appendChild(profileBody);
}
How does web page interact with this JavaScript
To tie this interaction with your web page, you need to first identify the part of your web page which will be updated dynamically and mark it with DIV tag and provide it an id, like,

<div id="profileSection">

</div>

Identify what event will update the marked area, like clicking on a link, mouseover a image etc. trap this events using JavaScript and call the JavaScript function which creates the XMLHttpRequest object.
What does server side element need to take care of?
While sending the response back the server should set the Content-Type to text/xml, since the XMLHttpRequest object will process this type of request. Also you can set the Cache-Control to no- cache to avoid the browser caching the response locally. The server should always send back data in XML format. The data should be a valid XML format and parse able by the JavaScript. If your data contains characters not friendly with the XML parsers, use the CDATA section in your XML.
Example Code:
This example shows a combo box filled with all JavaReference author names, and shows their profile below it, when the name is selected using AJAX.
The JSP page, javareference_authors.jsp, marks the area where author's profile will be displayed using DIV with id profileSection. The jsp page interacts with the AuthorsBean and fills the combo box with author names.
The ONCHANGE event of the combox calls the getProfile JavaScript function that uses XMLHttpRequest object to call the servlet GetAuthorsProfile with author name as parameter.
The callback function processRequest, then updates the DIV profileSection with the response received from the Servlet.


JSP Page : javareference_authors.jsp 
--------------------------------------- 

<HTML> 
<HEAD> 
<TITLE> 
    AJAX DEMO : Getting JavaReference Author Profile using Ajax interaction 
</TITLE> 
</HEAD> 

<%@ page import="java.util.*" %> 
<jsp:useBean id="AuthorsBean" scope="session" class="jr.beans.common.AuthorsBean" /> 

<script type="text/javascript"> 
    var httpRequest; 
   
   /** 
    * This method is called when the author is selected 
    * It creates XMLHttpRequest object to communicate with the  
    * servlet  
    */ 
    function getProfile(authorSelected) 
    { 
        var url = 'http://www.javareference.com/exampledemo/GetAuthorsProfile?author=' + authorSelected; 

        if (window.ActiveXObject) 
        { 
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        else if (window.XMLHttpRequest) 
        { 
            httpRequest = new XMLHttpRequest(); 
        } 
         
        httpRequest.open("GET", url, true); 
        httpRequest.onreadystatechange = function() {processRequest(); } ; 
        httpRequest.send(null); 
   } 
   
   /** 
    * This is the call back method 
    * If the call is completed when the readyState is 4 
    * and if the HTTP is successfull when the status is 200 
    * update the profileSection DIV 
    */ 
    function processRequest() 
    { 
        if (httpRequest.readyState == 4) 
        { 
            if(httpRequest.status == 200) 
            { 
                //get the XML send by the servlet 
                var profileXML = httpRequest.responseXML.getElementsByTagName("Profile")[0]; 
                 
                //Update the HTML 
                updateHTML(profileXML); 
            } 
            else 
            { 
                alert("Error loading page\n"+ httpRequest.status +":"+ httpRequest.statusText); 
            } 
        } 
    } 
        
   /** 
    * This function parses the XML and updates the  
    * HTML DOM by creating a new text node is not present 
    * or replacing the existing text node. 
    */ 
    function updateHTML(profileXML) 
    { 
        //The node valuse will give actual data 
        var profileText = profileXML.childNodes[0].nodeValue; 
            
        //Create the Text Node with the data received 
        var profileBody = document.createTextNode(profileText); 
                       
        //Get the reference of the DIV in the HTML DOM by passing the ID 
        var profileSection = document.getElementById("profileSection"); 
            
        //Check if the TextNode already exist 
        if(profileSection.childNodes[0]) 
        { 
            //If yes then replace the existing node with the new one 
            profileSection.replaceChild(profileBody, profileSection.childNodes[0]); 
        } 
        else 
        { 
            //If not then append the new Text node 
            profileSection.appendChild(profileBody); 
        }        
    } 
        
</script> 

<BODY> 
<% 
    //get author list 
    List authors = AuthorsBean.getAllAuthors(); 
%> 

<TABLE align=left border=0 cellPadding=3 cellSpacing=1 width="100%" > 
    <TR> 
        <TD align="center"> 
            <STRONG>Getting JavaReference Author Profile using Ajax interaction.</STRONG> 
            <br> 
        </TD> 
    </TR> 
    <TR bgColor="#C6D3E7"> 
        <TD> 
            <SELECT id=authors name=authorComboBox ONCHANGE="getProfile(this.options[this.selectedIndex].value)">  
             <% Iterator it = authors.iterator();                 
            while(it.hasNext()) 
            { 
                String authorName = (String)(it.next()); %> 
                     
                <OPTION value='<%=authorName%>' ><%=authorName%></OPTION> 
            <% 
            }%>       
            </SELECT> 
            &nbsp;&nbsp;&nbsp;<<<&nbsp;Select Author 
        </TD> 
    </TR> 
    <TR bgColor="#FFD0B1"> 
        <TD> 
            <div id="profileSection"> 
                <br><br> 
            <div> 
             
            <script type="text/javascript"> 
                getProfile(authorComboBox.options[authorComboBox.selectedIndex].value); 
            </script> 
             
        </TD> 
    </TR> 
</TABLE>     

</BODY> 
</HTML> 


---------------------------------------------------------------------- 

Servlet : GetAuthorsProfile.java 
---------------------------------- 

/*  
* This example is from javareference.com  
* for more information visit,  
* http://www.javareference.com  
*/ 

package jr.exampledemo; 
  
import jr.beans.common.AuthorsBean; 

import java.util.*; 
import java.io.*;  
import javax.servlet.http.*; 

/** 
 * This servlet handles the get author profile 
 * ajax request. It gets the author name as parameter 
 * queries the AuthorBean for the author profile and 
 * returns the profile back. 
 *  
 * @author Rahul Sapkal(rahul@javareference.com) 
 */ 
public class GetAuthorsProfile extends HttpServlet  
{     
    /** 
     * This method is overriden from the base class to handle the 
     * get request.  
     */ 
    protected void doGet(HttpServletRequest requestObj, HttpServletResponse responseObj) 
                   throws IOException 
    { 
        //set the content type 
        responseObj.setContentType("text/xml"); 
         
        responseObj.setHeader("Cache-Control", "no-cache"); 
         
        //get the PrintWriter object to write the html page 
        PrintWriter writer = responseObj.getWriter(); 
         
        //get parameters store into the hashmap 
        HashMap paramsMap = new HashMap(); 
        Enumeration paramEnum = requestObj.getParameterNames(); 
        while(paramEnum.hasMoreElements()) 
        { 
            String paramName = (String)(paramEnum.nextElement()); 
            paramsMap.put(paramName, requestObj.getParameter(paramName)); 
        } 
        //get the author name passed 
        String authorName= (String)paramsMap.get("author"); 
         
        //creating the author bean 
        AuthorsBean authBean = new AuthorsBean(); 
         
        //get the author profile by quering the AuthorsBean by passing author name 
         writer.println("<Profile><![CDATA[" + authBean.getAuthorProfile(authorName) + "]]></Profile>"); 
         
        //close the write 
        writer.close();                     
    }         
}

Tuesday, 27 March 2012

                               Liferay UI
link: http://alloy.liferay.com/demos.php


Creating the service.xml file
Create a file called service.xml in the WEB-INF folder of your project. This file will contain your table definitions. You'll populate this file with all the information Service Builder needs to generate the SQL for the table—for all the databases Liferay supports as well as database persistence objects you can use in your Java code.
We'll use the simplest of the tables as our example, which is the Product table. The following listing defines the table for Service Builder.
Listing 1 Defining the Product table using Service Builder

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.inkwell.internet.productregistration"> 1
<author>Rich Sezov</author> 2
<namespace>PR</namespace> 3
<entity name="PRProduct" local-service="true" remote-service="false"> 4
<column name="productId" type="long" primary="true" /> 5
<column name="productName" type="String" /> 6
<column name="serialNumber" type="String" />
<column name="companyId" type="long" /> 7
<column name="groupId" type="long" />
<order by="asc"> 8
<order-column name="productName" />
</order>
<finder name="G_PN" return-type="Collection"> 9
<finder-column name="groupId" />
<finder-column name="productName" />
</finder>
<finder name="GroupId" return-type="Collection">
<finder-column name="groupId" />
</finder>
<finder name="CompanyId" return-type="Collection">
<finder-column name="companyId" />
</finder>
</entity>
</service-builder>

#1 Java package for code
#2 Author for JavaDoc
#3 Namespace for tables
#4 Name of entity
#5 Primary key
#6 Additional fields
#7 Foreign Keys
#8 Order data is returned
#9 Finder methods



Liferay Alloy UI Calendar DatePicker and DatePickerSelect
2/15/12 8:30 PM
This is a simple tutorial to create an input textbox with the alloy ui calendar datepicker.
Simple DatePicker:
1. Include the below taglib directive to your jsp
<%@ taglib prefix="aui" uri="http://liferay.com/tld/aui" %>
2. Wrap your date field input box around a div
<h2>Start Date:<h2>                     

<div class="aui-datepicker aui-helper-clearfix" id="#<portlet:namespace />startDatePicker">

 <input type="text" name="startDate" id="<portlet:namespace />startDate" size="30" />

</div>
3. Add the aui script to your jsp
<aui:script>
    AUI().use('aui-datepicker', function(A) {
    var simpleDatepicker1 = new A.DatePicker({
        trigger: '#<portlet:namespace />startDate',
    })
    .render('##<portlet:namespace />startDatePicker');
</aui:script>
For more configuration options with the datepicker check here
4. In your portlet render or action method add the below code to get the date from the request parameter:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = df.parse(actionRequest.getParameter("startDate"));
5. Build and deploy
DatePickerSelect:
1. Add the below html code.

  <div class="aui-datepicker-example aui-helper-clearfix" id="staticDatePicker">
    <h2>Start Date: from HTML Markup</h2>
    <div class="aui-datepicker aui-datepicker-display aui-helper-clearfix" id="datePickerBB">
        <div class="aui-datepicker-content" id="srcNode">
            <div class="aui-datepicker-select-
wrapper">
                <select id="yearNode" class="custom-field aui-datepicker-year">
                    <option value="2012">2012</option>
                </select>
                <select id="monthNode" class="custom-field aui-datepicker-month">
                    <option value="0">January</option>
                    <option value="1">February</option>
                    <option value="2">March</option>
                    <option value="3">April</option>
                    <option value="4">May</option>
                    <option value="5" selected>June</option>
                    <option value="6">July</option>
                    <option value="7">August</option>
                    <option value="8">September</option>
                    <option value="9">October</option>
                    <option value="10">November</option>
                    <option value="11">December</option>
                </select>
                <select id="dayNode" class="custom-field aui-datepicker-day">
                    <option value="9">9</option>
                </select>
            </div>
            <div class="aui-datepicker-button-wrapper">
                <button type="button" id="buttonTest" class="aui-buttonitem-content aui-widget aui-component aui-buttonitem aui-state-default aui-buttonitem-icon-only">
                    <span class="aui-buttonitem-icon aui-icon aui-icon-calendar"></span>
                </button>
            </div>
        </div>
    </div>
</div>
2. Add the aui script to your jsp

<aui:script>

    AUI().use('aui-datepicker', function(A) {

    var datepicker2 = new A.DatePickerSelect({
        srcNode: '#srcNode',
        contentBox: '#srcNode',
        boundingBox: '#datePickerBB',

        appendOrder: [ 'y', 'm', 'd' ],
        calendar: {
            dates: [ '10/10/2010' ],
            dateFormat: '%m/%d/%y'
        },

        // dayNode: '#dayNode',
        // monthNode: '#monthNode',
        // yearNode: '#yearNode',

        // dayNodeName: 'dayNode',
        // monthNodeName: 'monthNode',
        // yearNodeName: 'yearNode',

        nullableDay: true,
        nullableMonth: true,
        nullableYear: true,

        // populateMonth: false,
        // populateDay: false,
        // populateYear: false,
        yearRange: [ 1980, (new Date).getFullYear() ]
    }).render();

});
</aui:script>

3. Build and deploy

Saturday, 17 March 2012

A Lesson In Psychology

¤ When A Person Laughs 2 Much Even On Stupid Things,

   Be Sure That Person Is Sad Deep Inside.

¤ When A Person Sleeps Alot, Be Sure Dat Person Is Lonely.


¤ When A Person Talks Less, And If He Talks,

   He Talks Fast Then It Means That Person Keeps Secrets.

¤ When Someone Can't Cry Then That Person Is Weak.


¤ When Someone Eats In Abnormal Way Then That Person Is In Tension.


¤ When Someone Cry On Little Things Then It Means 

   He Is Innocent & Soft Hearted.

¤ When Someone Gets Angry On Silly Or Small Things It Means He Is In Love.

So True, Try To See All These In Real Life, U Will Find All...

--------------------------------------------------------------------------------------------------

look back-get experiences
look forword-see hope
look arround-find reality
look within -find ur self

-------------------------------------------------------------------------------------------------

भूल कर तो देखो एक बार हमें ! 
जिंदगी की हर अदा तुमसे रूठ जाएगी !!
जब भी सोचोगे अपनों के बारे में ! 
तुम्हे हमारी याद जरुर आएगी!

तेरे होने पर खुद को तनहा समझू ! 
मैं बेवफा हूँ या तुझको बेवफा समझू !!
ज़ख्म भी देते हो मलहम भी लगाते हो ! 
ये तेरी आदत हैं या इसे तेरी अदा समझू!!

जीने के लिए जान जरुरी हैं !
हमारे लिए तो आप जरुरी हैं !!
मेरे चेहरे पे चाहे गम हो…..!
आपके चेहरे पे मुस्कान जरुरी हैं !!

बेवफा हैं दुनियाँ किसी का एतबार न करो !
हर पल देते हैं धोखा किसी से प्यार न करो !!
मीट जाओ उम्र भर तनहा जी कर….!
पर किसी के साथ आँखे चार न करो !!

फूलों को महकना सिखाया नहीं जाता !
काँटों को चुभना बताया नहीं जाता…!!
कोई बन जाता हैं खुद से अपना !
वर्ना हर किसी को अपना बनाया नहीं जाता !! 

इश्क ने इंसान को क्या बना दिया ,
किसी को कवी तो किसी को कातिल बना दिया .
दो फूल का बोज न उठा सकती थी मुमताज़ ,
और शाहजह ने उसपे ताज महल बना दिया . 

GREAT THOUGHTS

There Is Always 
A Little Truth Behind ''Mazak Kr Rha Tha"
A Little Emotion Behind " Mujhe Koi Fark Nhi Pdta"
A Little Pain Behind "Its Ok"
A Little Need Behind "Mujhe Akela Rehne Do"
A Deep Thought Behind "Pata Nhi"..
A Lot Of Words Behind"Silence".

So Try To Understand the Feelings always not the Words :)

Thursday, 8 March 2012

HI Friends i m arvind jaiswal from sitapur
near to lucknow my no.is 09910870895.
now a days i m working as software developer in indian petro group in delhi.