使用 gzip 压缩组件 - Java EE

发布于 2024-08-24 05:56:38 字数 260 浏览 6 评论 0原文

我希望提高应用程序的前端性能,因此我在 Firefox 中使用了 YSlow 工具。当我为我的应用程序运行此工具时,在 YSlow 等级选项卡中,它显示了一个问题“使用 gzip 压缩组件时的等级 F”。似乎我们需要在从服务器发送到客户端时压缩文件(js,css)以增加服务器响应时间。

我的应用程序是一个 Struts Java 应用程序。谁能告诉我如何压缩并从服务器发送前端 UI 文件(JS,CSS),以便响应时间增加并且我的页面很快?在服务器上用 Java 压缩这些文件需要做什么?

I am looking to improve front-end performance of my application, so I used YSlow tool in Firefox. When I ran this tool for my app, in the YSlow grade tab it showed up a issue 'Grade F on Compress components with gzip'. Seems to be that we need to compress the files(js, css) while sending from the server to client to increase the server response time.

My app is a Struts Java application. Can anyone let me know how to compress and send the front end UI files(JS,CSS) from server so that the response time increases and my pages lot fastly? What are the things that I need to do to compress these files in Java at server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

故事灯 2024-08-31 05:56:38

我建议使用 Servlet Filter (自 servlet 2.3 规范起)

gzip 过滤器有据可查,因此无需重新发明轮子:

此外,一些 servlet 容器可以动态执行 gzip。看看这个相关问题< /a>.

I recommend to use a Servlet Filter (since servlet 2.3 spec)

A gzip filter is well documented, so there is no need to reinvent the wheel:

Also, some servlet containers can do gzip on the fly. Take a look at this related question.

一身仙ぐ女味 2024-08-31 05:56:38

为了提高 Java EE Web 应用程序的客户端性能,WebUtilities java 库可能会有所帮助。

这是链接::https://github.com/rpatil26/webutilities

它也可以通过 Maven 中心提供

从 0.0.4 版本开始,它有助于以下性能实践。

  1. 最小化 HTTP 请求 - 可以在一个请求中提供多个 JS/CSS 文件
  2. 客户端缓存 - 添加适当的缓存控制、过期标头
  3. 动态 JS/CSS 缩小 - 使用 YUICompressor
  4. 压缩 - 支持 gzip/deflate/compress 编码的 2way 压缩
  5. 响应缓存在服务器上 - 避免重新处理未更改的资源
  6. 添加字符编码 - 让浏览器提前知道

To improve client side performance of Java EE web application, WebUtilities java library can be helpful.

Here is the link :: https://github.com/rpatil26/webutilities.

It is also available through maven central.

Since version 0.0.4 it helps with following performance practices.

  1. Minimize HTTP requests - can serve multiple JS/CSS files in one request
  2. Client Side Caching - adds proper Cache-Control, Expires header
  3. On the fly JS/CSS minification - using YUICompressor
  4. Compression - supports 2way compression for gzip/deflate/compress encodings
  5. Response Caching at Server - to avoid reprocessing of unchanged resources
  6. Add Character Encoding - to let browser know in advance
蔚蓝源自深海 2024-08-31 05:56:38

我建议使用 Granule Tag lib http://code.google.com/p/capsule/

它 gzip 并结合由 g:compress 标签包装的 JavaScript

代码示例是:

<g:compress>
  <script type="text/javascript" src="common.js"/>
  <script type="text/javascript" src="closure/goog/base.js"/>
  <script>
       goog.require('goog.dom');
       goog.require('goog.date');
       goog.require('goog.ui.DatePicker');
  </script>
  <script type="text/javascript">
      var dp = new goog.ui.DatePicker();
      dp.render(document.getElementById('datepicker'));
  </script>
</g:compress>
...

I recommend to use Granule Tag lib the http://code.google.com/p/granule/

It gzip and combine javascripts wrapped by g:compress tag

code sample is:

<g:compress>
  <script type="text/javascript" src="common.js"/>
  <script type="text/javascript" src="closure/goog/base.js"/>
  <script>
       goog.require('goog.dom');
       goog.require('goog.date');
       goog.require('goog.ui.DatePicker');
  </script>
  <script type="text/javascript">
      var dp = new goog.ui.DatePicker();
      dp.render(document.getElementById('datepicker'));
  </script>
</g:compress>
...

浅紫色的梦幻 2024-08-31 05:56:38

您通常在应用程序服务器/servlet容器级别配置它。我不知道您使用的是哪一个,所以这里只是一个 Tomcat 目标示例。在 /conf/server.xml 中配置 组件,如下所示:

<Connector compression="on">

就这样。只需添加compression="on"。其他服务器提供了类似的设置。有关详细信息,请参阅其文档。通常它们是完全相同的,因为它们是构建在 Tomcat 之上的。

有关 Java EE Web 应用程序角度的更多 YSlow 提示,您可以找到此 博客文章更有用。

You normally configure it at appserver/servletcontainer level. I don't know which one you're using, so here's just a Tomcat targeted example. In the /conf/server.xml configure the <Connector> component as follows:

<Connector compression="on">

That's all. Just add compression="on". The other servers provides a comparable setting. consult its documentation for details. Often it's exactly the same because they're built on top of Tomcat.

For more YSlow hints in Java EE webapplication perspective you may find this blog article more useful.

哽咽笑 2024-08-31 05:56:38

您确定您正在尝试压缩 JS 和 CSS 而不是试图缩小它吗?一般来说,只要 Web 服务器正确设置了缓存标头,JS 和 CSS 就会在第一次访问后由浏览器缓存。在实践中,我发现缩小 JS 和 CSS 通常对于浏览器的初始下载来说已经足够了。

有很多 JS 压缩器。例如,位于此处

Are you sure you are trying to compress JS and CSS rather than trying to minify it? Generally JS and CSS are cached by the browser after the first visit provided your caching headers are set correctly by your web server. In practice I have found that minifying JS and CSS is usually good enough for the initial download by the browser.

There are many JS minifiers. For example one is located here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文