You quite often want to use java classes from within your webapp (In JSP / servlets etc). Webapps have a standard place to put library files (JAR files) and java class files so they can be used from within the web app.

nb1

Jar files go in the lib directory and java class files go in the classes directory.

To make the java classes in a Netbeans project useable from within our webapp we need to make Netbeans output the class files to this “classes” directory.

  1. Create the WEB-INF and classes directory (as above) in your webapp, if they don’t already exist.
  1. Create a new project in Netbeans. I’d recommend using a folder outside of  our webapp as the Netbeans project files aren’t really a part of the webapp.
  1. When you compile a netbeans project it defaults to writing out classes to the build/classes directory of the project. We want it to write the classes out to the classes folder of our webapp instead.
  1. Open the file <your netbeans project>/nbproject/project.properties.
  1. It should look something like this:

nb2

This file has many properties for configuring a Netbeans project. We are only interested in one property.

  1. The “build.classes.dir” property tells Netbeans where to output generated class files.

Change it to the path of your classes folder in your webapp. Eg.

build.classes.dir=c:/My_Tomcat/webapps/my_webapp/WEB-INF/classes

Remember to use forward slashes as most java applications use forward slashes as the path delimiter.

7.  Once you have changed this file and saved it you may have to close and reopen your project for the changes to take effect.

8. Once this is done clean and build your project. After the build there should be class files in the WEB-INF/classes directory of your webapp.  These classes should now be useable from within your webapp.

9. Create a class with a static method that returns a String then try to use it from within a JSP page. If you can use the class from within a JSP page then you have the Netbeans project set up correctly.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s