站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题NetBeans IDE 5.0 Quick Start Guide for Web Applications II

NetBeans IDE 5.0 Quick Start Guide for Web Applications II

摘要: This document takes you through more advanced NetBeans IDE features for web application development. The tutorial builds on the HelloWeb Web application you created using NetBeans IDE 5.0 Quick Guide for Web Application. This document is designed to get you going as quickly as possible. For more information on working with NetBeans IDE, see the Support and Docs page on the NetBeans website.

Software Needed for the Tutorial

Before you begin, you need to install the following software on your computer:

Tutorial Exercises


Exercise 1: Exercise editing features

In this exercise, you are going to exercise various editing features of NetBeans IDE 5.0.  This exercise assumes you have opened  HelloWeb project you have created by following NetBeans IDE 5.0 Quick Guide for Web Application.


Refactoring

1. Open NameHandler.java
2. Select name string and right-click  it.  Select Refactor and then Rename.



3. In the Rename Field name dialog box, type username in the New Name field. Click Next.



4.  Refactoring confirmation pane gets displayed in the bottom of the IDE. Click Do Refactoring.


5.  Right-click HelloWeb project node and select Run Project.

Error stripe

1. Open NameHandler.java.
2. Create some compile errors  in various places of the code  and see Error stripes are displayed in the right side of the editor.  In the example below,  a random string, x, is entered in various places to generate compile errors for the sake of this exercise.



3. Click CTRL-Z (Undo) several times until above compile errors are gone.

Code templates

1. Select Tools from the menu bar.
2. Select Options. The Options dialog box appears.
3. Select Editor
4. Select Code_Templates tab window.


5. Click New.
6. In the New Code Template Dialog box, type stringx for the Abbreviation field. Click OK.

7. In the Expanded Text: textarea, type in String x:  //This is my test code abbreviation.  Click OK.


8. In the NameHandler.java,  type stringx and Space bar. You should see the expanded code.



9. In the NameHandler.java code, type trycatch and see it gets expanded as well. Undo it by pressing CTRL+Z.

CamelHump code completion

1. In the NameHandller.java code, type ISR and press CTRL-SPACE.  You should see the two Java APIs that starts with the I, S, and R as starting capital letters.


2. Undo the changes by pressing CTRL+Z.

JSP code completion

1. Open response.jsp.
2. In any place of the file, type <jsp.  You will see code completion screens being popped up.  You can also induce code completion by pressing CTRL-SPACE.


3. Undo the changes by pressing CTRL-Z.

Summary

In this exercise, you exercises a few code editing features of NetBeans IDE 5.0.

Exercise 2: Exercise code navigation

In this exercise, you will exercise the following code navigation features of NetBeans IDE 5.0.

Go to Class (Alt+Shift+O)

1. Select Navigate from the menu bar and then select Go to Class (Alt+Shift+O).  The Go to Class dialog box appears.
2. Type NameH and see all the classes whose name starts with NameH.
3. Click Open. Observe NameHandler.java code displayed in the editor window.



Select document in

1.  Right-click in any point of NameHandler.java code and choose  Select in and Files


2.  Observe that the the Files view gets displayed.


3. Right-click in any point of NameHandler.java code and choose  Select in and Favorites to see the file under Favorites tab window.
4. Right-click in any point of NameHandler.java code and choose  Select in and Projects.

Hyperlinking

1. In the NameHandler.java code, move the cursor to String while holding down the CTRL key.  The String string will turns into a hyperlink.


2. Click the String string.  You will see the source code of the String class.

3. Try the same for Serializable.  You should see the source code of the java.io.Serializable interface.



Jump list (Alt+K, Alt+L)


1. While NameHandler.java code is in the editor, Press Alt+K.  You will see previous screen - source code of the String class.
2. Press Alt+K again.  You will see the source code of  NameHandler.java.
3. Press Alt+L and Alt+K to go and back and forth.

Bookmarking


1. While NameHandler.java code is in the editor,  move your cursor in the gray color vertical strip in the editor window and right-click it.
2. Select Bookmark and then select Toggle Bookmark (Ctrl+F2).




3. Move your cursor in another location of the gray colored vertical bar and make another bookmark.
4. Press F2 and Shift+F2 to go to these bookmarked locations.

Select a tab


1. Click vertical downward arrow and see the list of all the files.
2. Select a file.



Exercise 3: Debugging, JUnit testing, and HTTP monitoring

In this exercise, you will exercise the following features of NetBeans IDE 5.0.

Source level debugging of the JSP page

1. Open response.jsp in the editor window.
2. To make a breakpoint on the <jsp:setProperty name="mybean"  property="*" /> line, move your cursor to the location of the gray colored vertical bar of the line  and click.  The dark pink colored bar indicates that the breakpoint is created.


