File | Directory Monitoring Service

Log monitoring is prime requirement for any batch execution application. Often batch process get hung an we are not able to check if this happened.

To achieve this we need to have some sort of File/Directory Change Notification service is required, which should be able to detect any changes. One way of doing is to poll for file system but this is an in-efficient way of doing so.
One way to do so is to poll the file system looking for changes, but this approach is inefficient. It does not scale to applications that have hundreds of open files or directories to monitor.

Java provides java.nio.file package provides a file notification API called watch service API. This api provides a mechanism to register a directory with the watch service. While registering, you tell the service which events you are interested in:
  • File Creation
  • File deletion
  • File Modification
When Watchservice finds any such event it forwards to the regitered process.

Implementation of a simple DirectoryMonitor is as follows:
public class DirectoryWatcher {

 public static void doMonitor(Path path) {
  
  try {
   Boolean isFolder = (Boolean) Files.getAttribute(path,
     "basic:isDirectory", NOFOLLOW_LINKS);
   if (!isFolder) {
    throw new IllegalArgumentException("Path: " + path + " is not a folder");
   }
  } catch (IOException ioe) {
   // Folder does not exists
   ioe.printStackTrace();
  }
  
  System.out.println("Watching path: " + path);
  
  // We obtain the file system of the Path
  FileSystem fs = path.getFileSystem ();
  
  // We create the new WatchService using the new try() block
  try(WatchService service = fs.newWatchService()) {
   
   // We register the path to the service
   // We watch for creation events
   path.register(service, ENTRY_CREATE);
   
   // Start the infinite polling loop
   WatchKey key = null;
   while(true) {
    key = service.take();
    
    // Dequeueing events
    Kind kind = null;
    for(WatchEvent watchEvent : key.pollEvents()) {
     // Get the type of the event
     kind = watchEvent.kind();
     if (OVERFLOW == kind) {
      continue; //loop
     } else if (ENTRY_CREATE == kind) {
      // A new Path was created 
      Path newPath = ((WatchEvent) watchEvent).context();
      // Output
      System.out.println("New path created: " + newPath);
     }
    }
    
    if(!key.reset()) {
     break; //loop
    }
   }
   
  } catch(IOException ioe) {
   ioe.printStackTrace();
  } catch(InterruptedException ie) {
   ie.printStackTrace();
  }
  
 }

 public static void main(String[] args) throws IOException,
   InterruptedException {
  // Folder we are going to watch
  Path folder = Paths.get(System.getProperty("user.home"));
  doMontor(folder);
 }
}

Output

Watching path: /home/ram
New path created: a




Machine Learning | Introduction - 1


Machine Learning deals with the set of algorithm which can learn a pattern in different types of user beahviour and help developer to solve various problems:


  • Collaborative Filtering (Basket Analysis)
  • Classification (Auto Tagging, Spam Filtering)
  • Prediction
  • Pattern recognition (Clustering)
  • Text extraction (Named Entity Recognition)
We can distribute Machine Learning Algorithm in tow categories

  1. Supervised Learning
  2. Unsupervised Learning

Supervised Learning

In simple words Supervised learning means Learn By Examples. We have  prior knowledge about certain domain. Using Supervised Learning we can create a model from existing knowledge which helps us to classify new data. For example:

Spam Classification

We mark existing text as HAM or SPAM and generate a model using this model new text can be categorised in one of the category.

Un-Supervised Learning 

In unsupervised Learning we do not have any supervisor, rather algorithm tries to learn a mapping from existing data and applies to new data. For example:

Document Clustering

In document clustering we try to group similar documents like we have different types are getting posted to some site using this algorithm we can group them in politics, business, technology etc categories.

List of Algorithms

  • Classification
    • Logistic Regression (SGD)
    • Bayesian
    • Support Vector Machines (SVM) 
    • Hidden Markov Model
  • Clustering
    • Canopy Clustering 
    • K-Means Clustering 
    • Fuzzy K-Means 
    • Expectation Maximization 
    • Mean Shift Clustering 
    • Hierarchical Clustering 
    • Dirichlet Process Clustering 
    • Latent Dirichlet Allocation 
    • Spectral Clustering 
    • Minhash Clustering 
    • Top Down Clustering

