# The Java EE 7 Tutorial

# 6.4 A Web Module That Uses Java Servlet Technology: The hello2 Example

The `hello2` application is a web module that uses Java Servlet technology to display a greeting and response. You can use a text editor to view the application files, or you can use NetBeans IDE.

The source code for this application is in the tut-install`/examples/web/servlet/hello2/` directory.

## 6.4.1 Mapping URLs to Web Components

When it receives a request, the web container must determine which web component should handle the request. The web container does so by mapping the URL path contained in the request to a web application and a web component. A URL path contains the context root and, optionally, a URL pattern:

    http://host:port/context-root[/url-pattern]

You set the URL pattern for a servlet by using the `@WebServlet` annotation in the servlet source file. For example, the `GreetingServlet.java` file in the `hello2` application contains the following annotation, specifying the URL pattern as `/greeting`:

    @WebServlet("/greeting")
    public class GreetingServlet extends HttpServlet {
        ...

This annotation indicates that the URL pattern `/greeting` follows the context root. Therefore, when the servlet is deployed locally, it is accessed with the following URL:

    http://localhost:8080/hello2/greeting

To access the servlet by using only the context root, specify `"/"` as the URL pattern.

## 6.4.2 Examining the hello2 Web Module

The `hello2` application behaves almost identically to the `hello1` application, but it is implemented using Java Servlet technology instead of JavaServer Faces technology. You can use a text editor to view the application files, or you can use NetBeans IDE.

### 6.4.2.1 To View the hello2 Web Module Using NetBeans IDE

1. From the File menu, choose Open Project.

2. In the Open Project dialog box, navigate to:

       tut-install/examples/web/servlet

3. Select the `hello2` folder and click Open Project.

4. Expand the Source Packages node, then expand the `javaeetutorial.hello2` node.

5. Double-click the `GreetingServlet.java` file to view it.

   This servlet overrides the `doGet` method, implementing the `GET` method of HTTP. The servlet displays a simple HTML greeting form whose Submit button, like that of `hello1`, specifies a response page for its action. The following excerpt begins with the `@WebServlet` annotation, which specifies the URL pattern relative to the context root:

       @WebServlet("/greeting")
       public class GreetingServlet extends HttpServlet {

           @Override
           public void doGet(HttpServletRequest request,
                   HttpServletResponse response)
                   throws ServletException, IOException {

               response.setContentType("text/html");
               response.setBufferSize(8192);
               try (PrintWriter out = response.getWriter()) {
                   out.println("<html lang=\"en\">"
                           + "<head><title>Servlet Hello</title></head>");

                   // then write the data of the response
                   out.println("<body  bgcolor=\"#ffffff\">"
                       + "<img src=\"duke.waving.gif\" "
                       + "alt=\"Duke waving his hand\">"
                       + "<form method=\"get\">"
                       + "<h2>Hello, my name is Duke. What's yours?</h2>"
                       + "<input title=\"My name is: \"type=\"text\" "
                       + "name=\"username\" size=\"25\">"
                       + "<p></p>"
                       + "<input type=\"submit\" value=\"Submit\">"
                       + "<input type=\"reset\" value=\"Reset\">"
                       + "</form>");

                   String username = request.getParameter("username");
                   if (username != null && username.length()> 0) {
                       RequestDispatcher dispatcher =
                           getServletContext().getRequestDispatcher("/response");

                       if (dispatcher != null) {
                           dispatcher.include(request, response);
                       }
                   }
                   out.println("</body></html>");
               }
           }
           ...

6. Double-click the `ResponseServlet.java` file to view it.

   This servlet also overrides the `doGet` method, displaying only the response. The following excerpt begins with the `@WebServlet` annotation, which specifies the URL pattern relative to the context root:

       @WebServlet("/response")
       public class ResponseServlet extends HttpServlet {

           @Override
           public void doGet(HttpServletRequest request,
                   HttpServletResponse response)
                   throws ServletException, IOException {
               try (PrintWriter out = response.getWriter()) {

                   // then write the data of the response
                   String username = request.getParameter("username");
                   if (username != null && username.length()> 0) {
                       out.println("<h2>Hello, " + username + "!</h2>");
                   }
               }
           }
           ...

## 6.4.3 Running the hello2 Example

You can use either NetBeans IDE or Maven to build, package, deploy, and run the `hello2` example.

### 6.4.3.1 To Run the hello2 Example Using NetBeans IDE

1. Start GlassFish Server as described in To Start GlassFish Server Using NetBeans IDE, if you have not already done so.

2. From the File menu, choose Open Project.

3. In the Open Project dialog box, navigate to:

       tut-install/examples/web/servlet

4. Select the `hello2` folder.

5. Click Open Project.

6. In the Projects tab, right-click the `hello2` project and select Build to package and deploy the project.

7. In a web browser, open the following URL:

       http://localhost:8080/hello2/greeting

   The URL specifies the context root, followed by the URL pattern.

   The application looks much like the `hello1` application. The major difference is that after you click Submit the response appears below the greeting, not on a separate page.

### 6.4.3.2 To Run the hello2 Example Using Maven

1. Start GlassFish Server as described in To Start GlassFish Server Using the Command Line, if you have not already done so.

2. In a terminal window, go to:

       tut-install/examples/web/servlet/hello2/

3. Enter the following command:

       mvn install

   This target builds the WAR file, copies it to the tut-install`/examples/web/hello2/target/` directory, and deploys it.

4. In a web browser, open the following URL:

       http://localhost:8080/hello2/greeting

   The URL specifies the context root, followed by the URL pattern.

   The application looks much like the `hello1` application. The major difference is that after you click Submit the response appears below the greeting, not on a separate page.