Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

JSP Debugging: How to Debug JSP

In this article we will see the debugging process in a Java Server Page (JSP) based application development environment.

Debugging is a very important and an integral part of application development using any programming language. Basically there are three types of error present, 1st is logical error, 2nd is runtime error, and 3rd is compiler error.

In this section we will discuss debugging procedure interrelated with the compiler error. It is very much important for us to fix all the syntax errors, and upgrade the process. The logical errors, such as not taking care of all possible input combinations, can easily slip into an application during the development.

Debugging process

The process of finding and correcting this type of problem is called debugging. Debugger tools help us to step through the program line by line or run the program until it reaches a break point that we have defined and the agreement checks or inspects the values of the variables in the program model. With the very detailed analysis of the programming flow in runtime, we can also discover that it works the way it does, and not the way we want it to. There are debuggers for the Java Server Page as well, such as International Business Machine's graphical age for Java developing process. This product helps us to debug a Java Server Page. The point should be remembered that a real debugger is often heavy for the Java Server pages. If our pages are so complex that we feel the need for a debugger process, we usually want to move the code from the JSP pages into JavaBeans or the custom actions as an alternative. These types of components can then be debugged with a standard Java debugger, which can be found in most Java IDEs. To debug a Java Server Page, different time tested debugging approach is customarily appropriate by simply adding the code to print variable values to the output screen.

Here we will give a simple illustration of the Logical error (error.jsp). Through this code any error can go directly to the errorPage.jsp page.

Listing 1: Sample showing error page handling

  <%@ page contentType="text/html" %>
  <%@ taglib prefix="c" uri="http://java.sun.com/jstl2/core" %>
  <%@ taglib prefix="oradata" uri="oradatataglib" %>
  <html>
  <head>
  <title>Student Information Entry Form…</title>
  </head>
  <body bgcolor="lightyellow">
  <form action="errorPage.jsp" method="post">
  <input type="hidden" name="submitted" value="true">
  <table>
  <c:if test="${param.submitted||empty param.userName}">
  <tr><td></td>
  <td colspan="2"><font color="green"> Enter Student Name.. </font></td></tr>
  </c:if>
  

Please remember that the value which we have entered in the name field also displays the error message. In this way to find the errors on the page we can complement a few actions that include the parameter values and the value of the to check the error in the program. Here is an illustration.

Listing 2: Sample showing actions

  <%@ page contentType="text/html"%>
  <%@ sgclib prefix="c" uri="http://java.sun.com/jstl2/core" %>
  <%@ sgclib prefix="oradata" uri="oradatasgclib"%>

  ${param.submitted}: <c:out value="${param.submitted}"/><br>
  ${param.userName}: <c:out value="${param.userName}"/><br>
  ${param.submitted || empty param.userName}:
  <c:out value="${param.submitted || empty param.userName}" />
  <html>
  <head>
  <title>Student Information Entry Form….</title>
  </head>
  <body bgcolor="lightyellow">
  <form action="validateting_jsppage.jsp" method="post">
  <input type="hidden" name="submitted" value="true">
  <table>
  <c:if test="${param.submitted||empty param.userName}">
  <tr><td></td>
  <td colspan="2"><font color="lightgreen">Enter Student Name…..</font></td></tr>
  </c:if>
  

Process to deal with Runtime Errors:

Generally, our application will work with debugging process but it is also important to predict the runtime errors in the system. Nevertheless these things can still go wrong due to the complications with external systems on which our application depends on, such as a database. Even though we have tested and debugged our application, there may be runtime circumstances which we did not consider.

The JSTL actions also act with most of the situations, for instance the take no action when the items of the characteristic goes to null or vacant. The standard Java applications do this to throw an exception. Similarly Java Server Page performs actions and it can throw exceptions when something goes really unscrupulous. Using the natural way the Java Server Page catches the exception and displays its message and stack trace on the browser (web page accessing software).

Generally, the exception messages may reveal penetration information from a lot of security point of view, such as file paths and Structure Query Language (SQL) statements etc. We can also present it in a much more user-friendly way by telling the Java Server Page container to use a customized error page instead.

