Pr.Pg Next Pg

Servlets Debugging tutorials

  • Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a servlet program or any other computer program.

  • Debugging servlet or any other server side programming language is quite difficult because they tend to involve a large amount of client/server interaction.

 

println

  • println is quite possibly the most useful debugging tool there is especially for the beginner programmer with no knowledge of other tools such as debuggers, assertions, and/or loggers. println prints to the command line the values passed to the method as an argument (if an object is passed, the String returned by Object.toString() is printed).

  • For debugging, this tool allows the programmer to evaluate a variable value at a certain position in the code. But that's only the beginning it can be used to evaluate if a conditional is met, how and where a variable is modified, whether a method has been entered, and used with conditionals to more comprehensively inspect value. 

  • Quite often the development process is iterative. Through successive rounds of modifying the code to add or remove print statements, followed by compiling and re-running the code, one can evaluate how the changes affect the printed values.

  • This is invaluable to a beginner - not only in helping one learn the basics through modifying the code and inspecting the outcome, but also in debugging to evaluate which values are expected and which are not, and move forward or backward through the code to see where a problem may lie.

 

 

Example: Servlet bug program

Sam Sir

//Program name Debugingtest.java

// Servlet bug program

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class Debugingtest extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

int[] values = { 15,6,5,2,10,78 };

int min = Integer.MAX_VALUE;

for ( int i = 0; i < values.length; i++ ){

if ( values[i] > min ){

min = values[i];

}

}

out.println("The min value is " + min);

}

}

Output

Servlet debugging

  • Note: There is an obvious bug in the code above, however for the sake of argument assume we know nothing. The above code prints out 2147483647.

  • Letís use some println's . This value is not even in our array, could it be the value of Integer.MAX_VALUE? Letís see in below example.

 

 

Example: Servlet bug program

Sam Sir

//Program name Debugingtest.java

// Servlet bug program

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class Debugingtest extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

int[] values = { 15,6,5,2,10,78 };

int min = Integer.MAX_VALUE;

for ( int i = 0; i < values.length; i++ ){

if ( values[i] > min ){

min = values[i];

}

}

out.println("The min value is " + min + "<BR>");

out.println(Integer.MAX_VALUE);

}

}

Output

Servlet debugging

  • Note Running the above code assures us that this is indeed Integer.MAX_VALUE - as this is our initial value we initialize min with, it looks that min might never be set in other words the condition never evaluates to true. In above code letís do one step further and add a println in our coditional, just to be sure it never evaluates to true

 

 

Example: Servlet bug program

Sam Sir

// Program name Debugingtest.java

// Servlet bug program

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class Debugingtest extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

int[] values = { 15,6,5,2,10,78 };

int min = Integer.MAX_VALUE;

for ( int i = 0; i < values.length; i++ ){

if ( values[i] > min ){

min = values[i];

}

}

out.println("The min value is " + min + "<BR>");

out.println(Integer.MAX_VALUE);

}

}

Output

Servlets Debugging

  • Note: Confirms the suspicion that our conditional is never met. How could this be the case? Looking closely at the conditional, we should (hopefully) see the greater than symbol. don't we want to find the minimum? 


 

Example: Servlet bug program

Sam Sir

// Program name Debugingtest.java

// Servlet bug program

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class Debugingtest extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

int[] values = {15,6,5,2,10,78};

int min = Integer.MAX_VALUE;

for ( int i = 0; i < values.length; i++ ){

if ( values[i] < min ){

min = values[i];

}

}

out.println("The min value is " + min + "<BR>");

out.println(Integer.MAX_VALUE);

}

}

Output

Servlets debugging

  • Note: From not having a clue where the error lies, to iteratively printing out values and backtracking through the code to determine where the problem resides. out.println - the best debugging tool for the beginner

Pr.Pg border                                              Next Pg