When I call it from main.jsp
, test
is printed out as expected:
<%@ tag language="java" pageEncoding="utf-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
jspContext.setAttribute("test", "test");
%>
<c:out value="${test}"/>
But if I remove the last line and instead try to print the value of test
from main.jsp
, it doesn't work:
<c:out value="${test}"/>
Why can I not access the test
variable from the enclosing page but it is accessible from inside the JSP tag file?
Because the jsp context of the JSP is different from the one of the tag. You could see it as a method calling another method. If the second method declares a local variable, the first method won't see it. It's not in its scope.
See http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html#wp89909 for how to use out variables in JSP tag files.