RemoteServiceServlet 随便看看

其实一直对gwt servlet 如何与 spring frame work结合有点疑惑,今天瞄了两眼RemoteServiceServlet 的代码, 大概有点思路了,GWT RPC service client端提交数据,应该用的仍然是post方法,所以servlet处理客户端的Request调用的是doPost方法

RemoteServiceServlet 的基类 AbstractRemoteServiceServlet 里定义了doPost 方法如下:

@Override
public final void doPost(HttpServletRequest request,
      HttpServletResponse response) {
    ......
    processPost(request, response);
    ......
}

 省略了一些check,try,catch,就剩 processPost 方法了. 该方法定义在 RemoteServiceServlet 中

@Override
  public final void processPost(HttpServletRequest request,
      HttpServletResponse response) throws IOException, ServletException,
      SerializationException {
    // Read the request fully.
    //
    String requestPayload = readContent(request);

    // Let subclasses see the serialized request.
    //
    onBeforeRequestDeserialized(requestPayload);

    // Invoke the core dispatching logic, which returns the serialized
    // result.
    //
    String responsePayload = processCall(requestPayload);

    // Let subclasses see the serialized response.
    //
    onAfterResponseSerialized(responsePayload);

    // Write the response.
    //
    writeResponse(request, response, responsePayload);
  }

 接着是这里调用的processCall方法

public String processCall(String payload) throws SerializationException {
    // First, check for possible XSRF situation
    checkPermutationStrongName();

    try {
      RPCRequest rpcRequest = RPC.decodeRequest(payload, delegate.getClass(), this);
      onAfterRequestDeserialized(rpcRequest);
      return RPC.invokeAndEncodeResponse(delegate, rpcRequest.getMethod(),
          rpcRequest.getParameters(), rpcRequest.getSerializationPolicy(),
          rpcRequest.getFlags());
    } catch (IncompatibleRemoteServiceException ex) {
      log(
          "An IncompatibleRemoteServiceException was thrown while processing this call.",
          ex);
      return RPC.encodeResponseForFailure(null, ex);
    } catch (RpcTokenException tokenException) {
      log("An RpcTokenException was thrown while processing this call.",
          tokenException);
      return RPC.encodeResponseForFailure(null, tokenException);
    }
  }

 看到这里已经大致有点想法了,其实 RPC.invokeAndEncodeResponse 的调用已经完成了 调用server端方法,将结果组织成为String(serizable, 不知道这个词用的对不对,用错了见笑,请指正哈)的过程 ,该方法的第一个参数就是包含要调用的server端的方法(第二个参数)的实例,因此我们若想将spring框架和 GWT的 Servlet 结合起来 其实是要将第一个参数替换为通过spring的 beanfactory得到的各种bean实例.

猜你喜欢

转载自merlin-t.iteye.com/blog/1677375