Aspectj:从HttpServletRresponse获取Response body(HTML Text)

时间:2013-08-05 19:31:01

标签: java java-ee servlets aop aspectj

我试图使用aspectJ拦截HttpServlet.do *(请求,响应)并获取HTML文本(需要提取标题并可能将html存储到文件中)。

一旦我引用了HttpServletResponse,访问响应主体(html文本)的最佳方法是什么?

这是我的凝视代码。

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?

    }
}

1 个答案:

答案 0 :(得分:1)

这可能不是您问题的准确答案,但请尝试按照此处的建议提取答案:How can I read an HttpServletReponses output stream? 您不必创建过滤器,只需要传递给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
}