jsp 文件中包含大量 jspf 文件会导致内存使用率较高
我们有一个网页,在 glassfish 服务器上运行。在我们的 jsp 文件中,我们有很多包含,如下所示。
<jsp:directive.include file="johndoe/foobar.jspf"/>
这些文件根据用户选择包含在内。我们的jsp文件基本上是这样的:
<jsp:root version="2.1" xmlns:c="http://java.sun.com/jstl/core_rt"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:webuijsf="http://www.sun.com/webui/webuijsf">
//some unimportant codes here
<c:if test="${sessionScope.loadAvgTest}">
//some unimportant codes here
<f:subview id="reports15">
<jsp:directive.include file="siforms/sysinfoform.jspf"/>
</f:subview>
//some unimportant codes here
<f:subview id="reports16">
<jsp:directive.include file="siforms/sysinfoformscheduled.jspf"/>
</f:subview>
//some unimportant codes here
</c:if>
//some unimportant codes here
<c:if test="${sessionScope.bandwidthTest}">
//some unimportant codes here
<f:subview id="reports17">
<jsp:directive.include file="mailforms/mailfilter.jspf"/>
</f:subview>
//some unimportant codes here
<f:subview id="reports18">
<jsp:directive.include file="mailforms/mailfilterscheduled.jspf"/>
</f:subview>
//some unimportant codes here
</c:if>
....
大约有80个这样的if语句,每个语句都包含2个inculdes。当我删除了很多这样的 if 子句并只留下一些 if 和一些包含内存使用情况时,就很好了。但随着我使用更多的 if 子句和更多的包含,内存使用量会增加。有什么想法可以优化代码或如何对 servlet 配置进行配置更改以降低内存使用量?
We have a web page, running on a glassfish server. In our jsp file we have a lot of includes like the one below.
<jsp:directive.include file="johndoe/foobar.jspf"/>
These files are included according to the user choice. Our jsp file is basically like this:
<jsp:root version="2.1" xmlns:c="http://java.sun.com/jstl/core_rt"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:webuijsf="http://www.sun.com/webui/webuijsf">
//some unimportant codes here
<c:if test="${sessionScope.loadAvgTest}">
//some unimportant codes here
<f:subview id="reports15">
<jsp:directive.include file="siforms/sysinfoform.jspf"/>
</f:subview>
//some unimportant codes here
<f:subview id="reports16">
<jsp:directive.include file="siforms/sysinfoformscheduled.jspf"/>
</f:subview>
//some unimportant codes here
</c:if>
//some unimportant codes here
<c:if test="${sessionScope.bandwidthTest}">
//some unimportant codes here
<f:subview id="reports17">
<jsp:directive.include file="mailforms/mailfilter.jspf"/>
</f:subview>
//some unimportant codes here
<f:subview id="reports18">
<jsp:directive.include file="mailforms/mailfilterscheduled.jspf"/>
</f:subview>
//some unimportant codes here
</c:if>
....
There are approximately 80 if statements like this each one containing 2 inculdes. When I removed a lot of these if clauses and leaved only a few if and a few include memory usage was fine. But as I use more if clauses and more includes the memory usage grows. Any ideas how I can optimize the code or how I can make configuration changes to the servlets configuration to lower the memory usage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
jsp:include
而不是jsp:directive.include
解决了这个问题。据我从我的研究中了解到,
jsp:directive.include
(包含指令)在编译时将文件包含到 JSP 页面中,而jsp:include
包含以下位置的输出:运行时。(来源)
Using
jsp:include
instead ofjsp:directive.include
solved the problem.As far as I have learned from my research
jsp:directive.include
(include directive) includes the file into the JSP page at compile time whereasjsp:include
includes the output at runtime.(source)
您正在使用 JSF。在视图创建时,如果
if
语句的计算结果为 true,页面上的 JSF 控件将被添加到组件树中。对于服务器端状态保存,这些 UIComponent 实例和 它们的状态将(默认情况下)保留在用户的会话中。添加的控件越多,消耗的内存就越多。默认情况下,会话中会保留许多旧视图。您可以尝试:
You're using JSF. At view creation time, if an
if
statement evaluates to true, the JSF controls on the page will be added to the component tree. For server-side state saving, these UIComponent instances and their state will (by default) be persisted in the user's session. The more controls you add, the more memory you are going to consume. By default, a number of old views are persisted in the session.You could try:
com.sun.faces.numberOfViewsInSession
andcom.sun.faces.numberOfLogicalViews
or equivalent initialization parameters for your implementation)javax.faces.STATE_SAVING_METHOD
- this comes with security concerns and may alter application behaviour)