Debugging applications for GlassFish Server
From IntelliJ-Wiki
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.
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, C:\IdeaProjects\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:
- Select File | Reopen and click MetersToInchesConverter.
If the sample project is not present in the list of recently used projects, try opening the project like this:
1. Select File | Open Project.
2. In the Open Project dialog, find the project MetersToInchesConverter in the file system, select it (1), and click OK (2).
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:
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 == "") {
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
to the right of the run configuration selector on the toolbar. (Alternatively, press SHIFT+F10.)
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.
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:
This is a satisfactory result: 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.
The application responds with the following screen:
This response is reasonable: the string “one” is indeed not a number.
5. Now try to convert 1 meter into inches.
The result is again satisfactory:
6. Let’s see the result of conversion for 1.5 meters.
The application response is somewhat strange:
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
) and stop the server (click
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
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. No changes are required to the debug settings provided by IntelliJ IDEA by default.
To start debugging the sample application:
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;
As a result, a red spot appears indicating the breakpoint that you have set.
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.
3. To execute the current line of code, click the Step Over icon
on the toolbar of the Debug tool window. (Alternatively, press F8.)
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” );
At the same time, a new entry appeared in the Variables pane (conversionFactor = 39.37).
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
(or press F8) once more.
Another line of code is executed.
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.
The next line of code is executed.
See that metersToConvertDouble has been assigned 1.0 (!)
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.
Here is what you see in the editor:
Note that inchesDouble = 39.37 (= metersToConvertDouble*conversionFactor = 1.0*39.37).
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 page line by line until the end:
7. Keep clicking
(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:
Studying the application behavior after the code correction
To check the application behavior:
1. Update the application 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).
5. Resume the application and switch to the browser to see the same result.
It looks like everything is fine. We’ve eliminated the problem and can now undeploy the application and stop the server.
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
(3).
Stopping the server
To stop the server:































