当前位置:编程学习 > JAVA >>

客户端调用服务器端方法的简单例子

 昨天网友问了下DWR的问题,DWR是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,不过自己并未在项目中用过,只是之前在网上看过它的一些介绍,大概的了解它的调用原理。
如果要在客户端调用服务器端的java方法,一种是基于TCP协议,如RMI;另一种是基于HTTP协议,如web service、ajax。DWR就是基于ajax方式调用WEB服务器上的JAVA方法的一种。
如果是基于HTTP协议来调用,按个人理解,只需把3步做好就行了,
1、定义好访问规则,如怎样把需要调用的类名、方法名、参数提交到服务器端。
2、服务器端怎样按收客户端传类名、方法名、参数;接收到数据后,怎样实例化该类和执行指定的方法。
3、服务器把处理结果以什么形式返回给客户端。

和网友说着来了兴趣,回家顺便写了个实现尝试下。

下面开始按上面的原理步骤写一个简单的实现例子
1、类名、方法名、参数通过ajax方式pose到服务器端RemoteServlet,本文直接使用jquery的ajax
ScriptEngine.js,

function getRootPath(){
    var strFullPath=window.document.location.href;
    var strPath=window.document.location.pathname;
    var pos=strFullPath.indexOf(strPath);
    var prePath=strFullPath.substring(0,pos);
    var postPath=strPath.substring(0,strPath.substr(1).indexOf(/)+1);
    return(prePath+postPath);
    }
function ScriptEngine(){
    this.name = "";
    this.method = "";
    this.setClass = function(n){
        name = "class=" + n;
    }
    this.setMethod = function(m){
        method = "method=" + m;
    }
    this.invoke = function(){
        var result;
        var parames = "";
        if(arguments.length > 0){
            for(i = 0; i < arguments.length; i++){
                parames=arguments[i];
            }
        }
        $.ajax( {
            type : "POST",
            url : getRootPath() + "/RemoteServlet",
            cache : false,
            async : false,
            data : name + "&" + method + "&args=" + parames,
            success : function(data) {
                result = data;
            },
            error :function(data){
                result= data;
            }
        });
        return result;
    }
}
2、服务器端RemoteServlet接收客户端传来类名、方法名、参数,并通过反射的方式,实例化该类和执行相应的方法。
RemoteServlet.java
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RemoteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public RemoteServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        remote(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        remote(request, response);
    }
    protected void remote(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("class");
        String method = request.getParameter("method");
        String args = request.getParameter("args");
        if(name != null && method != null){
            try{
                Class cls = Class.forName(name);
                Object[] parames = !args.equals("") ? args.split(";") : null;
                Object obj = cls.newInstance();
                Method[] methods = cls.getMethods();
               
                for(Method m:methods){
                    System.out.println(m.getName());
                    if(m.getName().equals(method)){
                        Object result = parames != null ? m.invoke(obj,parames) : m.invoke(obj);
                        response.setCharacterEncoding("UTF-8");
                        response.getWriter().println(result.toString());
                        break;
                    }
                }   
            }
            catch(Exception e){
                e.printStackTrace();
            }
           
        }
    }
}
在web.xml中配置该servlet


  <servlet>
    <description></description>
    <display-name>RemoteServlet</display-name>
    <servlet-name>R

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,