sample program with DWR

su03 picture su03 · Jul 8, 2011 · Viewed 8.7k times · Source

I am trying to write a sample program with direct web remoting. went through the site http://directwebremoting.org/ have some basic questions. please help me

what to try the program http://directwebremoting.org/dwr-demo/simple/text.html

  1. where to save the html and javascript code and with what name???
  2. they say to include some 2 scripts (engine.js and util.js). in which page i should include these two.
  3. they have given a java code also, where to save this now???

Answer

smeerkahoven picture smeerkahoven · May 22, 2012

To start with DWR you have to create your dwr.xml file at

/WEB-INF/dwr.xml

add dwr.jar file to

/WEB-INF/lib/dwr.jar

if lib folder is not created then create it.

And how you must kwno, DWR uses servlet classs to be functional, so you have to declare them how must work. You have to modify your web.xml file to tell DWR that it is a servlet.

    ...
    <servlet>
      <display-name>DWR Servlet</display-name>
      <servlet-name>dwr-invoker</servlet-name>
      <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
      <init-param>
        <param-name>jsonpEnabled</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>
   ...

You can test you DWR configuration going to

    http://yourapp:port/dwr

check that yourapp is the name of your app and port is the number of the assigned port.

Well now you have to create a class controller that interactues with DWR and your JSP pages. Something like this

com.dwr.test.MyController

     public class MyController {
          public String doSomething() {
              return "example" ;
          }
     }

you have to tell DWR that this is a controller, so in your dwr.xml file you have to write

    <create creator="new" javascript="mycontroller">
        <param name="class" value="com.dwr.test.MyController "/>
     </create>

Check that javascript="mycontroller"
is how you are going to call your java class controller from your JSP page

Now you have a controller connection with your App controller and DWR.

Later, you have to tell to your JSP page what controller use.

I have index.jsp as an example

    <html>
    <head>
    <script type='text/javascript' src='/webbitacora/dwr/util.js'></script>
    <script type='text/javascript' src='/webbitacora/dwr/engine.js'></script>
    <script type='text/javascript' src='/webbitacora/dwr/interface/mycontroller.js'></script>
    ...

how this stuff works ? the two script lines (util an engine) are vital for DWR, this is the main scenario that DWR uses to work. And the third line is the name name of your Controller that you recently declared in your drw.xml file. You don't need to create this files, DWR is responsible of the creation of this files.

now you can use your method with any HTML controller, javascript function or anything else

    mycontroler.doSomething({
        callback : function (data){
            alert(data) ;
        } 
    });

to know more methods, passing parameters arguments I recommend you visit DWR's web page

http://directwebremoting.org/dwr/index.html