What are the scopes in jsp?

Most of the time in jsp you need scopes to get and set attributes. In addition to the servlet request, session, application(context) jsp has another “page” scope that you can get from the pageContext object.

Scope---In Servlet vs In JSP
Page---(not in servlet)---pageContext.setAttribute()

The request scope exists until the client get the response. Session scope exists for a client till the session ends. Context scope is available always for all the servlets and jsps. Page scope is needed at the time of developing custom tags.
You can use a page scope to get attributes from any other scope. To get and set the attributes for any scope by pageContext we can use APPLICATION_SCOPE, PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE. Methods to get any scope from pageContext are getRequest(), getServletContext(), getServletConfig(), getSession().

Setting/Getting page scope:

Using page scope to set/get session scope attributes:
pageContext.getAttribute(“name”, pageContext.SESSION_SCOPE);

Using page scope to get/set application scope attributes:

Using pageContext to get unknown scope attribute:
First it looks the pageContext after that it looks for other scopes(request then session then application) for “name”. The first attribute it finds with that name it stops searching.

No comments:

Post a Comment