jsp 文件中包含大量 jspf 文件会导致内存使用率较高

发布于 2024-12-05 10:41:44 字数 1548 浏览 5 评论 0原文

我们有一个网页,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-12-12 10:41:44

使用 jsp:include 而不是 jsp:directive.include 解决了这个问题。
据我从我的研究中了解到, jsp:directive.include (包含指令)在编译时将文件包含到 JSP 页面中,而 jsp:include 包含以下位置的输出:运行时。

I have found that, include action (runtime include) runs a bit slower 
yet it is preferred generally because it save a lot of memory of the system. 

来源

Using jsp:include instead of jsp: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 whereas jsp:include includes the output at runtime.

I have found that, include action (runtime include) runs a bit slower 
yet it is preferred generally because it save a lot of memory of the system. 

(source)

凉薄对峙 2024-12-12 10:41:44

您正在使用 JSF。在视图创建时,如果 if 语句的计算结果为 true,页面上的 JSF 控件将被添加到组件树中。对于服务器端状态保存,这些 UIComponent 实例和 它们的状态将(默认情况下)保留在用户的会话中。添加的控件越多,消耗的内存就越多。默认情况下,会话中会保留许多旧视图。

您可以尝试:

  • 不构建大型对象图
  • 减少会话中的视图数量(请参阅 com.sun.faces.numberOfViewsInSession 和 com.sun.faces.numberOfLogicalViews 或同等内容实现的初始化参数)
  • 使用带有 部分状态保存(如果您是)还没有(这可能涉及升级 Glassfish)
  • 实现 StateManager 将状态保存到 RAM 之外(例如保存到数据库,但这会导致其自身的问题)或使用默认实现切换到客户端状态保存(请参阅 javax .faces.STATE_SAVING_METHOD - 这会带来安全问题,并可能会改变应用程序的行为)

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:

  • Not building large object graphs
  • Reducing the number of views in the session (see com.sun.faces.numberOfViewsInSession and com.sun.faces.numberOfLogicalViews or equivalent initialization parameters for your implementation)
  • Using a JSF version with partial state saving if you are not already (this may involve upgrading Glassfish)
  • Implementing a StateManager to save your state out of RAM (e.g. to a database, but this leads to its own problems) or switching to client-side state saving with the default implementation (see javax.faces.STATE_SAVING_METHOD - this comes with security concerns and may alter application behaviour)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文