Tuesday, July 24, 2012

How to use Apache Axis2 for consuming a web service?

About the example:


In this example we will see how to use rather consume a web service using Apache Axis2. We have a Hello World web service which is already published, it accepts a String name and prints Hello name in return. To know more about how to publish a web service, click here.

Below are the steps which shows how to consume a web service using Apache Axis2

1. Download latest version of axis2 files from http://axis.apache.org/axis2/java/core/. (i.e. axis2-1.6.2-bin.zip).


2. Extract the folder (i.e. axis2-1.6.2) in your desire location. Lets assume as C:\axis2-1.6.2.

3. Add enviroment variable AXIS2_HOME and set the value "C:\axis2-1.6.2". Also make sure that JAVA_HOME is set in enviroment variables.

4. Now you can use wsdl2java.bat file from folder "C:\axis2-1.6.2\bin" to download web service stubs. Use command prompt and go to folder C:\axis2-1.6.2\bin and run below command.

   wsdl2java.bat -uri http://localhost:8080/SimpleWebservice/hellows?wsdl

   Note : Provide your wsdl URL in above command, you can also use XSD file instead of wsdl URL. Here we have already published a hello world web services. To know how to publish a simple hello world web service, click here.

   Please see the screen shot below.


Friday, July 6, 2012

Lucene 3.0 example - Indexing and searching database tables


Apache Lucene(TM) is a high-performance, full-featured text search engine library written entirely in Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform.
Apache Lucene is an open source project available for free download. To know more about Lucene , click here.

About the example:

Here is the simple java program which will create index files from the data which is fetched from database. And it will perform search from the created index files and display the results. We are using Lucene 3.0 and My SQL. The basics behind is very simple, we will fetch data from database using JDBC (you can use Hibernate or so accordingly) and create index files.

You can store database column(s) in the index files depending upon your requirement. If you want to perform search in one or two column only then no need to add all columns in index files. You can store that particular column and primary key, and perform search on that column and retrieve primary key and use it accordingly.

Creating index is simple and straight forward just add the filed and filed values in Document. Searching can be done in many ways as per requirement, if you want to perform search on one filed then you can use QueryParser, for searching multiple field you can use MultiFieldQueryParser. In query you can use wild card (e.g. DATA*), logical operators (e.g DATA1 OR DATA2) etc.

To run below example please add lucene-core-3.0.2.jar (For Lucene) and mysql-connector-java-5.1.5.jar (For JDBC - My SQL) in your application's classpath.
To download lucene-core-3.0.2.jar, click here.
How to install My SQL, click here.


Tuesday, July 3, 2012

Javascript : On scroll fill data at the end of the page

Other day while browsing facebook, i saw when we scroll to the end of the home page it fills more data without refreshing the whole page. Again when u reach to the end of the page it fetches more data and this continues. I didn't bother to check its upper limit, may be at some point of time it will go out of memory of the browser.

So lets see how this functionality can be achieved. Below javascript variables can be used to detect when user have reached to the end of the body.

  • document.body.scrollTop - scroll bar position
  • document.body.scrollHeight - height of the scroll bar
  • document.body.clientHeight - total height of the body

so if (scrollHeight - scrollTop) is equal to clientHeight you have reached to the end of the body. And obviously you can use various APIs like AJAX, DWR, Ext JS etc to load data without refreshing the whole page.

Below is the standalone tested HTML code, directly you can test this example no need to have any server. Please see the self explanatory html code.


<HTML>

<HEAD>
<TITLE>Scroll Example</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
divCount = 1;
function onScrollDiv()
{
if((document.body.scrollHeight - document.body.scrollTop) == document.body.clientHeight)
{
onLoadData();
}

return false;
}

function onLoadData()
{
previousCount = divCount;
divCount++;

//Get data using AJAX, DWR, Ext JS etc
htmlData = "<table width='50%' border='1'><tr><td height='200px' align='center'>"+new Date()+"</td></tr></table>";

document.getElementById("div"+previousCount).innerHTML = htmlData;

var newDiv = document.createElement("div");
newDiv.innerHTML = "<div id='div"+divCount+"'>Loading...</div>"
var previousDiv = document.getElementById("div"+previousCount);

insertAfter(newDiv,previousDiv.parentNode);
return false;
}

function insertAfter(new_node, existing_node) {

if (existing_node.nextSibling) {
existing_node.parentNode.insertBefore(new_node, existing_node.nextSibling);
} else {
existing_node.parentNode.appendChild(new_node);
}
}

//-->
</SCRIPT>
</HEAD>
<BODY onload="onLoadData();" onscroll="onScrollDiv();">
<center>
Simple Javascript example which fills data when you scroll at the end of the file same like Facebook
<br />
Resize the window so that scroll bar appears on the right hand side then you can test it.

<div>
<div id="div1"></div>
</div>

</center>
</BODY>
</HTML>

Sunday, July 1, 2012

Spring : MultiActionController Example

Lets create a webproject in My Eclipse (lets say MultiActionController). Now we will add spring Capabilities to it. Here we are using Spring 3.0


(Right click on project) -> My Eclipse -> Add Spring Capabilities..


Adding below libraries (as shown in below image)

  • Spring 3.0 Core Libraries
  • Spring 3.0 Web Libraries


Spring : SimpleFormController Example

Before understanding SimpleFormController you must understand its Spring bean lifecycle. Below diagram tries to explain you the flow of SimpleFormController. 

  • Whenever a request comes from browser it checks in deployment descriptor (i.e Web.xml) for its servlet and url mapping. 
  • Then request goes to its corresponding dispatcher-servlet.xml, The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request in our case its SimpleFormController.
  • The Controller process the request by calling the appropriate service methods i.e isFormSubmitted(). Here you can write your own logic of a form being submitted or not. Usually if a request object is empty its a first time request and it returns false. 
  • formBackingObject() method will be called regardless of form is submitted or not however formBackingObject() method will return FormBean Object to formView when form is not submitted and ModelAndView Object to successView when form is submitted. 
  • Then request goes to a ViewResolver to find the actual View to invoke. The View with the help of the FormBean or ModelAndView will render the result back to the user.



Lets create a webproject in My Eclipse (lets say SpringFormController). Now we will add spring Capabilities to it. Here we are using Spring 2.5


(Right click on project) -> My Eclipse -> Add Spring Capabilities..