Accessing Scoped Variables

To permit the JSP page to access the data, the servlet needs to use setAttribute to store the data in one of the standard locations: the HttpServletRequest, the HttpSession, or the ServletContext. Objects in these locations are known as “scoped variables,” and the expression language has a quick and easy way to access them.

You can also have scoped variables stored in the PageContext object, but this is much less useful because the servlet and the JSP page do not share PageContext objects. So, page-scoped variables apply only to objects stored earlier in the same JSP page, not to objects stored by a servlet. To output a scoped variable, you simply use its name in an expression language element.

For example, ${name} means to search the PageContext, HttpServletRequest, HttpSession, and ServletContext (in that order) for an attribute named. If the attribute is found, its toString method is called and that result is returned. If nothing is found, an empty string (not null or an error message) is returned.

So, for example, the following two expressions are equivalent. ${name} <%= pageContext.findAttribute("name") %>

 

http://pdf.coreservlets.com/JSP-EL.pdf

© Prentice Hall and Sun Microsystems Press. Personal use only

posted @ 2015-08-17 17:52  Jerry.Chin  阅读(227)  评论(0编辑  收藏  举报