IntelliJ

 < Day Day Up > 



In this section, we explain the steps needed to prepare Idea's IntelliJ IDE for debugging a Struts application.

start sidebar

As you complete the following sections, notice that we include some text from the Eclipse sections of this chapter. This text is duplicated because it applies to both IDEs.

end sidebar

Configuring Tomcat

When debugging Web applications using Tomcat and IntelliJ, you have to make a few changes to the way Tomcat runs. This is because IntelliJ uses something called the Java Platform Debugger Architecture (JPDA).

start sidebar

If you are using a 4.0.x version of Tomcat, you may feel comfortable skipping this step. If so, you can move on to the next section, "Configuring IntelliJ."

end sidebar

The JPDA is a multi-tiered debugging architecture that allows you to debug Java applications using a standard protocol. The implementation of this architecture supported by IntelliJ allows you to attach to a debug process using a special port you define during the configuration of Tomcat. To configure Tomcat for JPDA support, you must complete these steps:

  1. Open catalina.bat or catalina.sh file (based on your operating system).

  2. Find the environment variable JPDA_TRANSPORT and change its value from dt_shmem to dt_socket.

  3. Find the environment variable JPDA_ADDRESS and change its value from dt_jdbcon to dt_5050.

  4. Make sure that you have the wroxapp installed to Tomcat.

  5. Start Tomcat (from the <TOMCAT_HOME>/bin directory) using the following command for Windows:

     catalina.bat jpda start 

and this command for UNIX:

 catalina.sh jpda start 

start sidebar

At this point, Tomcat should be properly configured. Let's move on to the following section, which focuses on setting up IntelliJ for Web application debugging.

end sidebar

Configuring IntelliJ

This section covers the configuration process for IntelliJ's Tomcat Plugin. IntelliJ, much like many other IDEs, comes packaged with a Tomcat Plugin. To use it, follow these steps:

The following steps assume that you are using IntelliJ 3.0.4.

  1. Acquire and install IntelliJ according to the documentation packaged with the IDE.

  2. Start the IntelliJ application. You should see a figure similar to the image in Figure 7.20.

    click to expand
    Figure 7.20: The IntelliJ IDE

  3. Select File >New Project.

  4. Enter the name wroxapp into the Name text box.

  5. Enter the location of your wroxapp. For our application this value is <TOMCAT_HOME>/webapps/wroxapp/wroxapp.ipr.

    start sidebar

    When working in a real development environment, you will want to locate your Web application outside Tomcat's webapps directory. If you choose to do this, be sure to add a <Context> entry to your Tomcat server.xml file that points to the location of your Web application.

    end sidebar

  6. Select the Target JDK. We are using 1.4.1_02.

  7. Enter the location of your compiler output path. In this case, you will want it to point to the classes directory, which is directly under the WEB-INF directory. Figure 7.21 shows the completed dialog box.

    click to expand
    Figure 7.21: The New Project wizard.

  8. Click the Next button twice. You can ignore the Project Path setting—the default will work fine.

  9. Now add all of your source file locations. In this example, you should have the JDK 1.4 and <TOMCAT_HOME>/webapps/wroxapp/WEB-INF/src. Figure 7.22 shows the current values associated with the wroxapp project.

    click to expand
    Figure 7.22: Our project's Source path settings.

  10. Now click Finish. You should now see your new project in the Project window, as shown in Figure 7.23.

    click to expand
    Figure 7.23: The wroxapp project.

  11. At this point, you have a valid IntelliJ project, but you may notice that some of the objects listed in your source window are displayed in red. This is because the project is missing some JAR files from its classpath. To make the appropriate additions, right-click the project name in the Project window and select Project Properties.

  12. Now select the Libraries tab. You should see the settings shown in Figure 7.24.

    click to expand
    Figure 7.24: The Project Properties dialog box, Libraries tab.

  13. Click the Add button. You should see the dialog box shown in Figure 7.25 (if you have not previously set up a library).

    click to expand
    Figure 7.25: The Library Selection dialog box.

  14. Click the Configure button.

  15. The Configure Libraries dialog box opens (see Figure 7.26).

    click to expand
    Figure 7.26: The Configure Libraries dialog box.

  16. Select the Plus symbol and enter the name Struts 1.1.

  17. Select the Classpath tab and click the Add button.

  18. Browse to the location of your wroxapp/WEB-INF/lib directory and select all of the JAR files in this directory. You should see a dialog box similar to Figure 7.27.

    click to expand
    Figure 7.27: The Select Path dialog box.

  19. Now click OK until you reach the Select Libraries dialog box.

  20. Click the Configure button again and repeat the same process, except this time name the library Servlets and browse to the servlet.jar file within the <TOMCAT_HOME>/common/lib directory.

  21. When you have defined both of these libraries, as shown in Figure 7.28, select them both and click OK until you are back at the main IDE window.


    Figure 7.28: Select the Struts 1.1 and Servlets libraries.

  22. Now you need to rebuild the entire application. You can do this by selecting the Build > Rebuild Project command.

