1. Trang chủ
  2. » Công Nghệ Thông Tin

WebSphere Studio Application Developer Version 5 Programming Guide part 59 pot

10 102 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

554 WebSphere Studio Application Developer Version 5 Programming Guide Debugging a Web application In this section, we show how to debug a servlet running as part of a Web application. We will be using the ListAccounts.java servlet in the ItsoProGuideBasicWeb project for our example. This is the servlet that was developed in “Creating dynamic Web resources” on page 203. Setting breakpoints in a servlet Breakpoints are indicators to the debugger that it should stop execution at specific places in the code, and let you step through it. Breakpoints can be set to trigger always or when a certain condition has been met. To add a breakpoint in the code, do the following:  Open the ListAccounts.java in the Java editor.  Place your cursor in the gray bar (along the left edge of the editor area) on the line Banking banking = new Banking().  Double-click to set a breakpoint (Figure 16-1). A marker will be shown at the line. Figure 16-1 Add a breakpoint Chapter 16. Testing and debugging 555 Right-click the breakpoint in the breakpoint view, and select Breakpoint Properties from the context menu. A window opens where more detailed options about the breakpoint are displayed (Figure 16-2). Figure 16-2 Breakpoint properties The Enable Hit Count property, when set, causes the breakpoint to be triggered only when the lines has been executed as many times as the hit count specified. Once triggered, the breakpoint is disabled. The other property of interest here is Enable Condition . If set, then the breakpoint is reached only when the condition specified in the entry field evaluates to true. This condition is a Java expression. In our case, select Enable Condition and enter customerNumber.equals("104"); as the condition. The breakpoint will only be reached when you enter 104 as the customer ID. The Restrict to Selected Thread(s) list is only filled if a server is running in debug mode already, otherwise the list in empty. Note: Enabled breakpoints are indicated with a blue circle. If the enabled breakpoint is successfully installed in a class in the VM at runtime, it is indicated with a check mark overlay. 556 WebSphere Studio Application Developer Version 5 Programming Guide Click OK to close the breakpoint properties. In the breakpoints view, the breakpoint is now marked with a question mark , which indicates that it is a conditional breakpoint. Setting breakpoints in a JSP You can also set breakpoints in the JSP source code. However, you can only set breakpoints in Java code and <jsp> tags. Open the listAccounts.jsp that will be called from the ListAccounts servlet. Set a breakpoint as shown in Figure 16-3. Figure 16-3 Setting a breakpoint in a JSP You could also set a breakpoint in the lines with <jsp.getProperty> tags. Sometimes reformatting the source code to have only one statement per line is better for debugging. Chapter 16. Testing and debugging 557 Testing the application with breakpoints enabled Once you have set the breakpoint, the Web application can be started for debugging. From the Web perspective, bring up the context menu on ItsoProGuideBasicWeb and select Debug on Server (Figure 16-4). Figure 16-4 Debug Web project on server Application Developer switches to debug perspective automatically, if the preference to do so is set. In the Debug perspective, you should now see index.html displayed in the Web Browser view, as shown in Figure 16-5. Enter 104 as the customer ID. Note: The server used for testing, for example ItsoServer, must be either stopped or started in debug mode. Otherwise, an error message will be displayed. Tip: To debug a non-Web Java application, select Run -> Debug As -> Java Application to start the debugger. This is the only difference between debugging a Web application and debugging a Java application. 558 WebSphere Studio Application Developer Version 5 Programming Guide Figure 16-5 Web browser in the Debug perspective Click Submit to display the list of accounts. You are prompted to step into the itso.basicweb.control.ListAccounts.doPost method. This is a feature in the debugger to allow you to debug code without setting breakpoints. It can be disabled by clicking Enable/Disable step-by-step debugging . Select the Skip radio button, then check the Disable step-by-step mode check box, and click OK (Figure 16-6). Chapter 16. Testing and debugging 559 Figure 16-6 Disable step-by-step debugging After step-by-step has been disabled, the servlet is executed. As soon as the breakpoint in ListAccounts.java is reached, execution stops and the ListAccounts.java source file is displayed with the line containing the break point highlighted. The thread is suspended in debug, but other threads might still be running (Figure 16-7). Figure 16-7 Debug perspective when stopped at a breakpoint 560 WebSphere Studio Application Developer Version 5 Programming Guide Next we discuss the different views of the Debug perspective. Debug view with stack frames When a breakpoint is reached, the debugger displays a list of stack frames before the breakpoint occurred. Each frame corresponds to a called method. The entire list is in reverse chronological order. Figure 16-8 shows the stack frame listing for the breakpoint in the ListAccounts class, doPost method. Figure 16-8 Stack frame listing in Debug view When a thread suspends, the top stack frame is automatically selected. If you select another stack frame, all visible variables in that frame are shown in the Variables view. Debug functions From the Debug view, which should now be displayed in the top left pane, you can use the functions available from its icon bar to control the execution of the application. The following icons are available:  Resume: Runs the application to the next breakpoint  Suspend: Suspends a running thread  Terminate: Terminates a process  Disconnect: Disconnects from the target when debugging remotely  Remove All Terminated Launches: Removes terminated executions Chapter 16. Testing and debugging 561  Step Into: Steps into the highlighted statement  Step Over: Steps over the highlighted statement  Step Return: Steps out of the current method  Step Debug: Only for compiled languages (steps to next statement)  Show Qualified Names: Toggle option to show the full package name. In the upper right pane you can see the various debugging views that are available. Breakpoints view The Breakpoints view displays all the breakpoints set in the Workbench. (Figure 16-9). Figure 16-9 Debugging views You can use the breakpoints view to display and manipulate the breakpoints that are currently set. You can open the properties (for example to set the hit count), remove the breakpoint, or open its source file. Watching variables The Variables view displays the current values of the variables in the selected stack frame. Follow these steps to see how you can track the state of a variable. Click the Step Over icon to execute the current statement. Note that a new variable banking has been added to the Variables view. Click Step Over again and the customer variable is added. The plus sign (+) next to a variable indicates that it is an object. Expand the customer to reveal the values of its attributes (Figure 16-10). 562 WebSphere Studio Application Developer Version 5 Programming Guide Figure 16-10 Displaying variables If you want to test the code with some other value for any of these instance variables, you can change one of them by selecting Change Variable Value from its context menu. An entry field opens where you can change the value; for example, you can change the last name to upper case (Figure 16-11). Figure 16-11 Changing a variable value Inspecting variables To view more details about a variable, select a variable and Inspect from the context menu. The result opens in expressions view (Figure 16-12). Both the Variables and Expressions view can be split into two panes by selecting Show Detail Pane from the context menu. Chapter 16. Testing and debugging 563 Figure 16-12 Inspecting a variable in Expressions view Evaluating an expression To evaluate an expression in the context of the currently suspended thread, use the Display view. Enter the expression customer.getFirstName(), then highlight the expression and select Display from the context menu. Do the same for the expression banking.getTransactions("104-4001"). Each expression is executed and the result is displayed (Figure 16-13). Figure 16-13 Expression and evaluated result in display view The results of the Java expression can also be inspected by selecting Inspect from the context menu. You can also highlight any expression in the source code and select Display or Inspect (context). The result is shown either in the Display or Expressions view. For example, highlight the expression banking.getCustomer(customerNumber) in the source code and select Inspect (context). The result is displayed in the Expressions view (Figure 16-14). This is a useful way to evaluate Java expressions during debugging, without having to make changes in your code and recompile. Details pane . 55 4 WebSphere Studio Application Developer Version 5 Programming Guide Debugging a Web application In this section, we show how to debug a servlet running as part of a Web application. . the VM at runtime, it is indicated with a check mark overlay. 55 6 WebSphere Studio Application Developer Version 5 Programming Guide Click OK to close the breakpoint properties. In the breakpoints. debugging a Web application and debugging a Java application. 55 8 WebSphere Studio Application Developer Version 5 Programming Guide Figure 16 -5 Web browser in the Debug perspective Click Submit to

Ngày đăng: 03/07/2014, 20:20

Xem thêm: WebSphere Studio Application Developer Version 5 Programming Guide part 59 pot