Servlet 过滤器 - 识别被调用的 Servlet/JSP
我正在编写一个 Servlet Filter 来测量 http 请求和响应时间。
该过滤器部署在 Apache Tomcat 7 Web 服务器上。
我想知道除了解析请求的 URI(例如,检查 .jsp 后缀)之外,是否有任何方法可以识别每个请求正在调用哪个 Servlet 或 JSP?
I'm writing a Servlet Filter which measures http request and response times.
The filter is deployed on Apache Tomcat 7 web server.
I was wondering if there's any way to identify which Servlet or JSP is being called on each request other than parsing the request's URI (and for example, checking for .jsp suffix)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
必须更改 servlet/JSP,以便它们设置特定的请求属性,您可以在
FilterChain#doFilter()
调用后获得该属性。我只能给出提示
HttpServletRequest#getServletPath()
可能是获取实际请求资源的更好方法。它以与上下文无关的方式执行此操作,因此您无需摆弄getRequestURI()
来修剪上下文路径。另外,如果有一个 servlet 被映射到例如/foo/*
,那么这只会返回/foo
而不是/foo/bar/baz< /代码>。以防万一你有兴趣。
由于您使用的是 Tomcat,您可能对其内置 Access 感兴趣Log Valve 为您提供了一种与众所周知的 Apache HTTPD 或多或少相同的方式记录请求的方法,包括时间。当然,只有当您对 Tomcat 实例拥有完全管理控制权时,这才有意义。
Not without changing the servlets/JSPs so that they set a specific request attribute which you in turn get after the
FilterChain#doFilter()
call.I can only give the hint that
HttpServletRequest#getServletPath()
is probably a nicer way to obtain the actually requested resource. It does that in a context-independent way so that you don't need to fiddle withgetRequestURI()
to trim the context path off. Also, if there's a servlet which is mapped on for example/foo/*
, then this would only return/foo
instead of/foo/bar/baz
. Just in case you're interested.Since you're using Tomcat, you may be interested in its builtin Access Log Valve which offers you a way to log requests less or more the same way as the well known Apache HTTPD, including the times. This is of course only of interest if you have full admin control over the Tomcat instance.