1 / 52

flic.kr/p/7sKCJs

The Servlet Lifecycle. http://flic.kr/p/7sKCJs. What are you going to learn about today?. More details about the servlet lifecycle A difference between GET and POST that wasn ’ t mentioned yet A pitfall to watch out for in servlet development. http://flic.kr/p/8JpkTg.

deanal
Download Presentation

flic.kr/p/7sKCJs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Servlet Lifecycle http://flic.kr/p/7sKCJs

  2. What are you goingto learn about today? • More details about the servlet lifecycle • A difference between GET and POST that wasn’t mentioned yet • A pitfall to watch out for in servlet development http://flic.kr/p/8JpkTg

  3. Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96

  4. Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96

  5. Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96

  6. Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96

  7. Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96

  8. Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96

  9. Think back to step 1… Where did the servlet come from? When was it created? What is its lifecycle? http://flic.kr/p/9ksxQa

  10. ServletLifecycle Head First Servlets and JSP (2nd edition), p. 97

  11. ServletLifecycle But when doesthis happen? Head First Servlets and JSP (2nd edition), p. 97

  12. ServletLifecycle But when doesthis happen? Answer: Before the first call to service(). Beyond that it’s undefined Head First Servlets and JSP (2nd edition), p. 97

  13. Inherited lifecyclemethods Possibly override init() Possibly override destroy()??? Abstractclasses Probably don’t override service() Override at least one doX() Head First Servlets and JSP (2nd edition), p. 98

  14. Where do you put servlet initialization code?In the constructor or in init()? http://flic.kr/p/9ksxQa

  15. Where do you put servlet initialization code?In the constructor or in init()? In init() because after the constructor runs, but before init() runs, the servlet is in a Schroedinger’s servlet state—something more than an object and less than a servlet http://flic.kr/p/9ksxQa

  16. What does being a servlet get you? • ServletConfig object • One per servlet • Params set in the DD (web.xml) • Use for deploy-time info • Use to access ServletContext object • ServletContext object • One per web app (i.e., shared by servlets) • Params set in the DD (web.xml) • Use as web app “bulletin board” • Use to get server info (e.g., name/version of container)

  17. ServletConfig example web.xml <servlet> <servlet-name>BeerParamTests</servlet-name> <servlet-class>TestInitParams</servlet-class> <init-param> <param-name>adminEmail</param-name> <param-value>likewecare@wickedlysmart.com</param-value> </init-param> </servlet> servlet code out.println(getServletConfig().getInitParameter(“adminEmail”));

  18. How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152

  19. How a ServletConfig gets created/initialized NOTE: Container reads web.xml only when it’s creating a servlet(subsequent changes to the file won’t be noticed) Head First Servlets and JSP (2nd edition), p. 152

  20. How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152

  21. How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152

  22. How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152

  23. How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152

  24. How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152

  25. Time for a ServletConfig demo! http://flic.kr/p/5dfuqL

  26. How can a JSP get ServletConfig data? http://flic.kr/p/9ksxQa

  27. How can a JSP get ServletConfig data? Pass data in with request (like before) Servlet code // inside the doPost() method String color = request.getParameter(“color”); BeerExpert be = new BeerExpert(); List result = be.getBrands(color); request.setAttribute(“styles”, result); … or use ServletContext http://flic.kr/p/9ksxQa

  28. ServletContext example web.xml <servlet> <servlet-name>BeerParamTests</servlet-name> <servlet-class>TestInitParams</servlet-class> </servlet> <context-param> <param-name>adminEmail</param-name> <param-value>clientheaderror@wickedlysmart.com</param-value> </context-param> servlet code out.println(getServletContext().getInitParameter(“adminEmail”)); or ServletContext context = getServletContext(); out.println(context.getInitParameter(“adminEmail”));

  29. Recall: Servlet Lifecycle How can you run web-app-wide initialization code? Head First Servlets and JSP (2nd edition), p. 97

  30. Answer: Listeners! import javax.servlet.*; public class MyServletContextListenerimplements ServletContextListener { public void contextInitialized(ServletContextEvent event) { //code to initialize the database connection //and store it as a context attribute } public void contextDestroyed(ServletContextEvent event) { //code to close the database connection } } Listeners exist for most key events in the lifecycle

  31. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  32. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  33. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  34. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  35. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  36. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  37. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  38. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  39. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  40. How a ContextListener works NOTE: InitParams are StringsAttributes are Objects Head First Servlets and JSP (2nd edition), pp. 178–179

  41. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  42. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  43. How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179

  44. Do you remember? What two HTTP request “methods” do we keep talking about?How are they different? http://flic.kr/p/9ksxQa

  45. Do you remember? • GET: No data payload, but parameters can be passed to the server as part of the URL • POST: Has a data payload Not so fast. There’s more… What two HTTP request “methods” do we keep talking about?How are they different? http://flic.kr/p/9ksxQa

  46. Idempotency Head First Servlets and JSP (2nd edition), p. 115

  47. Idempotent operations run repeatedly always produce the same result (i.e., no side effects) GET should always be idempotent Head First Servlets and JSP (2nd edition), p. 115

  48. Idempotent operations always produce the same result (i.e., not side effects) POST is generally not idempotent What problems might a lack of idempotency cause? Head First Servlets and JSP (2nd edition), p. 115

  49. Recall this step in a servlet transaction Head First Servlets and JSP (2nd edition), pp. 95–96

  50. Watch out for the concurrency! http://flic.kr/p/7asM5k Head First Servlets and JSP (2nd edition), p. 100

More Related