Saturday, October 31, 2009

Recipe 13.5. Troubleshooting JSP Pages










Recipe 13.5. Troubleshooting JSP Pages




Problem



You want to see a dump of the names and values of servlet request and
HTTP session attributes and for a particular JSP page so you can
troubleshoot rendering problems.





Solution



Include the debug.jsp file shown
on the JSP page (see Example 13-5).




Example 13-5. JSP dump of request, session, and context data

<hr width="3px">
Debug Information:<br>
<table border="1" width="50%" class="debug">
<tr>
<th colspan="3" style="background:orange">
<b>Request Parameters</b>
</th>
</tr>
<c:forEach items="${paramValues}" var="parameter">
<tr>
<td><c:out value="${parameter.key}"/></td>
<td colspan="2">
<c:forEach var="value" items="${parameter.value}">
<textarea rows="2" cols="50">
<c:out value="${value}"/>
</textarea>
</c:forEach>
</td>
</tr>
</c:forEach>

<tr>
<th colspan="3" style="background:orange">
<b>Header Values</b>
</th>
</tr>
<c:forEach items="${header}" var="h">
<tr>
<td><c:out value="${h.key}"/></td>
<td colspan="2">
<textarea rows="2" cols="50">
<c:out value="${h.value}"/>
</textarea>
</td>
</tr>
</c:forEach>

<tr>
<th colspan="3" style="background:orange">
<b>Initialization Parameters</b>
</th>
</tr>
<c:forEach items="${initParam}" var="parameter">
<tr>
<td><c:out value="${parameter.key}"/></td>
<td colspan="2">
<textarea rows="2" cols="50">
<c:out value="${parameter.value}"/>
</textarea>
</td>
</tr>
</c:forEach>

<tr>
<th colspan="3" style="background:orange">
<b>Cookies</b>
</th>
</tr>
<c:forEach items="${cookie}" var="mapEntry">
<tr>
<td rowspan="8"><c:out value="${mapEntry.key}"/></td>
<td align="right">Name:</td>
<td><c:out value="${mapEntry.value.name}"/></td>
</tr>
<tr>
<td align="right">Value:</td>
<td><c:out value="${mapEntry.value.value}"/></td>
</tr>
<tr>
<td align="right">Domain:</td>
<td><c:out value="${mapEntry.value.domain}"/></td>
</tr>
<tr>
<td align="right">Max Age:</td>
<td><c:out value="${mapEntry.value.maxAge}"/></td>
</tr>
<tr>
<td align="right">Path:</td>
<td><c:out value="${mapEntry.value.path}"/></td>
</tr>
<tr>
<td align="right">Secure:</td>
<td><c:out value="${mapEntry.value.secure}"/></td>
</tr>
<tr>
<td align="right">Version:</td>
<td><c:out value="${mapEntry.value.version}"/></td>
</tr>
<tr>
<td align="right">Comment:</td>
<td><c:out value="${mapEntry.value.comment}"/></td>
</tr>
</c:forEach>

<tr>
<th colspan="3" style="background:orange">
<b>Page Scope Attributes</b>
</th>
</tr>
<c:forEach items="${pageScope}" var="itm">
<c:if test="${itm.key != 'javax.servlet.jsp.jspResponse'}">
<tr>
<td><c:out value="${itm.key}"/></td>
<td colspan="2">
<textarea rows="2" cols="50">
<c:out value="${itm.value}"/>
</textarea>
</td>
</tr>
</c:if>
</c:forEach>

<tr>
<th colspan="3" style="background:orange">
<b>Request Scope Attributes</b>
</th>
</tr>
<c:forEach items="${requestScope}" var="itm">
<tr>
<td><c:out value="${itm.key}"/></td>
<td colspan="2">
<textarea rows="2" cols="50">
<c:out value="${itm.value}"/>
</textarea>
</td>
</tr>
</c:forEach>

<tr>
<th colspan="3" style="background:orange">
<b>Session Scoped Attributes</b>
</th>
</tr>
<c:forEach items="${sessionScope}" var="itm">
<tr>
<td><c:out value="${itm.key}"/></td>
<td colspan="2">
<textarea rows="2" cols="50">
<c:out value="${itm.value}"/>
</textarea>
</td>
</tr>
</c:forEach>

<tr>
<th colspan="3" style="background:orange">
<b>Application Scope Attributes</b>
</th>
</tr>
<c:forEach items="${applicationScope}" var="itm">
<tr>
<td><c:out value="${itm.key}"/></td>
<td colspan="2">
<c:choose>
<c:when test="${itm.key eq 'org.apache.struts.action.
PLUG_INS'}">
<c:forEach items="${itm.value}" var="subitm">
<textarea rows="2" cols="50">
<c:out value="${subitm}"/>
</textarea>
</c:forEach>
</c:when>
<c:otherwise>
<textarea rows="2" cols="50">
<c:out value="${itm.value}"/>
</textarea>
</c:otherwise>
</c:choose>
</td>
</tr>
</c:forEach>
</table>







Discussion



During development, JSP pages commonly show incorrect data, to render
halfway, or to throw an exception. Developers troubleshoot these
problems by commenting out portions of the page that appear to be
causing the problem, and then entering scriptlet to display the
values of request and session attributes used on the page. This is
the equivalent of commenting out troublesome Java code and adding
System.out.println( ) statements.



This solution doesn't replace this arcane approach
but makes it easier to apply. You have to comment out the failing
portion of the page, but you can use the JSP fragment in Example 13-5 to eliminate the handcoded scriptlet. This
reusable JSP fragment, derived from work by James Mitchell, displays
request headers, scoped attributes, and other Struts-specific
information to help you debug your problem. You can include the
fragment anywhere on the page though, typically, it would be placed
near the bottom of the page using the include directive:



...
<!-- Debug Data -->
<%@ include file="debug.jsp" %>
<!-- End Debug Data -->
</body>
</html:html>




The debug.jsp displays the following
data:



  • Request parameter values

  • Request header values

  • Application initialization parameters

  • Cookies

  • Page-scope attributes

  • Request-scope attributes

  • Session-scope attributes

  • Application-scope attributes


You can modify debug.jsp to display custom
attributes that you may use in your application. Figure 13-6 shows a sample of the displayed information
when debug.jsp is included at the bottom of the
Registration.jsp file from the
struts-example web application.




Figure 13-6. Request parameters and header values




The debug.jsp page displays detailed information
about cookies, as shown in Figure 13-7.




Figure 13-7. Cookie details and page-scoped attributes




The debug.jsp page fragment should only be used
in development; leaving this information displayed in production will
make you look silly and will open a major security hole.





See Also



The debug.jsp page uses JSTL. You can use JSTL
tags in your own application using the Solution shown in Recipe 3.1.












    No comments:

    Post a Comment