After describing the above scenario, we will now discuss an illustration that shows a Java Server Page with a page directive, that defines an error page process.

Please follow the steps below to create the application using NetBeans IDE.

STEP - 1

At first open NetBeans IDE 7.2.1. Although you can use any other IDE for doing the process

Opening IDE

Figure 1: Opening IDE

STEP - 2

In the second step we just give the Project Name and set the path.

Set project name and
path

Figure 2: Set project name and path

STEP - 3

In third step we write down the program here.

Listing 3: Sample showing application

  <%@ page errorPage="errorpage.jsp?debug=log" %>
  <c:set var="sourcePage" scope="request" value="${pageContext.request.requestURI}" />
  <html>
  <head>
  <title>Calculator</title>
  </head>
  <body bgcolor="lightyellow">
  <jsp:useBean id="calculation" class="com.oradata.jsp.beans.calculation.CalculateBean">
  <jsp:setProperty name="calculation" property="*" />
  </jsp:useBean>
  <%-- Calculate the new numbers and state info --%>
  <c:set var="currentNumber" value="${ calculation.currentNumber}" />
  <form action="calculate.jsp" method="post">
  <table border=1>
  <tr>
  <td colspan="4" align="right">
  <c:choose>
  <c:when test="${currentNumber == ''}">
   
  </c:when>
  <c:otherwise>
  <c:out value="${currentNumber}" />
  </c:otherwise>
  </c:choose>
  <input type="hidden" name="presentNumber" value="<c:out value="${presentNumber}" />">
  <input type="hidden" name="beforeNumber" value="<c:out value="${ calculation.Number}" />">
  <input type="hidden" name="currentOperation" value="<c:out value="${ calculation.currentOperation}" />">
  <input type="hidden" name="reset" value="<c:out value="${ calculation.reset}" />">
  </td>
  </tr>
  <tr>
  <td><input type="submit" name="digit" value=" 7 "></td>
  <td><input type="submit" name="digit" value=" 8 "></td>
  <td><input type="submit" name="digit" value=" 9 "></td>
  <td><input type="submit" name="oper" value=" / "></td>
  </tr>
  <tr>
  <td><input type="submit" name="digit" value=" 4 "></td>
  <td><input type="submit" name="digit" value=" 5 "></td>
  <td><input type="submit" name="digit" value=" 6 "></td>
  <td><input type="submit" name="oper" value=" * "></td>
  </tr>
  <tr>
  <td><input type="submit" name="digit" value=" 1 "></td>
  <td><input type="submit" name="digit" value=" 2 "></td>
  <td><input type="submit" name="digit" value=" 3 "></td>
  <td><input type="submit" name="oper" value=" - "></td>
  </tr>
  <tr>
  <td><input type="submit" name="digit" value=" 0 "></td>
  <td> </td>
  <td><input type="submit" name="dot" value=" . "></td>
  <td><input type="submit" name="oper" value=" + "></td>
  </tr>
  <tr>
  <td> </td> 
  <td> </td>
  <td><input type="submit" name="clear" value=" C "></td>
  <td><input type="submit" name="operation" value= " = "></td>
  </table>
  </form>
  </body>
  </html>
  

Showing code in the IDE window

Figure 3: Showing code in the IDE window

Using the above program we can easily generate a simple calculator. This simple calculator is developed by following the output and if any error occurs in the page, it will redirect to the errorpage.jsp. Normal calculation is done by the calculate.jsp page. Basically this errorpage.jsp page is used to check out the arithmetic error.

Sample output

Figure 4: Sample output

The errorPage attribute in the page directive specifies the path for the page to be displayed if an exception is thrown by any Java Server Page element. At this moment the path is specified in the illustration, and the error page must be found in the same directory as the page that references it.

Nevertheless, when it starts with a font slash (/), it is interpreted in a context-relative way, and relative to the application's context path. This means we can define a common error page for all the Java Server pages in a simple application, even if we can place them in multiple subdirectories using a path such as /errorpagedata.jsp. Remember that the error pages Uniform Resource Interface (URI) includes a query string with the debug parameter, and that a action sets a request scope variable here. Following syntax shows the details.

  <%@ page errorPage="errorpagedata.jsp?debug=log" %>
  <c:set var="sourcePage" scope="request"
  value="${pageContext.request.requestURI}" />
  

