Exercise
4.1. Put you email address and phone number in DD as init-parameters, and then output them in the servlet.
4.2. Use annotations to solve Exercise 4.1.
4.3. We can use init parameters as the default parameters for requests. For example, we expect that user would submit a form with his name and city, but the city is optional. So in the servlet, we shall assign a default value for city, let's say Chengdu, if it is empty. Because we don't like hard coding, we could consider default values as init parameters.
Please implement the code in a servlet.
4.4. Based on Exercise 4.3, the optional parameters can be more than one (city, gender, occupation), so we can design a method in the servlet to reduce duplicates. Assume all parameters are strings.
private String getRequestParameter(
HttpServletRequest request,
String name) {
}
Please implement the method above.
4.5. In LogListener.java
(FileLogListener.java
) of ch4/listener
, the path of log file is hard-coded. Please set its path to an init parameter.
4.6. Will the container create a session for the mini mvc project (ch2/mini-mvc
)? Hint: you can inspect the Network
in InPrivate Window.
4.7. Please try to answer the question in Section 4.3: Under what conditions is a JSESSIONID created?
4.8. Use isNew()
method of HttpSession
to refactor HelloServlet.java
of ch4/session
.
4.9. Try to disable the cookies for your web browser, and then run HelloServlet.java
of ch4/session
.
4.10. Try to figure out what the default max inactive interval of HttpSession
is.
4.11. What if you use a session after it is killed?
session.invalidate(); // or `session.setMaxInactiveInterval(0)`;
String foo = (String) session.getAttribute("foo");
4.12. What is the max age of the JSESSIONID cookie?
4.13. Implement log in for ch4/cookie
.
4.14. Design a shopping chart for the book-selling website. To be specific, users can view books in the home page, and she can pick up books to adding into the shopping cart. She can also view details in her shopping cart.
- Hint: Use Session.
- Optional: The books in the home page can also be loaded by the attribute of a request.