Getting Beanclass object using dwr

Tuesday, January 4, 2011 Posted by Jagadeesh Manne
this example to show how to get bean object in jsp page and how to display values of that bean object
Creating beanClass
package mybean;

 * @author Jagadeesh Kumar
public class MyBeanClass {

   private String firstName ="";
   private String lastName="";

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;


Creating JavaClass
package mypack;

import mybean.MyBeanClass;

public class MyJavaClass
    public MyBeanClass getDetails()
        MyBeanClass mbobject = new MyBeanClass();
        return mbobject;
    //we will call this method
    //in client side using javascript class
    //here we are returning bean object. so we
    //can use bean properties in jsp page
creating dwr.xml
<create creator="new" javascript="MyJavaScriptClass">
<!-- here MyJavaScriptClass is dynamic javascript class generated by dwr-->
<param name="class" value="mypack.MyJavaClass"/>
<!-- now dwrservlet convert mypack.MyJavaClass to we can call methods of mypack.MyJavaClass using MyJavaScritpClass-->
<!-- now we have to convert our bean class using convertor type bean and we have to give bean class in match. if you put "mybean.*" it will convert all the bean class in that package. if you use "*" it will convert all the bean class in that application-->
<convert converter="bean" match="mybean.MyBeanClass"/>
Creating web.xml
creating jsp page
Document   : index
Created on : Jan 2, 2011, 7:37:30 AM
Author     : Jagadeesh
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dwr Example</title>
<!-- You have to include these two JavaScript files from DWR -->
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<!-- This JavaScript file is generated specifically for your application -->
<!-- in dwr.xml we have converted MyJavaClass to MyJavaScriptClass.js-->
<!-- so we can call java class methods using this javascript class-->
<script type='text/javascript' src='dwr/interface/MyJavaScriptClass.js'>
function getDetailsFromServer()
//calling serverside method using our generated
//javascript class.if you do my first example
//then you can understand this example very easy
function handleReceivedData(obj)
//DWRUtil.setValue and DWRUtil.getValue are the existing
//properties in engine.js, setValue sets the value to that
//particular id(including div,span,forms etc),DWRUtil.getValue
//get the value of that particular id.
//we got bean object and we converted that bean object to
//javascript object. so we have to call those bean properties
//using object.propertyname
FirstName:  <span id="firstname"></span> <br/>
LastName:  <span  id="lastname"></span><br/>
<button onclick="getDetailsFromServer()">submit</button>

in this example we got details from server using dwr with bean object
If you have any doubts post your comments below.
Labels: , ,
  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.

Post a Comment