FeaturesPluginsDocs & SupportCommunityPartners

Importing Existing Web Applications into NetBeans IDE 4.0

There are two project templates you can use to create a NetBeans 4.0 project from a web application:

Getting Your Code Working

Your source structure may correspond to more than a single IDE project. Therefore, you might find it beneficial to break your source structure down into multiple IDE projects with inter-project dependencies. If, for example, you have reusable components, applets, or tag libraries as part of your source structure, you might need to create individual Java Class Library projects and create dependencies between them and your Web projects.

In this document, we'll first use a hypothetical web application to outline how to import source code that doesn't use an Ant build script. In this scenario, the NetBeans IDE will create an Ant build script for you. Then we will import Tomcat's sample "Hello World" application, which is bundled with your NetBeans installation. This sample application has its own Ant build script. During this part of the document you will learn how to import your source code if you want to use your own Ant script to work with your web application.


Using the IDE to Build, Run, and Debug


Creating a project

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Choose Web > Web Project with Existing Sources. Click Next.
  3. In Location, click Browse to select your web application's web module root.
  4. In Project Name, type the name of your project. The name is also used as the name of the the project folder and the context path. Note that 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".
  5. Somewhere on your system, create a folder called NetBeans_projects to house your NetBeans project folders. In Project Location, click Browse to select the NetBeans_projects folder.
  6. Click Finish. The project opens in the IDE. You can view its logical structure in the Projects window and its file structure in the Files window. For example, your application might look as follows:

                

Creating dependencies between projects

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.

  1. Right-click the project to which you want to add a required project and choose Properties. The General pane of the Project Properties dialog box displays all of the project's required projects.
  2. Click Compiling Sources in the left panel of the Project Properties dialog box.
  3. Click Add Project.
  4. Select the project directory of the project whose JAR files you want to add to the classpath. IDE project directories are marked with a (icon) badge. When you select the project directory, the project name and project JAR files are displayed.
  5. Click Add Project JAR Files.

Running the application

  • Choose Run > Run Main Project to run your web application.

Using Your Own Ant Script to Build, Run, and Debug


Creating a project

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Select Web > Web Project with Existing Ant Script. Click Next.
  3. In Location, click Browse and select the web module root.
    For this example, the web module root is your NetBeans installation folder's nb4.0\jakarta-tomcat-5.0.28\webapps\tomcat-docs\appdev\sample folder. Browse to it and select it. The IDE fills in the rest of the fields automatically.
  4. Somewhere on your system, create a folder called NetBeans_projects to house your NetBeans project folders. In Project Location, click Browse to select the NetBeans_projects folder.
  5. Leave the Set as Main Project box selected. Click Next.

Linking Ant targets with IDE commands

  • Specify which Ant targets the IDE should run for project commands.
    • For Build Project select compile from the dropdown.
    • For Deploy Project select install from the dropdown.
    • For Redeploy select reload from the dropdown.
    Click Next.

Configuring web source directories and package folders

  1. The IDE fills the location of your Web Pages folder and your Source Packages folder automatically.
  2. In the Context Path text box, type /MyProject. Note that your context path is preceded by a backslash and that it should not contain spaces. Click Next.
  3. Optionally, add other source package folders. For this example this is not necessary.
  4. In Source Level, choose the JDK that your Ant script compiles and runs against. For this example, the JDK level is 1.5. Click Next.
  5. Optionally, add JAR files to the project classpath. This classpath is not used for compilation or execution - your Ant script handles the classpath for these tasks. These settings tell NetBeans which classes to include in code completion and refactoring.
  6. Click Finish. The project opens in the IDE. You can view its logical structure in the Projects window and its file structure in the Files window.

                

Customizing your Ant build script for use with NetBeans

Expand the MyProject project node in the Projects window, open the build.xml file in the Source Editor, and do the following:

  1. Edit the app.name property in the File and Directory Names section to configure the context path:

         <property name="app.name" value="MyProject"/>

    Change projectname to the value for the context path (except without the leading /).

  2. Edit the catalina.home property in the File and Directory Names section to define the entry to point to the Tomcat Server. For example, if your version of the Tomcat Server is 5.0.28 and the NetBeans IDE is installed in the default folder, use the following property statement:
         <property name="catalina.home"
         value="C:\Program Files\netbeans-4.0\nb4.0\jakarta-tomcat-5.0.28"/>
  3. If you want to change the Tomcat Server's default port number (8080), edit the manager.url property in the File and Directory Names section:

         <property name="manager.url"
         value="http://localhost:8084/manager"/>
  4. Edit the Custom Ant Task Definitions section to set your environment's CLASSPATH variable used by the IDE whenever it runs the Ant build script. For example, your entry for the "deploy","list", "reload", and "undeploy" tasks might be the following:
        <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" />
  5. Optionally, add your own targets to the Ant build script and map them to project commands in the IDE. For details, see Advanced Free-form Project Configuration.

Running the application

  1. In the Runtime window, expand the Server Registry node and check that your Tomcat Server is running. If it is not running, right-click your Tomcat Server's instance node and choose Start/Stop Server from the contextual menu.
  2. Right-click the project in the Projects window and choose Run Project from the contextual menu. A progress monitor window appears as the application is prepared for deployment.
  3. The first time that you run the application per session, Tomcat 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 tomcat-users.xml file in Tomcat's base directory. To identify the location of this directory, right-click the Tomcat Server instance node in the Runtime window and select Properties. In the Properties dialog box, the Base Directory property points to Tomcat's base directory.
  4. Click http://localhost:8084/MyProject. The IDE's default browser opens and Tomcat's Sample "Hello World" Application is displayed.

Debugging the application

  1. In the Projects window, go to the build.xml file for your project.
  2. Double-click build.xml. It opens in the Source Editor. This is where you modify or add a target to start the NetBeans debugger. For details, see Writing a Target to Debug Your Web Application.

Bookmark this page

del.icio.us furl simpy slashdot technorati digg
Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Open ESB - The Open Enterprise Service Bus Powered by