J2EE Web Fundamentals Lesson 5 Attributes and Listeners

Preview:

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

Citation preview

J2EE Web FundamentalsLesson 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– Being a Web App

Objectives

Agenda

• Init Parameters• Context Init Parameters• ServletConfig VS. ServletContext• ServletContextListener• The Eight Listeners• Attributes• Request 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 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.

Attribute and Thread Safety

Attribute and Thread Safety

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

Attribute and Thread Safety

• Are Session attributes thread-safe?

Attributes - Summary

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.

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.

Request Dispatching

Recap