【问题标题】:JSP info page debug helperJSP 信息页面调试助手
【发布时间】:2011-07-02 17:24:37
【问题描述】:

是否有人使用过 jsp 调试帮助页面,该页面可以拖放到任何 webapp 中或包含在另一个 jsp 页面中来查看 header、req 和 session 属性?

如果可能的话,你能分享一下吗?对很多人都有很大的帮助

【问题讨论】:

    标签: java debugging jsp helper


    【解决方案1】:
    • pageDebugger.jsp放到你的webapp中
    • 直接访问页面或从另一个 jsp 中包含,例如 layout.jsp 以在每个页面上显示此详细信息
    • 本页以表格的形式列出以下内容
      • 请求参数
      • 页面范围的属性和值
      • 请求范围内的属性和值
      • 会话范围的属性和值
      • 应用程序范围的属性和值
      • 请求标头
      • 范围内定义的所有 Spring bean
      • 在类路径中搜索资源的选项
    • 对 spring 框架的依赖是可选的,如果你不使用就删除它

    pageDebugger.jsp

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <head>
    <style type="text/css">
    td {
        word-wrap: break-word;
    }
    </style>
    </head>
    <body>
    <table width="100%" border="1" cellpadding="0" cellspacing="0"
        style="table-layout: fixed;">
        <colgroup>
            <col width="500">
        </colgroup>
        <tr>
            <th colspan="2">
            <h3>attributes in $paramValues</h3>
            </th>
        </tr>
        <c:forEach var="entry" items="${paramValues}">
            <tr>
                <td><c:out value="${entry.key}" /></td>
                <td><c:forEach var="item" items="${entry.value}"
                    varStatus="status">
                    <pre><c:out value="${item}" /></pre>
                    <c:if test="${not status.last}">
                        <br />
                    </c:if>
                </c:forEach></td>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="2">
            <h3>attributes in $requestScope</h3>
            </th>
        </tr>
        <c:forEach var="entry" items="${requestScope}">
            <tr>
                <td><c:out value="${entry.key}" /></td>
                <td><pre><c:out value="${entry.value}" /></pre></td>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="2">
            <h3>attributes in $sessionScope</h3>
            </th>
        </tr>
        <c:forEach var="entry" items="${sessionScope}">
            <tr>
                <td><c:out value="${entry.key}" /></td>
                <td><pre><c:out value="${entry.value}" /></pre></td>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="2">
            <h3>attributes in $pageScope</h3>
            </th>
        </tr>
        <c:forEach var="entry" items="${pageScope}">
            <tr>
                <td><c:out value="${entry.key}" /></td>
                <td><pre><c:out value="${entry.value}" /></pre></td>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="2">
            <h3>attributes in $headerValues</h3>
            </th>
        </tr>
        <c:forEach var="entry" items="${headerValues}">
            <tr>
                <td><c:out value="${entry.key}" /></td>
                <td><c:forEach var="item" items="${entry.value}"
                    varStatus="status">
                    <pre><c:out value="${item}" /></pre>
                    <c:if test="${not status.last}">
                        <br />
                    </c:if>
                </c:forEach></td>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="2">
            <h3>attributes in $applicationScope</h3>
            </th>
        </tr>
        <c:forEach var="entry" items="${applicationScope}">
            <tr>
                <td><c:out value="${entry.key}" /></td>
                <td><pre><c:out value="${entry.value}" /></pre></td>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="2">
            <h3>System Properties</h3>
            </th>
        </tr>
        <tr>
            <th>Key</th>
            <th>Value</th>
        </tr>
        <%@page import="java.util.Map"%>
        <%@page import="java.util.Set"%>
        <%@page import="java.util.Properties"%>
        <%@page import="java.util.Arrays"%>
        <%
            Properties p = System.getProperties();
            Set<Map.Entry<Object, Object>> set = p.entrySet();
            for (Map.Entry<Object, Object> e : set) {
        %>
        <tr>
            <td><%=e.getKey()%></td>
            <td><%="".equals(e.getValue()) ? "&nbsp;" : e.getValue()%></td>
            <%
                }
            %>
        </tr>
        <tr>
            <th colspan="2">
            <h3>Spring Beans</h3>
            </th>
        </tr>
        <%@page import="org.springframework.web.context.WebApplicationContext"%>
        <%@page
            import="org.springframework.web.context.support.WebApplicationContextUtils"%>
        <%@page import="org.springframework.core.io.Resource"%>
        <%
            try {
                WebApplicationContext springContext = WebApplicationContextUtils
                        .getWebApplicationContext(config.getServletContext());
                if (springContext != null) {
                    String[] beanNames = springContext.getBeanDefinitionNames();
                    Arrays.sort(beanNames);
                    for (String beanName : beanNames) {
                        String className = springContext.getType(beanName)
                                .getName();
        %>
        <tr>
            <td><%=beanName%></td>
            <td><%=className%></td>
        </tr>
        <%
            }
        %>
        <tr>
            <th colspan="2">
            <h3>Spring Resources</h3>
            </th>
        </tr>
        <tr>
            <th colspan="2">
            <form><input name="resources" size="50"/></form>
            </th>
        </tr>
    
        <%
            String resourceNames = request.getParameter("resources");
                    if (resourceNames != null) {
                        Resource[] resources = springContext
                                .getResources(resourceNames);
                        for (Resource r : resources) {
        %>
        <tr>
            <td><%=r.getFilename()%></td>
            <td><%=r.getURI()%></td>
        </tr>
        <%
            }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        %>
    
    
    </table>
    </body>
    </html>
    

    此外,如果您可以选择修改 web.xml,请查看这些也可用于监控/分析活动的 http 会话属性

    http://code.google.com/p/sessionmon/

    http://messadmin.sourceforge.net/

    【讨论】:

      【解决方案2】:

      在工作中,我使用 JDeveloper 从 IDE 以调试模式运行应用程序。您可以在 .jsp 中插入断点并查看所有调试信息。虽然,我不是 jdeveloper 的忠实粉丝,但它是 IDE 的一个不错的功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        相关资源
        最近更新 更多