Tools/Libraries

Mongo Cheat Sheet

These days NoSQL concept is hot. I am doing some POC on mongodb.

I am using java driver to access mongodb, to use same DAO pattern and ORM like interface I am using mongo-morphia library.

Quick access to mongo commands, you can go through mongo cheat sheet

JSP 2.0 EL and Functions

In my last project I need to call some utility methods from my JSP to manipulate model data. I didn't wanted to use JSP scriplets within the JSP, so the solution was to use utility method via JSP2.0 EL feature.

Approach I folowed was:
1. Declare function in a tag library descriptor.




xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">

Image Utility functions
Image Utility Tag Library
1.0
ImageUtility

getImagePath
com.til.hk.utils.CommonUtils
java.lang.String getImagePath(java.lang.String, java.lang.String)


getRelativeImagePath
com.til.hk.utils.CommonUtils
java.lang.String getImagePath(java.lang.String)




2. Store TLD under WEB-INF directory (/WEB-INF/tld/)
3. Specify the function's prefix and the library's Uniform Resource Identifier (URI) in JSP file

<%@ taglib uri="/WEB-INF/tld/ImageManip.tld" prefix="image"%>

Using Alert conditionally

In my last project I faced a lot of Bugs filed by QC team related to test Alert pop-ups coming which were introduced by developers for debugging and they forgot to remove those.

To get rid of this I used a wrapper function over alert() function which checks the value of a test variable if that is true then only alert() function will get called, this way I was able to control test alert pop-ups across the web application with single test variable.


DEBUG = false;
function prompt(text) {
if(DEBUG) {
alert(text);
}
}

Message Property Like functionality in Javascript

In my last project I was using JavaScript heavily which was using messages extensively.

Repetition of same kind of messages from multiple places was becoming unmanageable to handle that I have written a JavaScript class which provides Text message with respect to the message code provided.

This function works in two ways:
1. With code only (Messages.getText("error.text"))
2. With code and args (Messages.getText("error.invalid",args))
Here args is replacement for place holders like:
error.invalid = "Invalid input [1] for [0]"
So Invalid error message can be reused for multiple places where [1] will replace by value and [0] wil be replaced for Field name

For function overloading I have used addMethod - By John Resig as reference


function addMethod(object, name, fn){
var old = object[ name ];
if ( old )
object[ name ] = function(){
if ( fn.length == arguments.length )
return fn.apply( this, arguments );
else if ( typeof old == 'function' )
return old.apply( this, arguments );
};
else
object[ name ] = fn;
}

function MessageProvider() {
this.msg = new Object();
this.msg['error.Login'] = 'Please login to submit link';
this.msg['error.invalid'] = 'Invalid input [1] for [0] [0] is invalid';

addMethod(this, "getText", function(code){
return this.msg[code];
});
addMethod(this, "getText", function(code,args){
var text = this.msg[code];
for (var i = 0; i < args.length; i++) {
var re = new RegExp('\\[' + i + '\\]', 'g');
var dest = args[i];
//text = oldtext.replace(re,'’);
text = text.replace(re,dest);
}
return text;

});

}

var Messages = new MessageProvider();


Now to use centralized messaging in javascript you need to add this code to .js File and include in your html
Add all your messages to msg Object

like :



Conflicts faced While Load Testing

I was working on load testing of a web application last month, results were very conflicting when I was accessing application from browser response was very good but while executing script from Jmeter throughput was very less.

I had applied all possible optimizations like :
1. Query Caching
2. Output compression (mod_deflate)
3. Minify JS and CSS

but result was same.
I enabled logs to see compression ratio and realized that Jmeter was not sending accept-encoding HTTP header so Apache was not sending compressed result and that was affecting throughput.
After adding accept-encoding header throughput was as desired.

Using RemoveAllRows function with DIV based pattern

For quite long time we were not able to use DIV based HTML in our DWR based applications.

DWR supports only tables to remove and add rows for any data. Now if your HTML contains DIV based pattern you can not clear and re-populate rows, you need to convert the design to TABLE.

