This is a simple demonstration of how to dynamically update a web-page with the contents of a page retrieved from the server.
Included Page:
Normally the code called by DWR is a simple POJO that knows nothing about
the web. In this demonstration we want to find out about the web server so we
use the WebContext
class to get access to the ServletContext.
This example is similar to the dynamic text example, the big difference is that we are using this HTML page, and the WebContext class to allow us to get it. There are no iframes used in this example - just DWR and a humble div.
The Java code looks like this:
public String getInclude() { WebContext wctx = WebContextFactory.get(); return wctx.forwardToString("/simpletext/forward.html"); }
There is one other difference. In the previous example we were working with plain text. Here we are working with HTML. To help protect you from XSS attacks DWR automatically escapes HTML characters, however sometimes (like in this case) we know that we have HTML, and that it is safe. So the Javascipt code looks like this:
function forward() { Demo.getInclude(function(data) { dwr.util.setValue("forward", data, { escapeHtml:false }); }); }
The third parameter is the options parameter which is present on a number of DWR functions. It allows you to customize how the function acts. In this case we are asking DWR to not escape and HTML in the passed string.
<p> <input value="Include Page" type="button" onclick="forward()"/><br/> Included Page: </p> <div id="forward"></div>
function forward() { Demo.getInclude(function(data) { dwr.util.setValue("forward", data, { escapeHtml:false }); }); }
package org.getahead.dwrdemo.simpletext; import java.io.IOException; import javax.servlet.ServletException; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; public class Demo { public String getInclude() throws ServletException, IOException { WebContext wctx = WebContextFactory.get(); return wctx.forwardToString("/simpletext/forward.html"); } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="new" javascript="Demo"> <param name="class" value="org.getahead.dwrdemo.simpletext.Demo"/> </create> </allow> </dwr>