DWR使用实例

前端之家收集整理的这篇文章主要介绍了DWR使用实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.创建一个Web Project项目,向WEB-INF/lib/目录包里面导入dwr.jar

2.修改web.xml文件

[c-sharp] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <!--dwr核心servlet-->
  8. <listener>
  9. <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
  10. </listener>
  11. <servlet>
  12. <!--dwr核心servelt实现类-->
  13. <servlet-name>dwr</servlet-name>
  14. <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  15. <!--设置dwr可调式-->
  16. <init-param>
  17. <param-name>debug</param-name>
  18. <param-value>true</param-value>
  19. </init-param>
  20. </servlet>
  21. <servlet-mapping>
  22. <servlet-name>dwr</servlet-name>
  23. <url-pattern>/dwr/*</url-pattern>
  24. </servlet-mapping>
  25. <welcome-file-list>
  26. <welcome-file>index.jsp</welcome-file>
  27. </welcome-file-list>
  28. </web-app>

3.创建一个DwrService.java类

copy

    packagecom.yjpeng.dwr;
  1. publicclassDwrService{
  2. publicStringsayHello(Stringmessage){
  3. return"欢迎使用DWR"+message;
  4. }
  5. }

4.在WEB-INF目录下创建一个dwr.xml文件

copy

    <!DOCTYPEdwrPUBLIC
  1. "-//GetAheadLimited//DTDDirectWebRemoting3.0//EN"
  2. "http://getahead.org/dwr/dwr30.dtd">
  3. <dwr>
  4. <allow>
  5. <createcreator="new"javascript="dwrService"scope="application">
  6. <paramname="class"value="com.yjpeng.dwr.DwrService"></param>
  7. </create>
  8. </allow>
  9. </dwr>

5.修改index.jsp文件代码

[xhtml] copy
    <%@pagelanguage="java"import="java.util.*"pageEncoding="ISO-8859-1"%>
  1. <%
  2. Stringpath=request.getContextPath();
  3. StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  4. % <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  5. <htmlheadbasehref="<%=basePath%>"title>MyJSP'index.jsp'startingpage</scriptsrc="dwr/interface/dwrService.js">scriptscriptsrc="dwr/engine.js"scripttype="text/javascript" functionaa(){
  6. dwrService.sayHello("assd",bb);
  7. }
  8. functionbb(data){
  9. document.getElementById("message").value=data;
  10. bodyinputtype="text"id="message"/>
  11. inputtype="button"value="callDwr"onclick="aa();"/>
  12. >

6.把web项目部署到服务器中,在浏览器中输入相应的URL。最后测试成功会在文本框里面出现 “assd” 字符串。

猜你在找的Ajax相关文章