Lab 4: Servlets
Page Contents:
Goals
At the completion of this lab, you should be able to- Write basic servlets that return HTML pages
- Change a servlet's behavior based on input data
Set up
- Accept the assignment and clone the repository. For today, we will not integrate using git within Eclipse.
- Create a new dynamic web project named Lab4 Follow
the same set up as before, making sure that the Source folder
is src/main/java and the
Content directory
is src/main/webapp. - Create an
index.html
page that will have links to each of the pages (e.g., the login page and the sandwich page) you create in the lab.
Login Servlet
For this part of the lab, you will create a login page/form and a servlet that handles responses from that page.
- Create a login page (an HTML 5 document) named
login.html
that contains a form that has input fields and labels for a username and password. Which method should this form call? - 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 ofpassw0rd
, the servlet displays a "Congratulations!" response.Otherwise, the servlet displays an error message and provides a link for the user to try again. (This is not the same as what we did in the practice run. We want to display a web page with something more user-friendly to the user.)
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.
- Create a new HTML5 file named
sandwich.html
- Copy your
form.html
file into that file. - Copy any CSS files you need as well.
- 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.
- 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.
Links on index.html
Make sure that the index.html page has links to both parts of the assignment.
Turning in the Assignment
To turn in this assignment, export
a .war
file called Lab4.war
from your project using
Eclipse. (Include source files in the .war file.)
Then, copy 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.
Grading (100 pts)
Due tonight at 11:59 p.m.
- Index Page (5 pts)
- Login form (15 pts)
- Login servlet (30 pts)
- Modified sandwich form (5 pts)
- Sandwich servlet (45 pts)