Servlets must be identified as servlets

The first problem that can occur in NetBeans when running a servlet, is this familiar error:
java.lang.NoSuchMethodError: main
Exception in thread "main"
If you have done any Java programming at all, then you probably recognize what this error means. Java is looking for a method named main and can't find it. The solution is to mark the servlet as a servlet.
  1. Click on the servlet in the file system window
  2. Open the Tools menu from the menu bar
  3. Select Mark as Servlet. If the menu shows Cancel Servlet instead, then NetBeans already knows that your file is a servlet.

Servlets must be in web apps

The next problem is that servlets must be run in a web app. If you try to run a servlet that is not in a web app, this is the error that you get from NetBeans:

Exception occurred in Request Processor
If you inspect the details for the error, you will see this:
Annotation: Exception occurred in Request Processor
java.lang.NullPointerException
at org.netbeans.modules.web.core.jsploader.ServletDataObject.getModule(ServletDataObject.java:682)
at org.netbeans.modules.j2ee.deployment.execution.ServerDebuggerType$1.run(ServerDebuggerType.java:84)
at org.openide.util.Task.run(Task.java:136)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:330)
[catch] at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:686)

Note: If you are working in the AUL, then you could try to mount the servlets directory, but you will be prevented from using it because of the security permissions imposed by the web.xml file.

Solution

The solution is to create a separate web app that you can access from NetBeans. Do the following

You should also create subdirectories of classes for my downey package, and your user-name package.

You can now mount the local directory in NetBeans. Place your .java files in the classes directory (in the correct package).

If all goes well, then NetBeans will mount the web app directory and the classes directory separately (and automatically). The file structure should look like this when you are done.

Note that NetBeans has created some additional directories for the web app: META-INF and lib. Also note that in the WEB-INF directory the icon associated with the classes directory is different from the icon for the lib directory. This is because classes is also mounted as a file system.