1

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?

Brian
  • 93
  • 1
  • 3
  • 8

1 Answers1

2

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks - I misunderstood how tag files work - I thought you could set a variable that could be seen by the calling page. – Brian Dec 25 '11 at 19:55