I have modified removeAllRows function frovided by util.js and achieved same for DIV
 
dwr.util.removeAllRowsDiv = function(ele,options) {
return;
ele = document.getElementById(ele);
if (!options) options = {};
if (!options.filter) options.filter = function() { return true; };
if (ele == null) return;

var child = ele.firstChild;
var next;
while (child != null) {
next = child.nextSibling;
if (options.filter(child)) {
ele.removeChild(child);
}
child = next;
}
};


So Now by providing your parrent DIV id you can and pattern div Id you can acheive the same result.

Secrets of implementing a Gtalk Bot

I have read an article on implementing a Gtalk bot. I found it intresting so, I thought to write the concepts behind Google Talk and implementing your own Google Talk Bot.

Google Talk (GTalk) is a free Windows and web-based instant messaging application offered by Google Inc .

Instant messaging between the Google Talk servers and its clients uses an open protocol, XMPP, Protocol.

XMPP (Extensible Messaging and Presence Protocol) is an open technology for instant messaging. The core technology behind XMPP was invented by Jeremie Miller in 1998, refined in the Jabber open-source community in 1999 and 2000, and formalized by the IETF in 2002 and 2003, resulting in publication of the XMPP RFCs in 200.

Google talk uses XMPP for authentication, presence and messaging so any client that supports XMPP can connect to the Google Talk service . Google Talk seervice is hosted at talk.google.com on 5222 port.

To write a Google Talk bot you need to implement XMPP client API of your choice in your choice of platform. For example SMACK is java XMPP client API.

Steps required to write a Gtalk Bot:

1. Download SMACK client API from
2. Extract smack.jar, smackx.jar, smackx-debug.jar
3. Write Gtalk Service Code.

Sending and Receiving Message using SMACK:
  

public class MyGtalkClient implements MessageListener {

public void processMessage(Chat chat,Message message) {
/*Callback method from MessageListener interface .
It is called when a message is received */

System.out.println("Received message: " + message.getBody());
}

public static void main(String [] args) throws XMPPException,IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

/*Login to GTalk service*/
ConnectionConfiguration config = new ConnectionConfiguration (
"talk.google.com",
5222,
"gmail.com");
XMPPConnection connection = new XMPPConnection(config);

connection.connect(); /* Connect to the XMPP server */

connection.login("gtalkid","gtalk pass");
/*Enter your username & password to login to the gtalk service */
Chat chat = connection.getChatManager().createChat(
your_friend_id",new MyGtalkClient());

System.out.println(" ****Welcome to MyGtalkClient**** ");
System.out.println("****Enter your message, one per line ."+
"To stop chat enter stop****");

while( !(msg=br.readLine()).equals("stop")) {
chat.sendMessage(msg); //Send the message
}

connection.disconnect() ; //Disconnect
}
}


This is a very basic example here your buddy is hard coded and you are initiating the chat.
I will write implementing a calculator bot in next part which will provide concepts of listening for a packet, process that packet and send result back to that user.

Reverse Ajax with Spring and DWR


Ajax is used heavily for web 2.0 applications to provide enhance user experience, which allows a web page to be updated without the need of full refresh. Normally this updated information is pulled from client side.
Javascript call is used to make an Ajax call to the server and received information is updated.
But there are scenarios when server decides to update information on client side e.g. Chat applications, server side processing status, user presence etc.


In these situations server side "PUSH" is required instead of Client side "PULL". There are three techniques used for this purpose:
  • Polling (the browser sends requests at regular interval)
  • Comet (long lived HTTP connection connection is kept open and server keeps replying)
  • Piggyback (server waits for client to make request and sends update with that request data)
Configuration:
1. Enable Reverse Ajax in DWR configuration
There are two ways of going this:

You can enable in DWR.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>

or if you are using DWR name spaces then you can provide entry in [Servlet Name]-servelt.xml
<dwr:controller id="dwrController" debug="true">
<dwr:config-param name="activeReverseAjaxEnabled" value="true" />
</dwr:controller>

