Lab 5 - Servlet Practice
Objective
Practice writing a servlet and to understand the use of init()
, doGet()
, instance variables and local variables in a servlet.
Instructions
- Create a new servlet and place it in the java112.project2 package.
- Override the
init()
method.- Add a
log()
statement inside theinit()
method to track when this method executes.
- Add a
- Override the
doGet()
method.- Add a
log()
statement inside thedoGet()
method to track when this method executes.
- Add a
- Build and deploy your servlet. Do NOT access the servlet in the browser yet!
- Check the log files. Which
log()
statements, if any, executed? - Now, access the servlet in the browser.
- Check the log files. Which
log()
statements, if any, executed? - Add code to the servlet to display to the user the number of times the servlet has been accessed in it's lifetime (i.e., add a hit counter).
- Create a method that returns the current date/time.
- Add code to the servlet to display to the user the date/time when the servlet was accessed for the first time after deployment. If five different users accessed the servlet, they should all see the same "first time accessed" value.
- Add code to the servlet to display the current date/time.
- Once that is working, display the three pieces of information in a table.
- Add your answers to the two questions above, so they are displayed below the table.
Tip
- Read the objective of the lab and think about the techniques it is referencing.
- The course materials for this week along with your previous learning should provide you with everything you need to complete this.
- Avoid the "google rabbit hole"! We haven’t learned about session tracking or fancy stuff like that so it should not be used to solve this.
Bonus Task!
optional
- Find the javadoc for HttpServlet and review the methods available on HttpServlet.
- In the servlet you created above, override the
destroy()
method. - Add a
log()
statement inside thedestroy()
method to track when this method executes. - Build and deploy your servlet.
- Access the servlet in the browser.
- Check the log files. Which
log()
statements, if any, executed? Wasdestroy()
called? - How can you trigger the
destroy()
method? Verify your answer by testing your theory.
Screenshots
To receive credit for this lab save a screenshot in the projects/screenshots/week6
directory.
lab5-servlet.png
: The servlet running in the browser.
Submit Weekly Labs
- Add, commit, and push!
- Verify all work, including screenshots are visible in GitHub.
- Create a new issue in GitHub: "Week 6 ready for review".
-
Copy the below text and paste it into the "Add a description" textarea. Answer the reflection questions in the space provided.
@kkschumacher #### How are you doing? **Are you encountering any issues preventing you from completing this week's assignments? What can I do to better support you this week?** #### What did you find particularly enlightening this week? #### What challenges (if any) did you encounter in your learning this week? #### What questions (if any) do you have about the material covered this week? #### In Lab 2 - Linking, explain why the first way of linking to the home page was "wrong", and why the second way is best.
-
Click "Submit new issue"
Rubric
All of the following must be satisfied to achieve a "Met" status
- Screenshot should clearly shows the servlet running in the browser.
- Code and screenshots are properly named and saved in the correct directory.
- All lab steps have been accurately and appropriately implemented.
- Code adheres to the course coding standards.
- Commit messages are concise, atomic, and effectively describe the change(s) made.
- JavaDoc documentation is properly implemented, providing clarity and understanding of the code's functionality and usage.
- External sources (websites, classmates, AI tools, etc), if utilized, are referenced and documented within the code as comments.
Additional Considerations
- Issue created correctly with thoughtful answers to the reflection questions.
- Correct explanation given to why the first way of linking in Lab 2 was incorrect.