Config DWR

DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible. This post help you config DWR easily with example

Config DWR - How it works
  1. Add DWR library dependency

    <dependency>
      <groupId>org.directwebremoting</groupId>
      <artifactId>dwr</artifactId>
      <version>3.0.0-RELEASE</version>
    </dependency>
    
  2. DWR Definition - Add the DWR servlet definition and mapping to your application's web.xml

    Add the following lines to your web application's deployment descriptor (WEB-INF/web.xml). The <servlet> section needs to go with any existing <servlet> sections, and likewise with the <servlet-mapping> section.

    <servlet>
      <servlet-name>dwr-invoker</servlet-name>
      <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
      <init-param>
        <param-name>fileUploadMaxBytes</param-name>
        <param-value>25000</param-value>
      </init-param>
      <init-param>
        <param-name>activeReverseAjaxEnabled</param-name>
        <param-value>false</param-value>
      </init-param>
      <init-param>
        <param-name>allowScriptTagRemoting</param-name>
        <param-value>true</param-value>
      </init-param>
      <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>
    
  3. Config DWR - Create the DWR configuration file (dwr.xml)

    Create a new file in you web application's WEB-INF directory (alongside web.xml) named dwr.xml.

    <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
    <dwr>
      <allow>
        <create creator="new" javascript="JDate">
          <param name="class" value="java.util.Date"/>
        </create>
        <create creator="new" javascript="Demo">
          <param name="class" value="your.java.Bean"/>
        </create>
        <create creator="new">
          <param name="class" value="your.java.YourClass"/>
        </create>
        <create creator="new" scope="application">
          <param name="class" value="your.java.YourClassHasScopeApplication"/>
        </create>
        <create creator="new" scope="application">
          <param name="class" value="your.java.YourClassUsingBean"/>
        </create>
        <convert converter="bean" match="your.java.YourClassUsingBean"/>
      </allow>
    </dwr>
    
  4. How to make use of this from your web application

    Paste the text into <head> tag an HTML or JSP page in your web-app

    <script src='/[YOUR-WEBAPP-CONTEXT]/dwr/engine.js'></script>
    <script src='/[YOUR-WEBAPP-CONTEXT]/dwr/interface/[YOUR-SCRIPT].js'></script>
    

    Paste the text into <script> tag an HTML or JSP page in your web-app

    Remote.getData(parameter, handle);
    function handle(data) {
      console.log(data);
    }
    

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections