如何检查jsp文件是否包含在另一个jsp文件中?
看看你们都知道如何在 jsp 中包含文件... 我有 2 个文件
one.jsp Two.jsp
给出了下面的代码
Long something = 0;
in Two.jsp 我在 one.jsp 代码中
<%@include file='two.jsp'%>
<%@include file='two.jsp'%>
,我已经两次包含相同的文件,
所以会出现错误,因为将创建一些变量 2 次,对吗?
所以这意味着在第二次包含文件之前我应该检查一下,如果 一旦包含,我就不应该再次包含它,如何检查 那 ?
就像在 php 中一样,有类似的函数 包含(“文件.php”); //这将包括文件
和 include_once("文件.php"); //如果之前未包含该文件,则这将包含
该文件,有任何解决方案吗???
see you all know about including file in a jsp...
i have 2 files
one.jsp
two.jsp
in two.jsp i have below given code
Long something = 0;
in one.jsp code is
<%@include file='two.jsp'%>
<%@include file='two.jsp'%>
i have included same file two times,
so there will be error because something variable will be created 2
times, right?
so it means before including file second time i should check that, if
once its included then i should not inculde it again, how to check
that ?
as in php there are functions like
include("file.php");
//that will include file
and
include_once("file.php");
//this will include file if its not included before,
ANY ANY solutions for that????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,拥有这两个静态包含将导致同一变量声明两次的错误。这些导入在编译时会将包含页面的代码附加到包含页面中,然后一起编译。
我不知道 JSP 中内置有任何
include_once
方法,但您可以通过使用全局Set
(即HashSet
),声明为顶级页面中的变量,或声明为请求属性(处理结束时应清除该属性),您可以在其中添加已包含的页面的名称。one.jsp
two.jsp
请注意,这与 C 中的
#ifndef #define #endif
模式非常相似。请考虑使用动态包括您将避免出现重复变量的问题。包含的页面将在其自己的范围内执行,要访问服务器端变量,您必须使用
或在Request
、Session
或Application
范围。有关静态和动态包含之间的差异,请参阅此问题的答案: include指令和 jsp:include 属性名称问题此外,如果您有大量“条件”静态包含,则最终可能会遇到 64K 方法限制
Indeed, having those two static includes will result in an error of having the same variable declared twice. Those imports, at compilation time, will append the code of the included page in the including one, and will then be compiled altogether.
I'm not aware of any
include_once
approach built-in in JSP, but you could do something similar by having a globalSet
(i.e.HashSet
), declared as a variable in the top-level page, or as a request attribute (that you should be clearing when the processing ends) in which you could be adding the names of the pages already included.one.jsp
two.jsp
Note this is quite similar to the
#ifndef #define #endif
pattern in C.Take into account that using dynamic includes you would be avoiding the problem of having duplicate variables. The included page would execute on its own scope, and to access server-side variables you'll have to pass them through with
<jsp:param>
or in one ofRequest
,Session
orApplication
scopes. See this question's answer for the differences between static and dynamic includes: include directive and jsp:include attribute name problemAdditionally, if you have a great number of those "conditional" static includes, you can end up hitting the 64K method limit