Obsolete

View the updated version at netbeans.html

Create a Project

Create a project in Netbeans.

  1. File, New Project
  2. Category: Web
  3. Project Type: Web Application

Choose a location for your project. If you are creating this project in the JCCL on the U: drive, do not create the project in the webapps folder. I suggest that you create a folder named local in the cgs4854 directory, and place your projects there.

web.xml Location

The web.xml file is located in the Web Pages/WEB-INF folder.

JSP Location

JSPs should be placed in sub-folders of the Web Pages folder. If you want to hide the pages so that they are not directly visible from the web, then place them in sub-folders of the Web Pages/WEB-INF folder.

Servlet Location

Servlets should be placed in sub-folders of Source Packages. The package name of a servlet must match the folder location of the .class file. Do not use the default package for a servlet. Always create a sub-folder for a servlet and place the servlet in a package that matches the directory structure.

Create a Servlet

Create a folder in the Source Packages folder. Create a Java class in the new folder. Copy the following servlet into the file. Change the name of the package to correspond to the name of the folder.

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.getWriter().print("Hello NetBeans");
    }

}

Servlets must be Identified as Servlets

If you right-click the servlet in the file tree, and select Run, you will get this error:

The solution is to add a servlet mapping for this servlet to the web.xml file.

    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>test.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/test/TestServlet</url-pattern>
    </servlet-mapping>

The directory that is used in the mapping does not actually exist. There is a directory named WEB-INF/test and another named classes/test, but there is no directory named test in the root of the web application. You may map a servlet to a real directory or to a fictitious directory.

Run the Servlet

Right-click on the name of the servlet in the file tree and select Run File. You will be prompted for the URL to use to call the servlet. You can add a query string at this time if you want to send some data to the servlet.

Update index.jsp

Instead of running the file directly, it is possible to add a hypertext link to the index.jsp page of the web application and then run the application. When the web application is run, it will call the index.jsp file first.

Creating a .war file

When the project is cleaned and built (select Clean and Build from the Build menu), a .war file is created in the dist folder. By default, this .war does not include the .java source files. When you upload files to ocelot, I would like the .java source files included in the .war file.

If you upload a .war file, unpack it using unzip on ocelot. Be sure to update the web.xml file so that it is the secure web.xml file.