Lab 4: Servlets
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.
Clone Repository
Within Eclipse, clone the assignment repository.
- Open the
Git Repositories
view by going toWindow --> 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. - Click
Clone a Git Repository
or click the clone icon. - If Eclipse did not populate the form for you, paste the clone URI into the appropriate field.
- Enter your user name and password (token) for GitHub.
- Click through the screens using "Next" (the defaults should be
right: you'll checkout the
main
branch) and then click "Finish".
Import Project
- In the Git Repositories View, expand the project, expanding the Working Tree.
- Right click on
ServletLab
(under Working Tree) and selectImport Projects...
. Follow along in the dialog, clicking Next/OK, since I don't think any changes are necessary. - 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.
- Check/select
- 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.
- Create a login page (an HTML 5 document) named
login.html
, saved insrc/main/webapp
, that contains a form that has input fields and labels for a username and password. Which method should this form call? - Update the
index.html
page to link to the login page. - 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. (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.
- Create a new HTML5 file named
sandwich.html
- Copy your
form.html
file into that file. - Copy any CSS files you need as well.
- Link to this page from the
index.html
page. - 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.
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.
- Index Page (5 pts)
- Login form (15 pts)
- Login servlet (30 pts)
- Modified sandwich form (5 pts)
- Sandwich servlet (45 pts)