A small example of a dwr

Source: Internet
Author: User

At the weekend, study dwr at home and do an example. Implements a message from the client to the server, after the message is returned, displayed on the client. The code is as follows:

JSP page:

<%@ page language= "java" pageencoding= "Utf-8"%>
<script src= ' dwr/interface/hello.js ' ></script>
<script src= ' dwr/engine.js ' ></script>
<script src= ' dwr/util.js ' ></script>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<base href= "<%=basePath%>" >

<title> This is the test dwr</title>
<script type= "Text/javascript" >
function Tijiao ()
{
var info=document.all["Infor"].value;
Hello.say (Info,callback);
}
function callback (data)
{
Document.all["Con"].innerhtml=data;
}
</script>

<body>
<br><br><br><br><br><br><br>
<center>
Haha, see the address is not AH. The basepath of this page is: <%=basePath%><br>
Haha, see the address is not AH. The path to this page is: <%=path%><br><br><br><br>
<input type= "text" name= "infor" >
<input type= "button" value= "Look at Me" onclick= "Tijiao ()" ><br>
The content returned after submission is as follows:
<br><font color= "Red" > <span id= "con" ></span> </font>
</center>

</body>

server-side Java classes

package;

public class Hello {

public string Say (String infor) {
Return "The message you sent is:" + infor;
}
}

dwr.xml File:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE dwr Public "-//getahead limited//dtd Direct Web Remoting 2.0//en" "Http://getahead.org/dwr/dwr20.dtd" >

<dwr>
<allow>
<create creator= "new" javascript= "Hello" scope= "session" >
<param name= "Class" value= ". Hello "/>
<include method= "Say"/>
</create>
</allow>

</dwr>

web.xml File:

<?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>

</web-app>

Jar Pack yourself added, note I lazy write, make up to see it!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.