Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Sunday, July 29, 2007

Printing session attributes and request attributes in JSP for debugging

I generally like to use following JSP which I call req_session_attribute_printer.jsp for debugging purpose. I just include this jsp, where ever i want to debug, i have found this very useful over time for simple debugging.


<%--
req_session_attribute_printer.jsp
Author:<a href="mailto:get.anurag at gmail do com">Anurag Kumar Jain</a>
Date: July 25, 2007
--%>
<%@ page language="java" contentType="text/html; "%>
<script type="text/javascript">
function showHideAttributes(spanId){
if(document.getElementById(spanId).style.display=='block'){
document.getElementById(spanId).style.display='none';
}else if(document.getElementById(spanId).style.display=='none'){
document.getElementById(spanId).style.display='block';

}
}
</script>
<a href="#" onclick="showHideAttributes('debugSpan')"> Attributes</a>
<span id='debugSpan' style='display:none'>
Session Attributes:
<% java.util.Enumeration salist = session.getAttributeNames();
while(salist.hasMoreElements()){
String name=(String)salist.nextElement();
Object value = session.getAttribute(name);%>
<%=" "+name+"="+value %>
<% }%>
Request Attributes:
<% java.util.Enumeration ralist = request.getAttributeNames();
while(ralist.hasMoreElements()){
String name=(String)ralist.nextElement();
Object value = session.getAttribute(name);%>
<%=" "+name+"="+value %>
<% }%>
</span>



This is specially very helpful for debugging in environments other than dev, where you have less control.

Enabling Expression Language (EL) in JSP

You can do it in two ways:

  • Enable it for whole application : Put following in web.xml

    <jsp-config>
    <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <el-ignored>false</el-ignored>
    </jsp-property-group>
    </jsp-config>

  • Enable it for just one JSP page : put following at top of your JSP Page
    <%@ page isELIgnored="false" %>
Make sure when you use the first option you use 2.4 schema or above. i. e. your root tag in web .xml looks something like this.

< web-app id="WebApp" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

make sure you are using Jasper jars for JSP 2.0 or above.