dwr入门小例子

Hello.java
package com.makey;

public class Hello {

	public String Hello(String name) {
		return "Welcome to you:" + name;
	}
}


dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
	<allow>
		<create creator="new" javascript="Hello">
			<param name="class" value="com.makey.Hello" />
		</create>
	</allow>
</dwr>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
		<init-param>
			<description></description>
			<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>


hello.js
function hello() { 
	alert("dfsds");   
	var user = user.value;    
	Hello.hello(user, callback);
} 

function callback(msg) {  
	DWRUtil.setValue('result', msg);
} 


index.jsp
<%@ page language="java" pageEncoding="gbk"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'index.jsp' starting page</title>
		<script src="dwr/engine.js"></script>
		<script src="dwr/util.js"></script>
		<script src="hello.js"></script>
	</head>

	<body>
		<input id="user" type="text" />
		<input type="button" value="halou" onclick="hello();" />
		<div id="result"></div>
	</body>
</html>

你可能感兴趣的:(java,jsp,xml,Web,DWR)