1

I have two JSP files on a GAE project. I'm trying to pass data between them:

index.jsp:

<%! String title = "foo"; %>
<jsp:include page="templates/header.jsp" />

templates/header.jsp:

<!-- ... -->
<title><%=title%></title>
<!-- ... -->

This fails, saying that title can't be resolved. What is the right way to do this?

The error is:

An error occurred at line: 8 in the jsp file: /templates/header.jsp title cannot be resolved

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • possible duplicate of [Pass data from Java Servlet to JSP?](http://stackoverflow.com/questions/5414600/pass-data-from-java-servlet-to-jsp) – BalusC Jun 06 '11 at 11:49

2 Answers2

2

The quickest way for your existing code to make use of the JSP defined variable is to switch your jsp:include tag for the jsp include directive...

Instead of writing

<jsp:include page="templates/header.jsp" />

you should change it to:

<%@include file="templates/header.jsp" %>

The difference in tag works due to how the JSP compiler treats each tag type: Using the directive, as defined above, more or less just tells the compiler to dump the contents of the included file, in your current file, where as the jsp:include tag tells the compiler to process the include file (as a separate entity), and incorporate its output into the current file.

HTH

Crollster
  • 2,751
  • 2
  • 23
  • 34
0

answer: Pass data from Java Servlet to JSP?

Community
  • 1
  • 1
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699