【问题标题】:measure the total transmitted byte sent/received in the HttpRequest/Response in servlets测量 servlet 中的 Http 请求/响应中发送/接收的总传输字节数
【发布时间】:2015-07-08 10:49:21
【问题描述】:

我想测量 servlet 中请求和响应的总传输字节数。

此外,为响应发送的字节数和为请求接收的字节数(http get 和 post 方法)。

实现这一目标的优雅方法是什么?

假设,我正在从 servlet 写入 HTML 内容输出。在该输出中,涉及到 css 样式和 JavaScript 文件。如何获得响应中发送的字节的确切大小?

StringBuilder htmlcontent = new StringBuilder("");
.append("<!DOCTYPE html><!--[if IE 9]><html class='lt-ie10' lang='en' > <![endif]-->")
                    .append("<html class=\"no-js\" lang=\"en\" >")
                    .append("<head>")                   
                    .append("<title>Sample Page</title>")
                    .append("<link rel=\"stylesheet\" href=\"css/foundation-5.4.css\" />")
                    .append("<script type='text/javascript' src='scripts/app.js'></script>")
                    .append("</head>")
                    .append("<body>")
                    .append("<body>")                   
                    .append("</body>")
                    .append("</html>");

response.getOutputStream().write(htmlcontent).getBytes("UTF-8");

【问题讨论】:

  • 我首先实现一个带有计数功能的响应包装器,然后使用过滤器将标准响应包装到我的计数中。
  • request.getContentLength();可用于获取请求长度

标签: jsp servlets tomcat7


【解决方案1】:

每个容器都支持记录网络请求。您应该能够记录以下内容:

  • 请求网址
  • HTTP 方法
  • 响应状态码
  • 响应大小
  • 请求大小。

对于tomcat,这里是你可以做的,

http://www.techstacks.com/howto/configure-access-logging-in-tomcat.html

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 2011-03-03
    • 1970-01-01
    • 2017-02-02
    • 2013-09-29
    • 2011-05-05
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    相关资源
    最近更新 更多