9. Developing and Deploying Web Application in NetBeans

9.3. Writing a Hello-World Servlet/JSP Web Application

Create a New Servlet/JSP Project

  1. From "File" menu ⇒ choose "New Project...".
  2. "Choose Project" ⇒ Under "Categories", choose "Java Web" ⇒ Under "Projects", choose "Web Application" ⇒ "Next".
  3. "Name and Location" ⇒ In "Project Name", enter "HelloServletJSP" ⇒ In "Project Location", select a suitable directory to save your works ⇒ Check "Set as Main Project" ⇒ Next.
  4. "Server and settings" ⇒ Choose your server, or "add" a new server ⇒ Next.
  5. "Frameworks" ⇒ Select none for pure servlet/JSP application ⇒ Finish.

Writing a Hello-World JSP

A JSP page called "index.jsp" is automatically created, which says "Hello world!". To execute this JSP, right-click on the project ⇒ "Run". The URL is http://localhost:8080/HelloServletJSP/index.jsp .

Writing a Hello-World Servlet

  1. Right-click on the project "HelloServletJSP" ⇒ New ⇒ Servlet.
  2. "Name and Location" ⇒ In "Class Name", enter "HelloServlet" ⇒ In "Package", enter "hello " ⇒ Next.
  3. "Configure Servlet Deployment" ⇒ In "Servlet Name", enter "HelloServletExample" ⇒ In "URL Pattern", enter "sayhello " ⇒ Finish.
  4. Enter the following codes for "HelloServlet.java":
    package hello;
     
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    public class HelloServlet extends HttpServlet {
     
       @Override
       public void doGet(HttpServletRequest request, HttpServletResponse response)
               throws IOException, ServletException {
          // Set the response message's MIME type (in Content-Type response header)
          response.setContentType("text/html;charset=UTF-8");
          // Get an output Writer to write the response message over the network
          PrintWriter out = response.getWriter();
          // Write the response message (in an HTML page) to display "Hello, world!"
          try {
            out.println("<!DOCTYPE html>");
             out.println("<html>");
             out.println("<head><title>Hello Servlet</title></head>");
             out.println("<body><h1>Hello, World (from Java Servlet)!</h1></body>");
             out.println("</html>");
          } finally {
             out.close();  // Always close the output writer
          }
       }
    }          
  5. To execute the servlet: Right-click on the project ⇒ run ⇒ Change the URL to http://localhost:8080/HelloServletJSP/sayhello .

Generating a WAR-file for a Web Application

A WAR (Web Archive) file is basically a zip file for distributing web application in single file. You can use WinZip or WinRAR to inspect or unzip the war file.

To distribute the project as a war-file, right-click project ⇒ "Clean and Build". The war file is created in the "dist" directory. You can deploy the web application by dropping the war-file into Tomcat's "webapps" directory. Tomcat will automatically unzip the war-file and deploy the application upon startup.

Debugging Web Application

The most important reason for using IDE is to use the graphic debugger for debugging the program. You can set a breakpoint in your server-side Java codes, and "Debug" a web application, similar to a standalone application.