Skip to main content.

Lab 4: Servlets

Goals

At the completion of this lab, you should be able to

Set up

Accept the assignment.

Clone Repository

Within Eclipse, clone the assignment repository.

  1. Open the Git Repositories view by going to Window --> Show View --> Other --> Git Repositories. You can move the view to whichever pane you like. I tend to put the view in the bottom pane.
  2. Click Clone a Git Repository or click the clone icon.
  3. If Eclipse did not populate the form for you, paste the clone URI into the appropriate field.
  4. Enter your user name and password (token) for GitHub.
  5. Click through the screens using "Next" (the defaults should be right: you'll checkout the main branch) and then click "Finish".

Import Project

  1. In the Git Repositories View, expand the project, expanding the Working Tree.
  2. Right click on ServletLab (under Working Tree) and select Import Projects.... Follow along in the dialog, clicking Next/OK, since I don't think any changes are necessary.
  3. Then, we need to make some changes to the project. Right click on the project and go to Properties. Then, click on Project Facets on the right.
    • Check/select Dynamic Web Module and make it be 6.0
    • Set the Java version to be at least Java 17.
  4. Right click on the project, go to "Build Path" and then "Configure Build Path". Under the Libraries tab, select "Classpath", then "Add Library..." and then "Server Runtime". Select "Next" and select "Apache Tomcat v10.1" and Finish.

Login Servlet

Make sure you are committing and pushing your code periodically.

For this part of the lab, you will create a login page/form and a servlet that handles responses from that page.

  1. Create a login page (an HTML 5 document) named login.html, saved in src/main/webapp, that contains a form that has input fields and labels for a username and password. Which method should this form call?
  2. Update the index.html page to link to the login page.
  3. Create a servlet that handles requests from the login page. Which method should be implemented?

    If the user enters the valid username of username and password of passw0rd, the servlet displays a "Congratulations!" response.

    Otherwise, the servlet displays an error message and provides a link for the user to try again. (What are the possible ways that the errors could occur?)

    Refer to the (updated) SurveyServlet code for how to handle if the user doesn't pick an animal.

As you may have noticed, this is a very simple login servlet. This is not how we will verify users in our deployed applications.

Sandwich Servlet

For this part of the lab, you will create a page that responds to your custom sandwich form.

  1. Create a new HTML5 file named sandwich.html
  2. Copy your form.html file into that file.
  3. Copy any CSS files you need as well.
  4. Link to this page from the index.html page.
  5. Create a servlet that handles requests from this form. The response page from the servlet should contain the following content, in the following order:
    • The user's name as a heading. What should be displayed if the user doesn't enter their name?
    • if the order is for here or to go
    • The sandwich's base, bread, and cheese. Examples:
      turkey sandwich on wheat with Swiss cheese
      veggies sandwich on wrap with no cheese
    • The condiments and veggies. If the user did not specify any condiments or veggies, put "plain". (How will you know if the user did not request any condiments or veggies?)
    • The user's comments/instructions.
  6. Modify your sandwich.html form to point to the appropriate servlet so that you can test your servlet.

In the end, your servlet will respond with a page that looks something like the following (although it may not have the same CSS styles):

Smith's Order

For here.

ham sandwich on white with Swiss cheese

lettuce tomato pickles

User Comments: Thank you!

Make sure to test/handle error cases.

Turning in the Assignment

To turn in this assignment, make sure you have committed/pushed all of your code to your remote repository.

Then, export a .war file called Lab4.war from your project using Eclipse. (Include source files in the .war file.) Save the .war file in your git repository. Add the .war file into your git repository. Commit and push the .war file.

Typically, you wouldn't include source files (you just give people your .class files within the .war file), but for this course, you will include source files. You would put the generated .war file into Tomcat's webapps directory for deployment. Tomcat will automatically extract the web application from the file. Later this semester, you'll create .war files to put on the development server for your project.

Make sure that you've added, committed, and pushed all your files (HTML, CSS, servlets) to the git repository.

Grading (100 pts)

Due tonight at 11:59 p.m.