At this point you have everything that you need to debug your Struts application—the focus of our next section.

Stepping Through Your Source

Now that you have everything configured to debug your application, let's get started. This is the easy part—there are only a few places that you will want to set breakpoints.

The first of these places is in the source of your ActionForm object. For this example, let's look at the ch03.LookupForm. To set a breakpoint in this ActionForm implementation, open the source file by double-clicking on it in the Package Explorer. You will find it in the WEB-INF/src/ch03/ directory. Once you have this file open, scroll down to the validate() method and double-click on the left side of the editor pane on the line containing the following snippet:

 ActionErrors errors = new ActionErrors(); 

This sets a breakpoint on this line, which tells IntelliJ that it should stop on this line whenever it encounters this piece of code while debugging. If you follow these steps correctly, your editor pane should have a breakpoint marker similar to the one shown in Figure 7.29.

click to expand
Figure 7.29: A source breakpoint.

The next place that you want to test is your Action implementation, so find the ch03.LookupAction and open the source file by double-clicking on it in the Package Explorer. Locate it in the WEB-INF/src/ch03/ directory, and once you have this file open, scroll down to the execute() method. Doubleclick on the left side of the editor pane on the line containing this code snippet:

 String target = new String("success"); 

It is now time to test your breakpoints. To begin this process, click the Debug button on the main toolbar to display the Debug window. Select the WebApp tab and complete the following steps:

  1. Select the Plus symbol.

  2. Enter wroxapp in the Name text box.

  3. Click the Configure button. You will see the Web dialog box.

  4. Select the Enable Web Application Support checkbox.

  5. Click the Add button and enter wroxapp for the name. Browse to the root directory of your wroxapp Web application. Once you enter these values, your dialog box should look the one shown in Figure 7.30.

    click to expand
    Figure 7.30: The WebApp dialog box.

  6. Click OK until you return to the Debug dialog box.

  7. Enter the location of your Tomcat installation in the Catalina Home text box. This should automatically populate the Path To Catalina Configs text box. When you have modified all these options, your dialog box should like the one shown in Figure 7.31.

    click to expand
    Figure 7.31: The Debug dialog box.

    start sidebar

    If the Debug button in this dialog box is not active, try switching between tabs and check the availability of the Debug button once more. This seems to be a simple bug, and once the button is active, everything seems to work fine.

    end sidebar

  8. Make sure that Tomcat is running (as we described in the previous section) and click the Debug button. If everything went according to plan, you will see this message:

     Connected to the target VM at 'localhost:5050' using socket transport. 

  9. You should also see a debug window. This window allows you to step through the actual application. It features a toolbar with 13 buttons that you can use to perform various debug operations (only the appropriate buttons will be enabled). Figure 7.32 shows the debug window for this application.

    click to expand
    Figure 7.32: The IntelliJ debug window.

