Debugging applications for GlassFish Server

From IntelliJ-Wiki

Jump to: navigation, search

This is the second tutorial in the series dealing with integration of IntelliJ IDEA with GlassFish Server. So it is assumed that you have already studied the previous tutorial Developing applications for GlassFish Server and completed all the tasks contained therein. If not, please, do so first.

This tutorial discusses:

  • Preparatory steps. These are the actions needed to get to the state which you were at when completing the previous tutorial. If you have just finished "Developing applications for GlassFish Server" and haven’t yet quit IntelliJ IDEA, skip this section.
  • Spoiling the source code and observing the application behavior. This section shows how to "spoil" the source code developed before to make the sample application behave peculiarly.
  • Debugging the sample application. In this section you’ll find explanation and instructions for the most typical debugging tasks.

Contents

Preparatory steps

To get to the state you were at when completing the previous tutorial, do the following:

1. Start IntelliJ IDEA.

2. Open your sample project (presumably, MetersToInchesConverter).

3. If the file result.jsp is not already open in the editor, open it.

Once you've done that, you are ready to start modifying the code.

Opening the sample project

To open your sample project:

  • If no project is currently open in IntelliJ IDEA, click MetersToInchesConverter in the Recent Projects pane on the Welcome screen. Otherwise, select File | Reopen | MetersToInchesConverter.

If the sample project is not present in the list of recently used projects, try opening the project like this:

1. Click Open Project on the Welcome screen, or select Open from the File menu.

2. In the dialog that opens, find and select your project, and click OK.

Opening result.jsp

To open the file result.jsp in the editor:

  • In the Project tool window, double-click the file. (Alternatively, select the file and press F4.)

Spoiling the source code and observing the application behavior

To show how to debug GlassFish Server-oriented applications in IntelliJ IDEA, we have slightly “spoiled” the source code of the sample application. We made one simple intentional mistake in result.jsp, the file that is responsible for the main part of the application logic. As a result, the application behavior has become somewhat peculiar.

Now we suggest that you do the same, namely:

1. Modify result.jsp

2. Study the application behavior

After that we will debug the application.

Modifying result.jsp

Replace the <body> element (<body>...</body>, inclusive) with the following:

    <body>
        <%
            String headingText;
            String messageText;
            String linkText;
            double conversionFactor = 39.37;
            String metersToConvertString = request.getParameter( "meters" );
            try {
                double metersToConvertDouble = (int) Double.parseDouble(metersToConvertString);
                double inchesDouble = metersToConvertDouble*conversionFactor;
                String inchesString = Double.toString(inchesDouble);
                headingText = "CONVERSION RESULT";
                messageText = metersToConvertString + " m = " + inchesString + " inches";
                linkText = "Convert another number";
            } catch (NumberFormatException nfe) {
                headingText = "CONVERSION FAILED";
                if (metersToConvertString.isEmpty()) {
                    messageText = "You forgot to specify the number of meters.";
                } else {
                    messageText = "\"" + metersToConvertString + "\" is not a number.";
                }
                linkText = "Try once more";
            }
        %>
        <p><%= headingText %></p>
        <p><%= messageText %></p>
        <p><a href="index.jsp"><%= linkText %></a></p>
    </body>

Studying the application behavior

To run the sample application and study its behavior, do the following:

1. Click Run File:I12RunIcon.png to the right of the run configuration selector on the toolbar. (Alternatively, press SHIFT+F10.)

File:I12GF29Run.png

After a little while, the default Web browser starts and goes to http://localhost:8080/MetersToInchesConverter_war_exploded/. As a result, the starting page of the sample application is displayed.

File:005AppInBrowserGFE1.png

2. Leave the field next to Enter amount in meters (m) empty and click Convert into inches.

The browser switches to result.jsp, and you see the following:

File:006ResultForNoInputGFE1.png

This is fine: we really "forgot" to specify a number on the previous page.

3. Click Try once more to come back onto the starting page.

4. Type one in the input field and click Convert into inches.

File:007InputOneGFE1.png

The application responds with the following screen:

File:008ResultForOneGFE1.png

This response is reasonable: the string "one" is indeed not a number.

5. Now try to convert 1 meter into inches.

File:009Input1GFE1.png

The result is again satisfactory:

File:010ResultFor1GFE1.png

6. Let’s see the result of conversion for 1.5 meters.

File:011Input1_5GFE1.png

The application response is somewhat strange:

File:012ResultFor1_5GFE1.png

The result is the same as for 1 meter!

7. Try other numbers. Looks like there is a problem with non-integers, doesn’t it?

8. Close your Web browser (or the tab where you were working with the sample application).

9. Switch to IntelliJ IDEA, undeploy the artifact (on the Deployment tab in the Run tool window, click the artifact, and then click File:I12UndeployIcon.png) and stop the server (click File:I12StopIcon.png or press CTRL+F2).

Now we are going to study our application in more detail by running it in the debugger.

Debugging the sample application

The debugging workflow in IntelliJ IDEA normally includes performing the following typical tasks:

1. Starting the application in the debug mode.

2. Inserting one or more breakpoints in the source code.

3. Executing the code line by line to get a detailed picture of what is going on inside the application.

4. Correcting the source code if a problem has been found.

5. Resuming the application to get to the next of the breakpoints or see how the application behaves after the part you have scrutinized (and possibly corrected).

6. Studying the application behavior after the code correction to check if the problem has been eliminated.

The following sections provide the corresponding how-to examples.

Starting the application in the debug mode

To debug the sample application, we’ll use the same run/debug configuration as for running the application. Once we have fixed the debug settings, the run/debug configuration can be used as is.

