<%
 String str = "";
 // 下面就可以使用str变量了。 
%>你就将include标记理解为把menu.jsp里面的全部内容拷贝到当前jsp页面来了,
合成一个页面,那你说可不可以访问。

解决方案 »

  1.   

    <%
     String str = "";
     // 变量定义
    %>
    能!
      

  2.   

    这样我试了,不行,提示:
    Undefined variable: str
      

  3.   

    出错啊,
    menu.jsp
    <%
    String catName="ttt";
    %>
    index.jsp
    <jsp:include page="const.jsp"/>
    <%=catName%>
      

  4.   

    如果你要实现这种效果,你可以在转到特定的jsp时把这个特定的变量当作参数传过去
    像:
    xxx.jsp?nTemp=12&nTemp2=13
      

  5.   

    我KAO,你上面是menu.jsp,下面的include里是const.jsp,你说能不能取到?
      

  6.   

    写错了,我本地是用
    const.jsp
    <%
    String catName="ttt";
    %>index.jsp
    <jsp:include page="const.jsp"/>
    <%=catName%>
      

  7.   

    a.jsp
    -----
    <%!  // 注意有"!"
      String str = "fuck";
    %>b.jsp
    -----
    <jsp:include page="a.jsp"/>
    <%
      out.write("str="+str);
    %>
      

  8.   

    你们都没有说出问题根源,
    <%@ include file="const.jsp"%>
    和<jsp:include page="const.jsp"/>引用是不同的,用<%@ include file="const.jsp"%>这种方式,变量才可以被index.jsp所用。
    两种引用的JSP编译结果是不同的,一个生成一个servlet,一个生成一个servlet。
      

  9.   

    上面我写错了,更正:
    两种引用的JSP编译结果是不同的,
    一个生成一个servlet,<%@ include file="const.jsp"%>一个生成两个servlet。<jsp:include page="const.jsp"/>JSP编译后,不就生成servlet吗?
    两个servlet,变量当然也就不能直接使用。
      

  10.   

    我试了一下,<jsp:include page = "const.jsp"/>的话,在index.jsp中是不能用声明时加“!”也不行。一般index只能往const里传数据(用<jsp:param name = "paraName" value = "paraValue"/>)!要共享,还是用session或application的setAttribudte好了。
      

  11.   

    <%! 
    %>
    定义全局变量
      

  12.   

    放在一个javabean里不是更好
    设 scope="session"