520 likes | 537 Views
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.
E N D
The Servlet Lifecycle http://flic.kr/p/7sKCJs
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
Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96
Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96
Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96
Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96
Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96
Recall how a servlet processes a request Head First Servlets and JSP (2nd edition), pp. 95–96
Think back to step 1… Where did the servlet come from? When was it created? What is its lifecycle? http://flic.kr/p/9ksxQa
ServletLifecycle Head First Servlets and JSP (2nd edition), p. 97
ServletLifecycle But when doesthis happen? Head First Servlets and JSP (2nd edition), p. 97
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
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
Where do you put servlet initialization code?In the constructor or in init()? http://flic.kr/p/9ksxQa
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
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)
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”));
How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152
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
How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152
How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152
How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152
How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152
How a ServletConfig gets created/initialized Head First Servlets and JSP (2nd edition), p. 152
Time for a ServletConfig demo! http://flic.kr/p/5dfuqL
How can a JSP get ServletConfig data? http://flic.kr/p/9ksxQa
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
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”));
Recall: Servlet Lifecycle How can you run web-app-wide initialization code? Head First Servlets and JSP (2nd edition), p. 97
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
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works NOTE: InitParams are StringsAttributes are Objects Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
How a ContextListener works Head First Servlets and JSP (2nd edition), pp. 178–179
Do you remember? What two HTTP request “methods” do we keep talking about?How are they different? http://flic.kr/p/9ksxQa
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
Idempotency Head First Servlets and JSP (2nd edition), p. 115
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
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
Recall this step in a servlet transaction Head First Servlets and JSP (2nd edition), pp. 95–96
Watch out for the concurrency! http://flic.kr/p/7asM5k Head First Servlets and JSP (2nd edition), p. 100