
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:
- Web Project with Existing Sources - Use
this project template to create a standard web project. A standard web project
uses the IDE to compile, run
and debug a web application.
- Web Project with Existing Ant Script - Use
this project template to create a free-form web project. A free-form web project uses your own Ant script to compile and run
a web application, and to start the NetBeans debugger.
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
- Choose File > New Project (Ctrl-Shift-N).
- Choose Web > Web Project with Existing Sources. Click Next.
- In Location, click Browse to select your web application's web module root.
- 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".
- 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.
- 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.
- 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.
- Click Compiling Sources in the left panel of the Project Properties dialog box.
- Click Add Project.
- 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.
- 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
- Choose File > New Project (Ctrl-Shift-N).
- Select Web > Web Project with Existing Ant Script. Click
Next.
- 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.
- 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.
- 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
- The IDE fills the location of your Web Pages folder and your Source Packages folder automatically.
- 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.
- Optionally, add other source package folders. For this example this is not necessary.
- 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.
- 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.
- 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:
- 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 /).
- 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"/>
- 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"/>
- 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" />
- 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
- 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.
- 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.
- 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.
- Click http://localhost:8084/MyProject.
The IDE's default browser opens and Tomcat's Sample "Hello World" Application
is displayed.
Debugging the application
- In the Projects window, go to the build.xml file for your
project.
- 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.
|
Related Documents
Quickly create, build, and execute a simple a simple
J2SE application.
Quickly create, build, and deploy a simple HelloWeb
web application.
A short guide to getting your existing Java applications
into NetBeans IDE 4.0.
Transition guide for NetBeans IDE 3.6 users.
A complete list of docs for NetBeans IDE 4.0.
|