2. Enable Reverse Ajax in JSP
dwr.engine.setActiveReverseAjax(true);

3. Now we need to get Browser sessions from server side and push content there:

WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();

Get current page from webContext

Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Get all other clients session

After getting these sessions you can push data from here using proxy interface
Util utilAll = new Util(sessions);
utilAll.addOptions("chatlog", messages, "text");

Above lines will add messages collection to all the browsers under "chatlog" element

Similarly you can execute scripts on browsers from server side.

ScriptBuffer script = new ScriptBuffer();
script.appendScript("receiveMessages(")
.appendData(messages)
.appendScript(");");

Above snippet will execute receiveMessages() javascript function on client side.

This way you can have full control on client side from server side. Chat application, Stock Ticker control applications, mail client can use this feature effectively.

Logging using Log4j

Logging is an important of any application. There are several APIs available majorly used are:
1. Apache Log4J
2. Java Logging (java.util.logging)

Logging is required to monitor the state of the running application, debug the application, ignorant developers heavily relies on System.out.println(), which is quite expensive in terms of resources and you dont have any way to manage these messages.

To control this we have different API's like mentioned above.

Using Log4J from Apache:
Log4j consists of 3 aspects: logger, appender and, layout relations between three can be expressed as "logger logs to an appender using a layout"

Each class in an application can have seperate logger or a common logger, now this logger should know where to send request for logging. This where are known as appenders which can be:
- FileAppender (flat file)
- JDBCAppender (database )
- Console Appender (Console)
- SMTPAppender (Email)
- JMSAppender (to remote JMS servers)
- SocketAppender (to remote server).
there are few more.

Now we need to decide what and how we want the out out of that log, this is decided by layout

Each class can have a logger as mentioned above similarly each logger should have log level. There are five different log levels (ordered):
- DEBUG
- INFO
- WARN
- ERROR
- FATAL

To configure a class with a logger, appender, layout and log level we need to use an external configuration file (log4j.properties is the default)

Sample configuratiion file is as:
--------------------------------
# Set root logger level to DEBUG and its only appender to CONSOLE.
log4j.rootLogger=INFO, CONSOLE

# A1 is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Change the level of messages for various packages.
log4j.logger.com.tj.cand.controller=WARN
log4j.logger.com.tj.cand.domain=WARN
log4j.logger.com.tj.cand.dao=DEBUG
--------------------------------

Usage:

