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.
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.
- Create the WEB-INF and classes directory (as above) in your webapp, if they don’t already exist.
- 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.
- 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.
- Open the file <your netbeans project>/nbproject/project.properties.
- It should look something like this:
This file has many properties for configuring a Netbeans project. We are only interested in one property.
- 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.
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.