Skip to content

Lab 3-6 - Using the ServletContext

Instructions

Research

We have not yet discussed ServletContext in this class, but it is similar to the HttpServletRequest with regard to the ability to set and get attributes. This is something I'd like you to explore on your own. These short articles might be good places to begin to understand ServletContext:


Coding

Part 1

  1. Create a JSP page named lab36.jsp and save it into your projects/public_html directory.
  2. Add a JSP declaration section to the file.
  3. Add a jspInit() method to the declaration section that does the following:
    • Access the ServletContext object.
    • Add an attribute to the ServletContext. The key and value should be Strings.
  4. Add a JSP expression that outputs the value of the attribute you added in the jspInit() method.
  5. Start Tomcat and access the page.

Part 2

  1. Create another JSP page named lab36b.jsp and save it into your projects/public_html directory.
  2. Add a JSP scriptlet section to the page. In the scriptlet get the ServletContext attribute added in the first JSP page and output the value.
  3. Access this page with a browser. Make sure that the first page has been accessed before this one.
  4. Open the translated code in codespaces and view the results.
    • the path to the translated code: /home/student/tomcat/work/Catalina/localhost/java112/org/apache/jsp/

Screenshots

To receive credit for this lab save screenshot(s) in the projects/screenshots/week10 directory.

You may combine the below into one screenshot and name it lab6.png.

  1. lab6-servlet-context-1.png: The lab36.jsp running in the browser.
  2. lab6-servlet-context-2.png: The lab36b.jsp 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 10 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?
    
    
    #### Bonus Question! How could the ServletContext attribute be displayed in the JSP using EL?
    
  5. Click "Submit new issue"


Rubric

All of the following must be satisfied to achieve a "Met" status

  • Screenshot clearly shows the servlet running in the browser.
  • Code and screenshot are properly named and saved in the correct directory.
  • All lab steps have been accurately and appropriately implemented.

Additional Considerations

  • Issue created correctly with thoughtful answers to the reflection questions.
  • Correct answer given to the Bonus Question in the issue.
  • The ServletContext is retrieved in the simplest way possible (code shouldn't be overly complex).
  • Correct use of the ServletContext.