2

我正在尝试使用带有过滤器的 RemoteServiceServlet 来捕获请求以跟踪在我的用户的 webapplicaiton 中请求的方法。

我无法做到这一点......

任何人都可以建议一种最佳方法来跟踪称为 ServletFilter 之类的方法。

注意我喜欢跟踪用户和他请求的请求。

4

3 回答 3

0

在每个方法 java.util.logging.Logger 的开头使用,然后写入参数和用户信息。该信息将写入您的 tomcat 日志中。

于 2012-12-18T13:27:51.160 回答
0

您可以使用“Spring MVC”“Google Guice”来实现,这个想法背后的步骤是:

  1. 编写一个通用 servlet 并将所有 RPC 的 URL 映射到它。
  2. 通用 servlet 必须扩展“RemoteServiceServlet”类。
  3. 重写 processCall 方法,以便您可以找出客户端要调用哪个类的哪个方法。

    @Override public String processCall(String payload) 抛出 SerializationException {

    try {
        RPCRequest rpcRequest = RPC.decodeRequest(payload);
    
        RemoteService service = getServiceInstance(rpcRequest.getMethod().getDeclaringClass());
    
        return RPC.invokeAndEncodeResponse(service, rpcRequest.getMethod(),
                rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());
    
    } catch (IncompatibleRemoteServiceException ex) {
        getServletContext()
                .log("An IncompatibleRemoteServiceException was thrown while processing this call.", ex);
        return RPC.encodeResponseForFailure(null, ex);
    }
    

    }

您可以看到这个使用 Google guice 的示例。

玩得开心。

于 2012-12-18T14:11:53.817 回答
0

您可以在 RemoteServiceServlet 的实现中覆盖public boolean preProcess(HttpExchange exchange)如下。

public boolean preProcess(HttpExchange exchange) {
    String body = null;
    RPCRequest req = null;

    // Read and parse the request
    byte[] bytes = null;
    Object bytebuf = getAttribute("RequestBody");
    if ((bytebuf != null) && (bytebuf instanceof ByteBuffer)) {
        bytes = ((ByteBuffer) bytebuf).array();
    }

    if (bytes == null) {
        setAttribute("ResponseCode", new Integer(500));
        setAttribute("ResponseBody", "Missing Request Body.");
        return true;
    }

    body = new String(bytes, "UTF-8");

    req = RPC.decodeRequest(body, this.getClass());

    String methodName = req.getMethod().toString();
    //Here you have name of a method
    //You can get parameters
    //Object[] parms = req.getParameters();
    return true;
}
于 2012-12-19T11:07:48.427 回答