Once this output is printed and you can see the active debug window, you can begin the debugging process. To get this process under way, open a browser to the following URL:

  • http://localhost:8080/wroxapp/index.jsp

Now enter the value SUNW into the text box and click the Submit button. This activates the IntelliJ debugger and runs the wroxapp until it reaches its first breakpoint in the LookupForm. To step through your source, select one of the buttons from the Debug toolbar—at this point, I recommend using the Step Over button.

As you are stepping through each line of code, keep in mind that you can add objects to the Watch window by simply right-clicking on the object that you want to inspect and selecting Add To Watches from the pop-up menu. After doing this, you can select the Watches tab in the debug window and watch the value of the selected object as you step through your application.

After stepping through the LookupForm, the debugger moves on to the next breakpoint (which in this case is in the LookupAction.execute() method). This breakpoint allows you to step through your execute() method and monitor the processing of both the data passed to your Action and the logic encased in this Action.

As I am sure you are aware, there are many other places that can be debugged in a Struts application. The goal of this section was not to identify every debuggable entry point, but rather to give you a look at some of the more common places you can add breakpoints.

Stepping Through the Struts Source

This section goes one step further in our debugging efforts: we actually step through the Struts source. This exercise should give you a good handle on how Struts processes its requests.

To get this process started, complete the following steps:

  1. If you have not done so already, download the Struts 1.1 source (in a zip format) from http://www.apache.org/dist/jakarta/struts/source/.

  2. Extract the Struts source zip file into another convenient location. This example uses C:\StrutsSrc.

  3. Right-click on wroxapp.ipr and select Project Properties.

  4. Click the Libraries tab and select the Struts 1.1 library (Figure 7.33).

    click to expand
    Figure 7.33: The Struts 1.1 library.

  5. Click the Edit button and select the Sourcepath tab.

  6. Click the Add button and browse to the /Jakarta-struts-1.1-src/src/share directory in the extracted source location. The fully qualified path in this example is C:\StrutsSrc\jakarta-struts-1.1-src\src\share. Once you have found this directory, select it and click OK. Your screen should look similar to Figure 7.34.

    click to expand
    Figure 7.34: The Struts 1.1 source path.

  7. Now click OK until you are back at the main IDE window.

You are now ready to step through the Struts source. Again, there are several places that you can set a breakpoint, but one of the best places to begin your debugging of a Struts application is in the ActionServlet—this is where everything begins in a Struts application. And the best place to begin within the ActionServlet is the process() method. If you remember from our previous discussions, the process() method is the first method called by the ActionServlet's doPost() and doGet() methods and is the entry point to all Struts requests.

To set a breakpoint in the process() method, complete these steps:

  1. From the Project window, select the Sourcepath pull-down list to display all of the source paths associated with this project. Figure 7.35 shows the current set of project source paths.

    click to expand
    Figure 7.35: The project source paths.

  2. Expand the C:\StrutsSrc\jakarta-struts-1.1-src\src\share node until you find the org.apache.struts.action.ActionServlet implementation.

  3. Open this file and scroll down until you find the process() method.

  4. Now find the following line in the process() method, and set a breakpoint on this line:

     RequestUtils.selectModule(request, getServletContext()); 

That's all there is to it. Now restart Tomcat, if necessary, and start the IntelliJ debugger. Once the IntelliJ debugger has started, open your browser to the following wroxapp/index.jsp:

  • http://localhost:8080/wroxapp/index.jsp

Enter SUNW and click Submit. When Tomcat receives the request, it sends the request to the ActionServlet's doPost() method, which in turn calls the process() method containing our breakpoint. Now you can simply step through the application as if it was any other application.

start sidebar

When you are debugging the process() method, you must click the Step Into button if you want to follow the entire processing of the request.

end sidebar



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net