Skip to content

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

  1. Create a new servlet and place it in the java112.project2 package.
  2. Override the init() method.
    • Add a log() statement inside the init() method to track when this method executes.
  3. Override the doGet() method.
    • Add a log() statement inside the doGet() method to track when this method executes.
  4. Build and deploy your servlet. Do NOT access the servlet in the browser yet!
  5. Check the log files. Which log() statements, if any, executed?
  6. Now, access the servlet in the browser.
  7. Check the log files. Which log() statements, if any, executed?
  8. 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).
  9. 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.
  10. Once that is working, display the three pieces of information in a table.
  11. Add your answers to the two questions above, so they are displayed below the table.

Tip

  1. Read the objective of the lab and think about the techniques it is referencing.
  2. The course materials for this week along with your previous learning should provide you with everything you need to complete this.
  3. 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

  1. Find the javadoc for HttpServlet and review the methods available on HttpServlet.
  2. In the servlet you created above, override the destroy() method.
  3. Add a log() statement inside the destroy() method to track when this method executes.
  4. Build and deploy your servlet.
  5. Access the servlet in the browser.
  6. Check the log files. Which log() statements, if any, executed? Was destroy() called?
  7. 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.

  1. lab5-servlet.png: The servlet running in the browser.

Submit Weekly Labs

  1. Add, commit, and push!
  2. Verify all work, including screenshots are visible in GitHub.
  3. Create a new issue in GitHub: "Week 6 ready for review".
  4. 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.
    
  5. 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.