Search code examples
javadebuggingjsptomcatjdb

Debugging a Java program running from Tomcat (JSP)


I don't know why I never found myself having to use a debugger to step through my program and see what was going on, probably because I'm used to using interpreted languages such as PHP where it becomes very easy to add debugging code (print_r) and see changes live.

However with this new Java project I feel like I must learn the correct ways of debugging.

So this program, that I didn't write, runs on Tomcat and uses basic JSPs. The issue is that when I try to access a specific JSP page it throws an exception and gives me the stacktrace of what happened:

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:503)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:363)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:306)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.sgrp.singer.filters.SingerLoginFilter.doFilter(SingerLoginFilter.java:128)

How would I step through my program using a tool such as JDB? I can't really step through a specific class because I need to mimic what my JSP is doing... I would like to do this through the command-line, without using an IDE.


Solution

  • An alternative solution that may be easier to use than hooking a debugger to Tomcat:

    First, take a look at the call stack. At the bottom, you'll see your class named org.sgrp.singer.filters.SingerLoginFilter. The problem lies here, at line 128 of method doFilter.

    The first line says org.apache.jasper.JasperException: java.lang.NullPointerException. That means you've used an object whose value is null at line 128 of mentioned class.

    Check out that code to find out what could be wrong. Also, add some print/logging statements to your code.

    Debugging should be your last resort. You can gather a lot of information just by looking at your stack trace.