There are two project templates you can use to create a NetBeans IDE 5.5 project from an existing web application:
In this document, you first use a hypothetical web application to learn how to import source code that does not have its own Ant build script. In this first scenario, the NetBeans IDE creates an Ant build script for you. In the second scenario, you import the Tomcat Web Server's sample "Hello World" application, which is bundled with the NetBeans IDE 5.5 installation. This sample application has its own Ant build script. During this part of the document, you learn how to import your source code if you want to use your own Ant build script to work with your web application.
Before you start writing code, you have to make sure you have all of the necessary software and that your project is set up correctly.
Before you begin, you need to install the following software on your computer:
Optionally, you can download and use one of the following application servers:
The bundled Tomcat Web Server is registered with the IDE automatically. However, before you can deploy to the SJS Application Server, JBoss, or WebLogic, you have to register a local instance with the IDE. If you installed the NetBeans IDE 5.5/SJS Application Server bundle, the local SJS Application Server is registered automatically.
When you create a standard web application project, the IDE creates an Ant build script and properties files that control how your project is built and deployed. The IDE updates the Ant script as you set options for the project.
Note: The context path cannot contain spaces. If your project name contains spaces, they are replaced by underscores in the context path. For example, a project named "Servlet Examples" would have a context path consisting of "/servlet_examples".
Note: Your project folder cannot already contain a folder called nbproject, because this is the name of the folder that the IDE creates to house the project-level files.
Note: The source root cannot be owned by another loaded project. In addition, the source root cannot be owned by another compilation unit of the same project. For examples, a source package folder cannot also be a test package folder for the same project.
When you create separate projects for each of your source roots, you have to set up the classpath dependencies between the projects. Typically you set up one main project and several required projects. A required project is a project that has been added to another project's classpath.
Note: The NetBeans IDE 4.1 Applet Tutorial illustrates the usefulness of creating dependencies between projects.
When you create a free-form web application project, the IDE uses your Ant build script to build and deploy your project. In the IDE, you have to set up the project to mirror the settings that are contained in the project's Ant script. You also have to write your own targets to debug the project in the IDE.
enterprise3\apache-tomcat-5.5.17\webapps\tomcat-docs\appdev\sample
Note: Your project folder cannot already contain a folder called nbproject, because this is the name of the folder that the IDE creates to house the project-level files.
Note: The source root cannot be owned by another loaded project. In addition, the source root cannot be owned by another compilation unit of the same project. For examples, a source package folder cannot also be a test package folder for the same project.
enterprise3/apache-tomcat-5.5.17/common/libNote: The classpath is not used for compilation or execution, because your Ant script handles the classpath for these tasks. These settings tell the IDE which classes to include in code completion and refactoring.
Expand the MyProject project node in the Projects window, open the build.xml file in the Source Editor, and do the following:
<property name="app.name" value="MyProject"/>
<property name="catalina.home" value="C:\Program Files\netbeans-5.5\enterprise3\apache-tomcat-5.5.17"/>
<property name="manager.url" value="http://localhost:8084/manager"/>
<path id="catalina.ant.cp"> <pathelement path="${catalina.home}/server/lib/catalina-ant.jar"/> </path>
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="catalina.ant.cp"/> <taskdef name="list" classname="org.apache.catalina.ant.ListTask" classpathref="catalina.ant.cp"/> <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="catalina.ant.cp"/> <taskdef name="undeploy" classname="org.apache.catalina.ant.ReloadTask" classpathref="catalina.ant.cp" />
<nbbrowse url="http://localhost:8084/MyProject"/>
The default install and reload targets do not display the application in a browser. When you add the above line, the IDE's default browser is opened and the application is displayed as the final step of these targets.
Note: The first time that you run the application per session, the Tomcat Web Server asks you for a username and password. The only acceptable username and password is that of a user with a "manager" role. This is defined in the conf/tomcat-users.xml file in the Tomcat Web Server's base directory. To identify the location of this directory, right-click the Tomcat Web Server instance node in the Runtime window and select Properties. In the Properties dialog box, the Base Directory property points to the Tomcat Web Server's base directory.
After the application has successfully deployed, you can redeploy it by right-clicking the project in the Projects window and choosing Redeploy Project from the contextual menu. When you linked Ant targets to IDE commands, you linked the reload target to the Deploy Project command. This is the target that is called when you run the project.
Let's add a debug target to the project. When you run the debug target, it will run the project in the debugger and allow you to step line by line through your code. The IDE can generate a complete debug target for you. After you have provided some settings that are specific to your project, you'll be ready to go. However, maybe you have your own debug target already. In this case, take the following steps:
<action name="debug"> <script>path to my build script</script> <target>name of my debug target</target> </action>
<ide-action name="debug"/>
Note:You are prompted to let the IDE generate an IDE-specific debug target in nbproject/ide-targets.xml.
A complete debug target is generated in the new ide-targets.xml file and all the properties are defined in the new debug.properties file. The debug.properties file, ide-targets file, and projects.xml file all open in the Source Editor. The debug.properties file has the following content:
jpda.session.name=MyProject jpda.host=localhost # Sun Java System Application Server using shared memory (on Windows) # jpda.address=localhost4848 # jpda.transport=dt_shmem # Sun Java System Application Server using a socket # jpda.address=9009 # jpda.transport=dt_socket # Tomcat using shared memory (on Windows) jpda.address=tomcat_shared_memory_id jpda.transport=dt_shmem # Tomcat using a socket # jpda.address=11555 # jpda.transport=dt_socket src.folders=src web.docbase.dir=web # you can change this property to a list of your source folders debug.sourcepath=${src.folders}:${web.docbase.dir} # Client URL for Tomcat client.url=http://localhost:8084/MyProject # Client URL for Sun Java System Application Server # client.url=http://localhost:8080
The properties above specify that the application will be deployed to the Tomcat Web Server via shared memory. If you want to use the Sun Java System Application Server instead, or if you want to use a socket connection, place a comment symbol (#) in front of the Tomcat properties and remove those for the applicable connection using the Sun Java System Application Server. Similarly, notice that the application will be deployed to http://localhost:8084/MyProject by default, but that the port number for the Sun Java System Application Server is included. Finally, the debug.sourcepath property specifies the folders that contain the files that you want to debug. In this example, we will make the Java files and the web files available for debugging.
For more information on these properties, see Writing a Target to Debug your Web Application.
<target name="-load-props"> <property file="nbproject/debug.properties"/> </target> <target name="-check-props"> <fail unless="jpda.session.name"/> <fail unless="jpda.host"/> <fail unless="jpda.address"/> <fail unless="jpda.transport"/> <fail unless="web.docbase.dir"/> <fail unless="debug.sourcepath"/> <fail unless="client.url"/> </target> <target depends="-load-props, -check-props" name="-init"/> <target depends="-init" if="netbeans.home" name="debug-nb"> <nbjpdaconnect address="${jpda.address}" host="${jpda.host}" name="${jpda.session.name}" transport="${jpda.transport}"> <sourcepath> <path path="${debug.sourcepath}"/> </sourcepath> </nbjpdaconnect> <antcall target="debug-display-browser"/> </target> <target name="debug-display-browser"> <nbbrowse url="${client.url}"/> </target>
Any customization you need to do should be done in the debug.properties file, not in the debug-nb target or the targets that support it.
Before you can use the debug target, your web application must be deployed.
Now click Start Server (Debug) in the Server Status dialog box.
Attaching to tomcat_shared_memory_id User program running Breakpoint reached at line 68 in hello.jsp by thread http-8084-Processor23. Thread http-8084-Processor23 stopped at hello.jsp:25.
User program running Thread http-8084-Processor23 stopped at hello.jsp:26.
See the Debugging section in the NetBeans User FAQ.
For more information about developing web applications in NetBeans IDE 5.5, see the following resources:
To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the nbj2ee@netbeans.org mailing list. For more information about upcoming Java EE development features in NetBeans IDE, see http://j2ee.netbeans.org/.