Thursday, May 24, 2007

JSP: Setting and accessing variables

A new variable can be declared in JSP with the <c:set&ht; tag. Example:
<c:set var="variable_name" value="value" scope="page|session|request|application">

As the example above indicates, we can select among the four available scopes:

  • page
  • request
  • session
  • application

The variable can be printed out with the <c:out> tag. It can be accessed in EL (expression language) with the following syntax:

and from within a scriptlet as follows:
<%= pageContext.getAttribute(variable_name) %>
<%= request.getAttribute(variable_name) %>
<%= session.getAttribute(variable_name) %>
<%= application.getAttribute(variable_name) %>

No comments: