There are a number of ways to declare and access variables in JSPs and JSPX files. The diffences are mostly syntactical. In this tutorial I will cover ways of declaring and accessing using JSP/JSPX declarations and scriptlets and also JSTL and EL methods of achieving the same results.
JSP
We will be discussing the difference in the use of Declaration <%! and Scriptlets <% in JSPs. Firstly in either case you can declare a variable using the standard fashion i.e. int i = 10;.
<%-- Declaration --%> <%! int i = 10; %> <%-- Scriptlet --%> <% int j = 10; %>
What is the difference between the two? These differ in where they appear in the generated servlet. In the case of the declaration <%! the var is declared as a global variable and so will be set for all clients accessing this i.e. it is not thread safe. Where as in the scriptlets <% the variable is declared in the service() method which makes it unique to each client. So depending on the scope you intend it is always best to use the most local scope possible for you work.
To access these vars declared above you can use the JSP expression <%= or in the scriptlets <%. For an example we will add the two vars declared above together and output the result in the page.
<%-- Scriptlet --%>
<% out.println("result="+(i+j)); %>
<%-- Expression --%>
<%= "result="+(i+j) %>
The scriptlet example uses the JSP implicit object out. This uses standard Java syntax ending with a ;. The Expression is slightly different this takes a value that can be output to the screen, this is a heavily over-ridden method and will handle almost anything you pass in. It will throw a run time exception if you pass it a void (i.e. from a method call). Pay attention to the fact that the java ; is unnecessary.
JSPX
The examples here are used in JSPX documents. That is JSPs in XML format but can also be used in standard JSPs. You are required to declared an special namespace to use the following xmlns:jsp="http://java.sun.com/JSP/Page" this should be added to the root element of your document.
<jsp:root version="2.0" xmlns="http://www.w3c.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page"></jsp:root>
To provide a an equivalent to the declaration and scriptlet in the first example we will use <jsp:declaration /> and <jsp:scriptlet />
<!-- Declaration --> <jsp:declaration>int i = 10; </jsp:declaration> <!-- Scriptlet --> <jsp:scriptlet>int j = 10;</jsp:scriptlet>
To avoid getting into detail about the differences between JSP and JSPX I will just summarise by saying that <jsp:declaration> is equivalent to <%! and equally <jsp:scriptlet> is equivalent to <%. If this is the case you have probably guessed how the retrieval will go.
<!-- Scriptlet -->
<jsp:scriptlet>out.println("result="+(i+j));</jsp:scriptlet>
<%-- Expression --%>
<jsp:expression> "result="+(i+j) </jsp:expression>
span.inlinePre{ background:#FEFEFE none repeat scroll 0%; border:1px dashed #2F6FAB; color:black; }
JSTL and EL
JSTL (Java Server Tag Library) and EL (Expression Language) This can be used in either JSP pages or JSPX documents. You will need to import the tag library to use it in
JSP
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
JSPX
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"></jsp:root>
You can then use the core set tag to set a variable and you can specify the scope to set it in. In the var attribute you specify the name in the value attribute you specify the value you want the variable to have. Finally in the scope you specify the scope in which you want the variable to exist there are 4 valid values [page|request|session|application]. When you put that together it will look like
<c:set var="i" scope="page" value="10"/> <c:set var="j" scope="page" value="10"/>
To continue with the examples show above we will output this using EL ${}.
<c:out value="${i+j}"/>
The ${} expression will search all scopes starting with the page and working up to application looking for a variable/attribute in those scope with the name specified. In the case it will find i and j in the page scope and resolve to process them as integers. This will output 100 as in the other cases above. What if you want to remove the variable? The core library provides a remove tag which will do just that.
<c:remove var="i" scope="page"/> <c:remove var="j" scope="page"/>
Here is a simple page which creates a variable in the session attempts to access it and then removes it.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="foo" scope="session" value="bar"/>
<c:out value="${foo}"/> <%-- Output: bar --%>
<%= request.getParameter("foo") %> <%-- Output: null --%>
<%= pageContext.getAttribute("foo") %> <%-- Output: null --%>
<%= session.getAttribute("foo") %> <%-- Output: bar --%>
<c:remove var="foo" scope="session"/>