The debug parameter uses the action to log the information about what went wrong in the error page. The sourcePage is a variable, set to the Uniform Resource Interface for the current pages. The rest of the page in Illustration implements a simple calculator. It is proposed to illustrate how an error page handles its work. In that situation a user tries to divide a quantity by zero, the CalculateBean used in this page throws an exception. This produces the error page which is shown in the illustration below.

This is the standard Illustration for fetching the Error page generated by the user

Listing 4: Sample showing fetching errors

  <%@ page contentType="text/html" %>
  <%@ taglib prefix="c" uri="http://java.sun.com/jstl2/core" %>
  <%@ taglib prefix="oracle" uri="oradatataglib" %>
  <%@ page isErrorPage="true" %>
  <html>
  <head>
  <title>Sorry for the Reason….</title>
  </head>
  <body bgcolor="lightyellow">
  Warning Message: We are sorry for this matter but the request could not be processed. The Detailed information about the error has been logged. We will investigate and rectify it as soon as possible.
  <p>
  Please try again, and <a href="mailto:mail2sgc@sgc.com">let us know</a> if the problem persists.
  <ora:fileWrite fileName="log">
  Error in: <c:out value="${sourcePage}" />
  Error message: <c:out value="${pageContext.exception.message}" />
  </oradata:fileWrite>
  <oradata:debug type="params" />
  </body>
  </html>
  

The above program uses a page directive with the attribute isErrorPage set to true. This tells the process that the exception of the property of the implicit pageContext variable should be prepared with a standard reference to the exception that caused the page to be invoked here. The type of exception object is java.lang.Throwable. This class also provides a property named message that contains a message about what went wrong. It is written to the application log file together with the source Page. The variable created in this illustration, using a combination of the the convention action and the Java Server Pages Standard Tag Library action. All the request parameters are then written to the log file using the custom action. Through this way the information about which page caused the exception was also thrown here, and all the parameter values that were acknowledged with the request causing the problem is logged in the application log file when something unexpected happens. In this situation we can also look at the log file from time to time to see what kind of issues may occur frequently, and confidently fine-tune the application to avoid them or at least provide more unambiguous error messages. The user is not interested in any of these specifications, nevertheless we want to be assured that the problem is being registered and also corrected.

Listing 5: Sample showing exception type

  <error-page>
  <exception-type>java.lang.Throwable</exception-type>
  <location>errorpage.jsp</location>
  </error-page>
  <error-page>
  <error-code>8000</error-code>
  <location>errorpage.jsp</location>
  </error-page>
  

The element contains an or an element, with element of a , context-relative path for the servlet, Java Server Page, or the static page to handle the debugging error. Basically the element contains fully practiced name of the type of exception we want to handle. In this position the element contains the Hypertext Transfer Protocol (HTTP) response status code to handle all general types of exception. We can also include multiple elements to use in different pages for different exceptions and status codes. Here in the element, the container picks one of the most closely matched types, while it uses an exact match for the element.

Generally an error page declaration in the deployment descriptor applies to all the resources in the application. When an errorPage attribute is specified in the Java Server Page, it is also used in its place of one declared in the deployment descriptor. At the scene of an unfortunate naming mismatch between the servlet and Java Server Page specification, there is one problem with this approach, if we use a Java Server Page to control the exception. This exception property of the implicit pageContext variable is not initialized so we never log or display the exception message as in illustration.

Conclusion

Java server page is one of the most widely used technologies for web application development. It is used in most of the Java/J2ee based enterprise applications. So the debugging of JSP applications is very important during application development process. In this article we have tried to explain most of the exception and error situations. Here we have also discussed with some example the process to send error from one page to another. We also know that lots of runtime errors are there which should be handled in various ways. It is also true that these runtime errors cannot always be solved. But it is true that all types of debugging errors can easily be checked through the above process and we can deliver a good and standard web application to the users.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login