j2ee web fundamentals lesson 5 attributes and listeners

35
J2EE Web Fundamentals Lesson 5 Attributes and Listeners Instructor: Dr. Segun Adekile

Upload: gitel

Post on 24-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

J2EE Web Fundamentals Lesson 5 Attributes and Listeners. Instructor: Dr. Segun Adekile. Outline. Objectives Course Text Book Basham, Bryan; Sierra, Kathy; Bates, Bert (2012-10-30). Head First Servlets and JSP (Kindle Location 1349). O'Reilly Media. Kindle Edition. Attributes and Listeners - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

J2EE Web FundamentalsLesson 5

Attributes and Listeners

Instructor: Dr. Segun Adekile

Page 2: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Outline

• Objectives• Course Text Book– Basham, Bryan; Sierra, Kathy; Bates, Bert (2012-

10-30). Head First Servlets and JSP (Kindle Location 1349). O'Reilly Media. Kindle Edition.

• Attributes and Listeners– Being a Web App

Page 3: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Objectives

Page 4: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Agenda

• Init Parameters• Context Init Parameters• ServletConfig VS. ServletContext• ServletContextListener• The Eight Listeners• Attributes• Request Dispatching

Page 5: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Init Parameters - scenario

Page 6: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Init Parameters - solution

Page 7: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Init Parameters - process

Page 8: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Context Init Parameter - scenario

Page 9: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Context Init Parameter - scenario

Page 10: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Context Init Parameter - solution

Page 11: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletConfig VS ServletContext

Page 12: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletContext: API

Page 13: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletContextListener: process

Page 14: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletContextListener: example

Page 15: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletContextListener

Page 16: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletContextListener

Page 17: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletContextListener

Page 18: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

ServletContextListener

Page 19: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

The Eight Listeners

Page 20: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

The Eight Listeners (continued…)

Page 21: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attributes

Page 22: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attributes

Page 23: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attribute API

Page 24: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attributes

Page 25: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attribute and Thread Safety• Everyone in the app has access to context attributes, and that means multiple

servlets. And multiple servlets means you might have multiple threads, since requests are concurrently handled, each in a separate thread. This happens regardless of whether the requests are coming in for the same or different servlets.

• Basham, Bryan; Sierra, Kathy; Bates, Bert (2012-10-30). Head First Servlets and JSP (Kindle Locations 4120-4122). O'Reilly Media. Kindle Edition.

Page 26: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attribute and Thread Safety

Page 27: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attribute and Thread Safety

Page 28: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attribute and Thread Safety• Synchronizing the service method is a spectacularly BAD idea

Page 29: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attribute and Thread Safety

• Are Session attributes thread-safe?

Page 30: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Attributes - Summary

Page 31: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Request Attributes and Request Dispatching

• Request attributes make sense when you want some other component of the app to take over all or part of the request.

• Our typical, simple example is an MVC app that starts with a servlet controller, but ends with a JSP view. The controller communicates with the model, and gets back data that the view needs in order to build the response.

• There’s no reason to put the data in a context or session attribute, since it applies only to this request, so we put it in the request scope.

Page 32: J2EE Web Fundamentals Lesson 5 Attributes and Listeners
Page 33: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Request Dispatching• RequestDispatchers have only two methods

– forward() and include().

• You can get a RequestDispatcher in two ways: – from the request or from the context.

• Regardless of where you get it, you have to tell it the web component to which you’re forwarding the request. In other words, the servlet or JSP that’ll take over.

Page 34: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Request Dispatching

Page 35: J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Recap