3.  Select Run from menu bar and then select Debug Main Project.  NetBeans will rebuild and deploy the application for debugging.


4. When the browser prompts you to enter your name, enter a name and click OK button.


5. Note that the breakpoint is hit. 
6. Click Local Variables tab window. Expand mybean and notice that the username field has not been set - it is null.


7.  Press F8 to step over.  Note that the username field of the mybean has been changed to the name you entered, Sang Shin, in this example.



8.  Change the name to someone else, like, "James Gosling" and press Enter.



8. Press CTRL+F5 to continue. (You can also select Run from the menu bar and then select Continue.)

9. Observe the new name is displayed in the browser.



JUnit testing

Now you are ready to unit test the methods of NameHandler.java using built-in JUnit.  For the sake of this exercise, you are going to add another method called addNumbers(int x, int y) to the NameHandler.java and test if the method performs as expected.

1.  Add addNumbers(...) method as shown below to the NameHandler.java. after setName(..) method.

public int addNumbers (int x, int y){
        return (x+y);
}

2. Right click NameHandler.javanode. Select Tools->JUnit Tests->Create JUnit Tests.



3. The Create Tests dialog box appears.  Accept all default values and click OK.


4. Observe that NameHandlerTest.java code gets displayed in the editor window.  Also observe that NameHandlerTest.java is created under  Test Packages.

5. Comment the IDE generated fail() methods testGetName(), testSetName(), and testAddNumbers() methods.

6. Change the testAddNumbers() method as following. The code fragments that need to be modified are high-lighted in red color.

  public void testAddNumbers() {
        System.out.println("addNumbers");
       
        int x = 5;
        int y = 7;
        NameHandler instance = new NameHandler();
       
        int expResult = 12;
        int result = instance.addNumbers(x, y);
        assertEquals(expResult, result);
       
        // TODO review the generated test code and remove the default call to fail.
        // fail("The test case is a prototype.");
  }


7. Select Run from the menu bar and then select Test "HelloWeb".


8. Observe that testGetName() failed while testSetName() and testAddNumbers() succeeded. 
9. Double-click testGetName Failed (0.016s).
10. Comment out the assertEquals(expResult, result); method

11. Select Run from the menu bar and then select Test "HelloWeb".
12. Observe all tests succeeded.

HTTP monitoring

1. If HTTP monitoring window does not get displayed, select  Window from menu bar and then select HTTP Monitor.

2. Run the above application.

3. Observe the captured HTTP traffic under HTTP Monitor window. 

Trouble-shooting: If the HTTP Monitor window shows no records you have to turn on the HTTP Monitoring for this server.  Go to the Runtime tab, expand the servers list, right click on the server name and select Properties. Check Enable HTTP Monitor, close the dialog and restart the server (right click on the server name and select restart).


4.  Click other tab windows such as Cookies, Session, Context, Client and Server, and Headers to see more information about a HTTP request or response.

5.  You can do replay or Edit and Replay.

Note: If you are running NetBeans 5.0 or 5.5preview or 5.5dev you will find that when you select
"Edit and Replay" for one of the records in the HTTP Monitor, you can edit the parameter value but when you send the HTTP Request the change is ignored and the old value is sent instead. There is now a bug open for this issue. Hopefully it will be fixed for
the final 5.5 release: http://www.netbeans.org/issues/show_bug.cgi?id=73655




Summary

In this exercise, you exercised source level JSP page debugging first.  You also learned how to do JUnit testing and  how to capture HTTP traffic.


Exercise 4: Project management

In this exercise, you will do various project management such as following:

Copying a project

1. Right click HelloWeb project and select Copy Project.


2.  In the Copy Project dialog box, type in a new project name, HelloWebClone, in this example. Click Copy button.


3. Note that HelloWebClone project gets created.
4. Right click HelloWebClone project and Run Project.

Rename a project

1. Right click HelloWebClone project and select Rename Project.
2. In the Rename Project dialog box, give it a new name, HelloWebClone2, and click Rename.
3. Observe that the project name has been changed.

Deleting a project

1. Right click HelloWebClone2 project and select Delete Project.
2. In the Delete Project dialog box, select Also Delete Source Under ... if you want to delete source code as well.



Run the ant Script at the command line

1. Go to the directory you have your HelloWeb project's build.xml file.  If you take the default, it should be the <home-directory>/HelloWeb. For example, under Windows, it should be C:\Documents and Settings\sang\HelloWeb.  Under Solaris/Linux, it should be $HOME/HelloWeb.
2. Type ant and observes that it gets built correctly.


Summary

In this exercise, you exercised source level JSP page debugging.  You also learned how to capture HTTP traffic.


↑返回目录
前一篇: NetBeans IDE 4.1/5.0 Profiler Tutorial
后一篇: NetBeans IDE 5.0