j2ee web fundamentals lesson 5 attributes and listeners

Download J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Post on 24-Feb-2016

39 views

Category:

Documents

0 download

Embed Size (px)

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

J2EE Web Fundamentals Lesson 5 Attributes and Listeners

J2EE Web FundamentalsLesson 5Attributes and ListenersInstructor: Dr. Segun Adekile

OutlineObjectivesCourse Text BookBasham, Bryan; Sierra, Kathy; Bates, Bert (2012-10-30). Head First Servlets and JSP (Kindle Location 1349). O'Reilly Media. Kindle Edition. Attributes and ListenersBeing a Web App

Objectives

AgendaInit ParametersContext Init ParametersServletConfig VS. ServletContextServletContextListenerThe Eight ListenersAttributesRequest Dispatching

Init Parameters - scenario

Init Parameters - solution

Init Parameters - process

Context Init Parameter - scenario

Context Init Parameter - scenario

Context Init Parameter - solution

ServletConfig VS ServletContext

ServletContext: API

ServletContextListener: process

ServletContextListener: example

ServletContextListener

ServletContextListener

ServletContextListener

ServletContextListener

The Eight Listeners

The Eight Listeners (continued)

Attributes

Attributes

Attribute API

Attributes

Attribute and Thread SafetyEveryone 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.

Attribute and Thread Safety

26Attribute and Thread Safety

Attribute and Thread SafetySynchronizing the service method is a spectacularly BAD idea

28Attribute and Thread SafetyAre Session attributes thread-safe?

Attributes - Summary

Request Attributes and Request DispatchingRequest 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. Theres 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.

Request DispatchingRequestDispatchers 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 youre forwarding the request. In other words, the servlet or JSP thatll take over.

Request Dispatching

Recap