Logger log = Logger.getLogger(MyController.class);
log.info("This is a logging message);




Why Spring Framework

Last year I had to shift my web application to some latest framework.
I decided to go for Spring Framework, EJB3 was also an option.

The reason why Spring was:

1. Spring was a well established Open Source
2. Spring provides all the configurations into XML where as EJB3.0 relies in annotations, in my scenario I am using WebSphere 6.1 which runs on JDK1.4 which doesn't support annotations.

3. Spring is a loosely coupled Framework You can decide your own stack. Whereas EJB is whole integrated package.

4. One major reason was You are not forced to use any Spring imports or rely on Spring classes. Dependency Injection combined with external meta-data helps to achieve this independence. So you can easily migrate. While in case of EJB 3.0 this is different.

5. One disadvantage of using annotations based EJB 3.0 is you need to change java source to incorporate any configurations whereas in case of spring you need to change XML file.

XML binding using JiBX

JiBX is a XML binding framework which binds XML to your java class.
XML binding is a 2 phase process:
1. JiBX uses binding definition to define the rule how conversion wil happen and modifies byte code of the class.
2. In phase 2 JiBX runtime does marshalling and un-marshalling of java objects to populate XML to java and java 2 XML.

Algorithm
  • Add following jar files to your lib directory from JiBX distribution
    1. bcel.jar (Byte Code Engineering Library used to change bytecode for the class)
    2. jibx-bind.jar (1,2 are used in phase I)
    3. jibx-run.jar
    4. jibx-extras.jar
    5. xpp3.jar (3,4,5 are used in phase II)
  • Modify java class using binding compiler (java -jar ../lib/jibx-bind.jar binding.xml)
  • Write code for marshalling and unmarshalling of java object

Example:



public void XML2Java2XML() {
try {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();

Object obj = uctx.unmarshalDocument
(new FileInputStream("customer.xml"), null);
Customer customer = (Customer)obj;
System.out.print(customer.toString());

IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(4);
mctx.marshalDocument(obj, "UTF-8", null,
new FileOutputStream("customer2.xml"));

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JiBXException e) {
e.printStackTrace();
}
}

Password Strength Meter

Almost every web site provides password strength meter.
To make a strong password it should contain:
  • Sufficient length
  • Mixed case
  • Combination of numbers and Special characters

My password strength meter works on same line:

If password is of minimum length and same case it is a weak passwd

If password is min length mixed case and number normal passwd

if password is min length mixed case number and special char it is medium password

if password is above min length and combination of number and special char, it is strong passwd

Client Side Validation

Validation is a very important aspect of any web application.We can validate user input in client side as well as server side. Client side validation is important since we dont want to send data to server and thenlet user know about the errors. But we can not rely only on client side validation since if Javascript is disabled on client side it will lead to invalid data on server.
So mix approach should be followed: validating data on server as well as client side.


I am using Spring Framework and Spring MVC, I have few options available:


  1. AJAX based validation
  2. Spring VLANG Validation
  3. Apache commons validation (Struts Validator Framework)

I have dropped idea of AJAX based validation since it will require to more Remote calls
Spring VLANG Validation is a good option but dependency is JDK 1.5 and we are still running on JDK 1.4.

Finally decided to go for Commons validator this is well proven and stable one.
I have also read blog from Matt where he appreciated commons validator.
I have used custom validation along with commons standard validation.

Steps followed by me to get commons validator configured with Spring are as follows:
Dependency:
1. Spring-commons-validator (Spring modules 0.8)
2. Commons validator

Step 1: Downloaded dependency jar files
Step 2: Added validation.xml, validation-rules.xml, validation-custom-rules.xml (For custom validators)
Step 3: Added validation specific entries to applicationContext-validation.xml
step 4: Added validator property to form controller entry in [context-root]-servlet.xml

My Validation.xml is as follows:

Validation.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd;
<form-validation>
<formset>
<form name="registerFormBean">
<field property="user.fname" depends="mask,maxlength">
<arg0 key="register.label.firstname"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/> <var><var-name>maxlength</var-name><var-value>5</var-value>
</var> <var> <var-name>mask</var-name> <var-value>^[a-zA-Z]*$</var-value> </var>
</field> <field property="user.email" depends="required,email">
<arg0 key="register.label.email"/>
</field> <
field property="user.password" depends="required,twofields">
<arg0 key="register.label.password" />
<arg1 key="register.label.verifypassword"/>
<var> <var-name>secondProperty</var-name>
<var-value>verifyPasswd</var-value>
</var> </field> <
field property="verifyPasswd" depends="required">
<arg0 key="register.label.verifypassword" />
</field>
<field property="user.location" depends="selectfield">
<arg0 key="register.label.location" />
<arg1 key="${var:maxsize}" name="selectfield" resource="false"/>
<var><var-name>maxsize</var-name><var-value>3</var-value></var>
</field>
</form>
</formset>
</form-validation>

Validation-rules-custom

My custom validation rules file is as follows:

<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd; <form-validation>
<global>


<validator name="twofields" classname="com.tj.cand.utils.ValidationUtil" method="validateTwoFields"

methodParams="java.lang.Object,

org.apache.commons.validator.ValidatorAction,

org.apache.commons.validator.Field,

org.springframework.validation.Errors"

depends="required" msg="errors.twofields">

<javascript><!

[CDATA[ function validateTwoFields(form) {

var bValid = true;

var focusField = null;

var i = 0;

var fields = new Array();

oTwoFields = new twofields();

for (x in oTwoFields) {

var field = form[oTwoFields[x][0]];

var secondField = form[oTwoFields[x][2]("secondProperty")];

if (field.type == 'text' field.type == 'textarea' field.type == 'select-one' field.type == 'radio' field.type == 'password') {

var value; var secondValue;

// get field's value

if (field.type == "select-one") { var si = field.selectedIndex; value = field.options[si].value; secondValue = secondField.options[si].value; } else { value = field.value; secondValue = secondField.value; } if (value != secondValue) { if (i == 0) { focusField = field; } fields[i++] = oTwoFields[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> <validator name="selectfield" classname="com.tj.cand.utils.ValidationUtil" method="validateSelectField" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.springframework.validation.Errors" depends="" msg="errors.multiselect"> <javascript><![CDATA[ function validateSelectField(form) { var bValid = true; var focusField = null; var i = 0; var fields = new Array(); oSelectFields = new selectfield(); for (x in oSelectFields) { var field = form[oSelectFields[x][0]]; if (field.type == 'select-multiple') { var iMax = parseInt(oSelectFields[x][2]("maxsize"));
if ((field.options.length == 0) (field.options.length > iMax)) { if (i == 0) { focusField = field; } fields[i++] = oSelectFields[x][1]; bValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('\n')); } return bValid; }]]> </javascript> </validator> </global>
</form-validation>

Installing DBD::MySql

I had spent last two days trying to install DBD::MySql version-4.005 for mySql 5.1-22.0 rc.

After a lot of permutation and combination finally able to get it done. I have seen a lot of discussion forums but not able to get the root cause of the problem finally I figured out that it was related with MySql Devel rpm and static linking of My sql Client Library.

This is a known issue with DBD::MySql. It fails if you try linking MySql Client library Dynamically.

The steps I followed after going through DBD::MySql documentation are as follows:

1. Install Mysql-Client rpm
2. Install MySql-Devel Library
3. Install MySql Shared Libraries

  • MySQL-client-community-5.1.22-0.rhel
  • MySQL-devel-5.1.22-0.glibc23
  • MySQL-shared-compat-5.1.22-0.rhel4
Installing DBD::MySql

1. tar xvzf DBD-mysql-.tar.gz
2. cd DBD-mysql-
3. cp /usr/lib/mysql/*.a /tmp/mysql-static (to link libmysql.client.a statically)
4. perl Makefile.PL --libs="-L/tmp/mysql-static -lmysqlclient" --testuser=user --testpassword=pass
5. make
6. make test
7. make install

All tests passed successfully.

Resources:

AJAXing with Spring using DWR

Recently I have an opportunity to work with Spring Framework. With Spring I have used following technologies:

1. Spring MVC
2. Ajax - DWR
3. Hibernate
4. Tiles
5. Acegi Security

It was really a nice experience working with Spring and other supporting technologies.

I found a littlet bit problem in getting DWR 2.0 working with Spring 2.0.

Approach that worked for me as follows:

1. Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!-- Listner Config-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- Ajax DWR config ends -->
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<servlet>

2. Application-Context.xml

<!-- DWR Configurations -->
<dwr:controller id="dwrController" debug="true"/>
<!-- Configure DWR handlers -->

<bean id="dwrUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true"/>
<property name="mappings">
<props>
<prop key="/dwr/**/*">dwrController</prop> </props> </property>
</bean>
<!-- Configure Convertor -->

<dwr:configuration>
<dwr:convert type="bean" class="com.app.domain.User" />
</dwr:configuration>

<!-- Configure Ajax controller -->

<bean id="ajaxController" class="com.app.controller.dwr.AjaxController">
<dwr:remote javascript="UserList">
<dwr:include method="getUser"/>
<dwr:include method="updateUser"/>
</dwr:remote>
<property name="userDao" ref="userDao"/>
</bean>

3. JSP Page

Add following lines to jsp page:
<script type='text/javascript' src='/spring/dwr/interface/UserList.js'></script>
<script type='text/javascript' src='/spring/dwr/engine.js'></script>
<script type='text/javascript' src='/spring/dwr/util.js'></script>

Define call back javascript function and attach that to some event

<script type="text/javascript">
function editUser(id) {
UserList.getUser(id,function(array){DWRUtil.setValue(fname,array.fname); });
}
</script>

Finally deploy the application and restart the server and here is your DWR wroking...

To test it you can use: http://localhost:8080/spring/dwr/index.html

Resources: