tags:

views:

15

answers:

1

Hi,

Can a variable declared in one jsp file be used in another, if imported? Example:

// index.jsp
<%
int count = 40;
%>

<%@include file='go.jsp'%><%


// go.jsp
<%
count += 10;
%>

is count visible to go.jsp? I am guessing this is not good design anyway (expecting global variables to be around for you from another page), just wondering if this can work while I prototype.

Thanks

+1  A: 

Yes, it will work. Your JSP is compiled to a servlet, whose doGet(..) method contains the code of all included pages.

If you want something like that, you'd better place the variable in the page context - pageContext.setAttribute("attrName", value); and then retrieve it with the corresponding method.

If using JSTL, you can do this by using <c:set var="varName" value="yourValue" /> and then use the variable in JSTL expressions: ${varName}

Bozho