Sharing data between JSP files?
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>
<!-- ... -->
Thi开发者_开发问答s 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
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
answer: Pass data from Java Servlet to JSP?
精彩评论