To start debugging the sample application:

  • Click the Debug icon File:I12DebugIcon.png on the toolbar. (Alternatively, press SHIFT+F9.)

File:I12GF01Debug.png

The debugger starts. As you can see in the Output pane of the Debug tool window, GlassFish Server is started and the application artifact is deployed to the server. Then, the starting page of the sample application is opened in the Web browser that is started automatically.

Inserting a breakpoint in the code

Now let’s insert a breakpoint into result.jsp.

Because the executable part of the page is very small, we can limit ourselves to only one breakpoint and insert it at the very start, before the first of the assignment operators (double conversionFactor = 39.37;).

To insert a breakpoint:

1. Switch to IntelliJ IDEA.

2. In the editor, click the gutter area next to the line double conversionFactor = 39.37;

File:I12GF04LineForBreakPoint.png

As a result, a red spot appears indicating the breakpoint that you have set.

File:I12GF05BreakPointInserted.png

Executing the code line by line

Now let us execute the code line by line:

1. Make the sample application run. To do that, switch to the Web browser. On the starting page of the sample application, type 1.5 in the field next to Enter amount in meters (m), and click Convert into inches.

As a result, the input data is submitted to the server. The control of the application is passed to result.jsp. When the breakpoint is reached, the application is paused.

2. Switch back to IntelliJ IDEA. Note that now the line double conversionFactor = 39.37; in result.jsp is highlighted blue. This means that the application is paused at the beginning of this line.

File:I12GF06BreakPointReachedEditor.png

3. To execute the current line of code, click the Step Over icon File:I12StepOverIcon.png on the toolbar of the Debug tool window. (Alternatively, press F8.)

File:I12GF07DebugStepOver.png

Look at the result.

Note that the blue highlight in result.jsp has moved from double conversionFactor = 39.37; onto the next line String metersToConverString = request.getParameter ( "meters" );

File:I12GF08LineExecutedEditor.png

At the same time, a new entry appeared in the Variables pane (conversionFactor = 39.37).

File:I12GF09LineExecutedDebugger.png

This means that one line of code has been executed, and, as a result, the variable conversionFactor has been assigned 39.37 as its value.

4. Click File:I12StepOverIcon.png (or press F8) once more.

Another line of code is executed.

File:I12GF10LineExecuted2Editor.png

As a result, the number of meters submitted to the server (1.5) is successfully retrieved from the corresponding request (request.getParameter( "meters" )) and assigned to metersToConvertString.

File:I12GF11LineExecuted2Debugger.png

5. Click File:I12StepOverIcon.png.

The next line of code is executed.

File:I12GF12LineExecuted3Editor.png

See that metersToConvertDouble has been assigned 1.0 (!)

File:021AfterThirdStepOverInDebugTWGFE1.png

The code line that has been just executed may be the one that causes the application to produce an incorrect conversion result. Let’s move one more line forward.

6. Click File:I12StepOverIcon.png.

Here is what you see in the editor:

File:I12GF14LineExecuted4Editor.png

Note that inchesDouble = 39.37 (= metersToConvertDouble*conversionFactor = 1.0*39.37).

File:I12GF15LineExecuted4Debug.png

This is the value that after conversion to string will be shown in the sample application as the result. You can check that this is the case by executing the remaining lines of code one by one until the end:

7. Keep clicking File:I12StepOverIcon.png (or pressing F8) until you reach the line containing %>. Observe the result after each click.

Because we’ve already found the code line that causes the problem (double metersToConvertDouble = (int) Double.parseDouble(metersToConvertString);), we can now make the necessary correction. Then, we will resume the application and repeat the debugging cycle to make sure that the problem has been eliminated.

Correcting the code

In the line of code

double metersToConvertDouble = (int) Double.parseDouble(metersToConvertString); 

delete (int). (We don’t want to cast the value to integer, to say the least.)

The resulting line should look like this:

double metersToConvertDouble = Double.parseDouble(metersToConvertString);

Now let’s see if this change has eliminated the problem in the application behavior.

Resuming the application

To resume the application:

  • In the Debug tool window, click the Resume Program icon File:I12ResumeIcon.png. (Alternatively, press F9.)

File:023DebugTWResumeAppGFE1.png

Studying the application behavior after the code correction

To check the application behavior:

1. Update the application (e.g. File:icon_update.png) and switch to the browser.

2. On the page that is currently shown (1.5 m = 39.37 inches), click Convert another number.

3. On the starting page, type 1.5 in the input field and click Convert into inches.

The application is again suspended at the beginning of the corresponding code line.

4. Switch into IntelliJ IDEA. Execute the code line by line as we did before.

After executing the line double inchesDouble = metersToConvertDouble*conversionFactor; note that now the input number of meters (1.5) has been converted into inches properly (1.5*39.37≈59.055).

File:I12GF16ErrorFixed.png

5. Resume the application and switch to the browser to see the same result.

File:025ResultInBrowserGFE1.png

It looks like everything is fine. We’ve eliminated the problem and can now undeploy the application, stop the server and close the Debug tool window.

Undeploying the application artifact

To undeploy the application artifact:

  • In the Debug tool window, click the Server tab (1), click the artifact (2), and click the Undeploy icon File:I12UndeployIcon.png (3).

File:I12GF17Undeploying.png

Stopping the server

To stop the server:

  • In the Debug tool window, click the Stop icon File:I12StopIcon.png. (Alternatively, press CTRL+F2.)

File:I12GF18Stopping.png

Closing the Debug tool wind

To close the Debug tool window:

  • Click File:I12CloseIcon.png. (Alternatively, press CTRL+F4.)
Personal tools