0

I m trying to use aspectJ to intercept HttpServlet.do*(request, response) and get the HTML text ( need to extract the title and maybe store the html to a file).

What is the best way to access the response body (html text) once I have a reference to the HttpServletResponse?

Here is my staring code.

public aspect HttpRequestHandlerAspect {

pointcut getRequest(HttpServletRequest request, HttpServletResponse response) 
    : execution(protected * javax.servlet.http.HttpServlet.*(HttpServletRequest, HttpServletResponse))  
    && args(request, response);

  Object around(HttpServletRequest request, HttpServletResponse response) : getRequest(request, response) {
      Object ret = proceed(request, response);
              // now how do I access the HTML response text ( and get the title of the page) in here?

    }
}
4

1 回答 1

1

这可能不是您问题的准确答案,但请尝试按照此处的建议提取响应:如何读取 HttpServletReponses 输出流? 您不必创建过滤器,只需传递给您的 HttpServletResponseWrapper

继续(请求,包装器)。

Object around(HttpServletRequest request, HttpServletResponse response): getRequest(request, response) {
    MyHttpServletResponseWrapper wrapper = new MyHttpServletResponseWrapper(response);
    Object ret = proceed(request, wrapper);
    // The MyHttpServletReponseWrapper class captures everything and doesn't forward to the original stream, so we have to do this
    response.getWriter().write(wrapper.toString());
    // use wrapper.toString() to access response
}
于 2013-08-05T19:40:01.743 回答