如何确定正在呈现哪些JSP页面?

时间:2010-02-11 05:13:57

标签: java jsp jspinclude

我正在使用使用<jsp:include>嵌套的简单JSP的遗留应用程序。

没有使用任何框架 - 只是JSP Servlet和过滤器。

有人可以建议一种方法来跟踪正在呈现的JSP页面吗?

也许在渲染引擎(Jasper)中有一个日志,或者一个钩子。

1 个答案:

答案 0 :(得分:2)

创建一个过滤器,该过滤器仅监听url-pattern *.jspINCLUDE调度程序。

<filter>
    <filter-name>includeFilter</filter-name>
    <filter-class>com.stackoverflow.q2242429.IncludeFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>includeFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

HttpServletRequest#getServletPath()获取父网页,使用密钥javax.servlet.include.servlet_pathHttpServletRequest#getAttribute()获取包含页面:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException
{
    HttpServletRequest httpreq = (HttpServletRequest) request;
    String parentPage = httpreq.getServletPath();
    String includePage = (String) httpreq.getAttribute("javax.servlet.include.servlet_path");
    // Log it here?

    chain.doFilter(request, response);
}