CS 220 Spring, 2008 Lab Exercise 6: Servlets

Note: The webpage user interface created as a result of this exercise is not meant to be useful, just as an exercise in using servlets and HTML. We will develop a more realistic and useful user interface in Exercise 7 on JSPs.

  1. Obtain files from instructor web page: NBCasClothesServlet.java and ResponseServlet.java.
  2. Create a new project in NetBeans.
  3. In the New Project dialog, in the Categories pane, select Web, and in the Projects pane, select Web Application. Click the Next button.
  4. Enter a Project Name such as Lab06; for Project Location, navigate to and select the folder that you want to hold the new project.
  5. Click the Finish button.
  6. Add a servlet to your project.
  7. Right-click on the Source Packages node and, from the pop-up menu, select New>Servlet.
  8. In the New Servlet dialog: enter a Class Name such as WelcomeServlet; the Location should be Source Packages; enter “web” for the Package name (without the quotes). Click the Next button.
  9. In the next dialog, click the Finish button.
  10. Edit the servlet code.
  11. Display the code for the new servlet class in the edit window. Note that the servlet class includes code for the servlet methods, but almost all the method bodies are empty.
  12. In the processRequest method, uncomment the 7 lines that begin with:

out.println("<html>");

  1. After the line out.println("<body>");, add the following two lines: out.println("<h1>Welcome to Java Programming!</h1>");out.println("<h2>Welcome to Servlet Programming!</h2>");
  1. Compile the servlet file. Have the focus of the file and press F9 or right click on the file name and select compile.
  2. Run the servlet and view in browser. Shift F6 or right click on the file name and select Run->Run File->Run”file name”
  3. This will bring up your web browser, and display the web page generated by the servlet (which runs on the server), shown below.

  1. Make a window capture of the web browser window and paste the window captures into an MS Word document with your name and the Lab number at the top.
  2. Create two additional servlets and use the files provided by the instructor.
  3. In NetBeans, add another servlet, named NBCasClothesServlet, to your project.
  4. Open the instructor-provided file named NBCasClothesServlet.java in a text editor and copy the appropriate sections into your NetBeans File.
  5. Save your files.
  6. Add another servlet, named ResponseServlet, to your NetBeans project.
  7. Do the same thing as in step 5c.
  8. Compile the two files. Fix any compile errors.
  9. Run the servlet NBCasClothesServlet.javaand view the results in the browser.
  10. Enter a name in the Name textbox and select a clothing item from the dropdown list. Then click the Submit button. This will invoke the ResponseServlet.
  11. The ResponseServlet will generate a new page that displays the user's two entries.
  12. The page generated by NBCasClothesServlet servlet is shown below.

  1. The response page generated by the ResponseServlet servlet is shown below.

  1. Modify the NBCasClothesServlet by adding a multi-select scrollable list of colors:
  2. Add another ArrayList to the processRequest method that holds the following list of color choices: Red, Blue, Green, Purple, Yellow, Black, White.
  3. Below the existing dropdown list for clothing category, add the label “Select colors:” followed by a multi-select scrollable list immediately below.
  4. Add the color choice values from the ArrayList to the multi-select scrollable list for display to the user.
  5. The list must enable the user to select multiple items in the list.
  6. Make the visible number of rows 3, so that the list scrolls.
  7. Modify the NBCasClothesServlet further by adding a text area for user comments:
  8. Add a prompt consisting of the text “Comments:” followed by a text area element.
  9. The text area should be 3 rows by 30 columns in size.
  10. Modify the ResponseServlet as follows:
  11. Add code to get the additional parameter(s) from the NBCasClothesServlet (in addition to the user name and item name).
  12. Add a HTML 3rd level header element to display the color choice(s) selected by the user in the NBCasClothesServlet page.
  13. Add a labeled paragraph to display the user-entered comments.
  14. Modify the ResponseServlet further as follows:
  15. Add a horizontal rule line to the page to be displayed, using the tag <hr>.
  16. We will now add code to display information that is not shown to users in real-world applications. We are displaying this for our own benefit to become more familiar with servlet capabilities and the content of HTTP request queries and headers. You would not display this information for end users.
  17. The class HttpServletRequest includes several methods to provide information about a submitted request. One of these is the method getQueryString() which returns the query sent to the server. Add code to the ResponseServlet that uses this method. Print out the query string returned by the getQueryString() method using paragraph tags (<p> . . . </p>).
  18. The class HttpServletRequest also includes methods to get header information. Header information is part of every request and response that is sent between a web client and web server. Use the method getHeaderNames() to get an Enumeration of all the header names, and then use the method getHeader(String name) to get the value of each header in the Enumeration and print it out. Print out each header and its value using paragraph tags (<p> . . . </p>).
  1. Recompile and re-execute the web application.
  2. Make window captures.
  3. Make a window capture of the web browser window after you have entered a name, selected an item, selected a color, but before you click the Submit button.
  4. Then after you click the Submit button, make another window capture of the page generated by the ResponseServlet.
  5. Paste the window captures into your MS Word document.
  6. EXAMPLE OF WORKING COMPLETED EXERCISE:

TO RECEIVE CREDIT FOR YOUR LAB EXERCISE:

Email your word file with the correct sequence of printscreens to

with the subject heading: “CS220 Lab.6” by 3/10/08