O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Attributes in a JSP

The example on the opposite page shows the JSP setting an application-scoped attribute using a method declaration that overrides jspInit(). But most of the time you’ll be using one of the four implicit objects to get and set attributes corresponding to the four attribute scopes available in a JSP.

Yes, four. Remember, in addition to the standard servlet request, session, and application (context) scopes, a JSP adds a fourth scope—page scope—that you get from a pageContext object.

You usually won’t need (or care about) page scope unless you’re developing custom tags, so we won’t say any more about it until the custom tags chapter.

 

In a servlet

In a JSP (using implicit objects)

Application

getServletContext().setAttribute(“foo”, barObj);

application.setAttribute(“foo”, barObj);

Request

request.setAttribute(“foo”, barObj);

request.setAttribute(“foo”, barObj);

Session

request.getSession().setAttribute(“foo”, barObj);

session.setAttribute(“foo”, barObj);

Page

Does not apply!

pageContext.setAttribute(“foo”, barObj);

But this isn’t the whole story! In a JSP, there’s another way to get and set attributes at any scope, using only the pageContext implicit object. Turn the page and find out how...

Note

There’s no such thing as “context” scope... even though attributes in application scope are bound to the ServletContext object.

The naming convention might trick you into thinking that attributes stored in the ServletContext are... context scope. But there’s no such thing. Remember, when you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required