DWR3.0简单实例

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

1.WEB-INFO/lib下导入dwr.jar

2.WEB-INFO/dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
	<allow>
		<create creator="new" javascript="dwrService" scope="application">
			<param name="class" value="dwr.service.DwrService" />
		</create>
	</allow>
</dwr>

3.WEB-INFO/web.xml文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app id="dwr">
 <listener>
  <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
 </listener>
 <servlet>
  <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>

 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>


4.dwr测试类dwrService.java

package dwr.service;

public class DwrService {
 public String login(String name,String password) {
  if (name.equals("admin") && password.equals("admin"))
   return "登陆成功";
  else
   return "登陆失败";
 }
}


5.测试页面

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  <script type="text/javascript" src="dwr/engine.js"></script>
  <script type="text/javascript" src="dwr/util.js"></script>
  <script type="text/javascript" src="dwr/interface/dwrService.js"></script>
  <script type="text/javascript">
 function login() {
  var username = document.getElementById("username").value;
  var password = document.getElementById("password").value;
  dwrService.login(username,password,callback);
 }
 function callback(result) {
  document.getElementById("message").value= result;
 }

</script>
 </head>

 <body>
  <input type="text" id="username">	//用户名
  <input type="text" id="password">	//密码
  <input type="text" id="message">	//显示是否登录成功
  <input type="button" value="dwrCall" onclick=login();>

 </body>
</html>

原文链接:https://www.f2er.com/ajax/166564.html

猜你在找的Ajax相关文章