Easy Logging from a JSP Page

Posted on June 5th, 2013 by Fernando Zamora

Recently I was debugging an issue in a Java Server Page (JSP) and I wanted to track the logic path. I didn’t want to add the overhead of logging to the JSP. Logging involves adding logging such as Log4J and then checking the log files. I wanted something much simpler like the alerts you use for debugging purposes from time to time in JavaScript. Except JSP is server side code so alert boxes or any other kind of boxes are out of the question.

out To the Rescue

To solve my problem I used the out instance in the JSP instance to send HTML comments to the page.

I had something like the following:


   ....code
   if(someCondition == true){
           //Code to do something
   }
   ....more code

I wanted to know if my code was stepping into the condition. Later I wanted to know more information such as variable values and such but that’s this enough to get you to see how you can add onto this example to get there.

So what I did was modify the code in the following manner:


   ....code
   if(someCondition == true){
           //Code to do something
          out.println("<!--Entered someCondition equals true-->");
   }
   ....more code

Notice the addition of the out.pintln statement. That resulted in HTML comments in the code. So then when I view the source in my browser I can see the comments left behind for tracing.

This is not an ideal solution and purists may balk at “such abuse” but it provided the easy solution that I needed so that I could focus on the actual bug instead of the debugging/logging infrastructure.

One Response to Easy Logging from a JSP Page

  • Mario Cortez
    June 5, 2013

    Fernando,

    Thank you for the tip.

Leave a Reply




Post Comment

Connect With Us

Recent Posts

A Guide for Learning Design Patterns

July 28th 2016 by Fernando Zamora If you’ve been writing code for more than a year, you might have h...

Read More

Using UML to Analyze Legacy Code

June 30th 2016 by Fernando Zamora For every programmer out there creating brand new code and working...

Read More

Python vs. Other Languages

April 29th 2016 by Fernando Zamora For the last two months or so my fellow McLane Advanced Technolog...

Read More

Naming Your Adapter When Implementing the Adapter Pattern

October 19th 2015 by Fernando Zamora At some point in your project you may need to use a third party...

Read More

10 Methods to Help You Debug Legacy Code

September 24th 2015 by Fernando Zamora A large majority of the work we do in our project is to fix r...

Read More