Getting Started with DWR
There are several ways to get started with DWR. We recommend the following:
- Follow the 5 steps outlined below.
- Download the dwr.war file and experiment with the examples.
1. Install the DWR JAR file
Download the dwr.jar file. Place it into the
WEB-INF/lib directory of your web application.
2. Install the Commons Logging JAR file
3. Add the DWR servlet definition and mapping to your application's web.xml
Add the following lines to your web application's deployment descriptor (
<servlet> section needs to go with any existing
<servlet> sections, and likewise with the
<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>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>
4. 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.
A simple way to start is with something like this:
The new creator that we used above uses the public no-args constructor that all Java Beans must have. It is also worth remembering that DWR has one restriction:
5. Take DWR for a test drive!
Go to the following URL:
You should see a page showing you the classes you configured in Step 4. Having followed a link you should see an index of all the methods ready for calling - simply enter the required parameters (if parameters are required) and click the execute button. These pages are valuable debugging tools and dynamically generated by DWR when the debug init-param is set to true.
How to make use of this from your web application
Another way to get started is to look at the source from the pages that you just viewed:
- Go to http://localhost:8080/[YOUR-WEBAPP-CONTEXT]/dwr/ and click on your class
- View source and find the line that executes the method that you are interested in.
- Paste the text into an HTML or JSP page in your web-app.
<script src='/[YOUR-WEBAPP-CONTEXT]/dwr/interface/[YOUR-SCRIPT].js'></script> <script src='/[YOUR-WEBAPP-CONTEXT]/dwr/engine.js'></script>
You can omit the
/[YOUR-WEBAPP-CONTEXT]/ section and use relative paths in your web pages if you wish.
What if it doesn't work?
We have a page containing troubleshooting advice and common problems and their fixes. This is the first place to look.
If you are still having problems please join the mailing list and ask there.