如何在JSP中避免递归和多个include指令?

时间:2013-04-10 22:16:18

标签: jsp include

我有4个jsp文件和1个js文件:index.jspconnect.jspload1.jspload2.jspfunction.js。请参阅以下代码以了解它们之间的关系。

index.jsp的

<%@include file = "load1.jsp" %>
<%@include file = "load2.jsp" %>

connect.jsp

<%!
    public String makeConnection(){
        String str = "connect to database";
        return str;
    }
%>

load1.jsp

<%@include file = "connect.jsp" %>
<%
    out.print(makeConnection());
%>
<button onclick = "doXHR()">load2</button>

load2.jsp

<%@include file = "connect.jsp" %>
<%
    out.print(makeConnection());
%>

function.js

function doXHR(){
    ...
    xmlhttp.open("GET", "load2.jsp", true);
    ...
}

请注意,这些代码不是实际代码,因为我必须使其更简单,所以我可以在这里发布。问题是,当我转到index.jsp时,会出现类似makeConnection() is already defined的错误。

如果我从<%@include file = "connect.jsp" %>删除此行load2.jsp,它将会有效,但如果我点击该按钮,则会提供500个状态代码,其错误类似于cannot find symbol: method makeConnection()。登记/> 我怎么解决这个问题?谢谢!

0 个答案:

没有答案