Making AJAX call using DWR (Direct Web Remoting)

This Article will explain how to make DWR (Direct Web Remoting) AJAX call from JSP

Follow the simple Steps
a         Copy the DWR jar file to the Lib directory under webcontent/WEB-INF (download from here)
b        Copy the following xml snippet to the web.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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

c         Create or Copy the dwr.xml under the WEB-INF directory and content of the file is as follows
<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
    <create creator="new" javascript=" AjaxDWRTest">
      <param name="class" value="com.vaka.siva.helper.AjaxDWRTest"/>
    </create>
  </allow>
</dwr>

d        Write a class with public methods and className  should be like as specified in dwr.xml (If you need the  HTTPServletSession then we use below extra code)

public class AjaxDWRTest {
public String getDisplayedTickets(int pageIndex) {
                                               
                                                WebContext ctx = WebContextFactory.get();
                                                HttpServletRequest request = ctx.getHttpServletRequest();
                                                HttpSession session = ctx.getSession();
                                                if (session == null)return null;
                                               
//call to some helpers and do the processing
//you can make backend calls now or you can make backend calls first and store it in the session and now retrieve from session

return tktstr
}
e        In above XML , we have specified the AjaxDWRTest as javascript ,so DWR will create  that for us we just need to include the following in JSP where we need the AJAX call

<script type="text/javascript" src='<%= renderResponse.encodeURL(renderRequest.getContextPath()+ "/dwr/interface/AjaxDWRTest.js") %>'>
</script>
<script type="text/javascript" src='<%= renderResponse.encodeURL(renderRequest.getContextPath()+ "/dwr/engine.js") %>'>

f          Following is the way we invoke the callback method in JSP

//Print the table with initial values List (this is not ajax call)
<c:if test="${!empty SessionBean.initialTicketValuesMap}">
<script type="text/javascript">
               <portlet:namespace/>paintDynaSec(intialTicketValuesString);
</script>
</c:if>

//following is AJAX call , when somebody click next page then we call below
AjaxDWRTest.getDisplayedTickets(currentPageIndex, <portlet:namespace/>paintDynaSec);

//Once AJAX backend call is over then above statement will automatically calls paintDynaSec javascript function with parameter that is returned from the AJAX
function <portlet:namespace/>paintDynaSec(tktstr) {
                        ticketList.innerHTML =tktstr;
}

No comments:

Post a Comment