ServletRequest 获取实际页面的名称
我正在使用 Filter
和 doFilter
-Function。我想知道正在请求哪个网站。有些函数给我类似 /firstDirectoryInWebDirectory/lala.jsp
的功能将是完美的。
到这里怎么走?使用上下文将是正确的方法,但我找不到任何好的方法:-(
谢谢
I am using a Filter
and the doFilter
-Function. I would like to know which site is being requested. Some function giving me something like /firstDirectoryInWebDirectory/lala.jsp
would be perfect.
What is the way to go here? Using the context will be the right way, but I can't find any good method :-(
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
request.getRequestURI()
应返回 URL 中域名后面的部分。从那里,您可以剥离
request.getContextPath()
(您必须首先将
ServletRequest
转换为HttpServletRequest
)request.getRequestURI()
should return the part of the URL after the domain.From there, you can strip the
request.getContextPath()
(You'd have to cast the
ServletRequest
toHttpServletRequest
first)当我刚开始学习 Java EE 时,很容易混淆 ServletRequest 和 HttpServletRequest 方法。正如 Bozho 所说,类型转换对于访问所需的方法是必要的。
当请求名为“Test”的servlet时,此代码将记录访问时间和IP:
基本上,请求 对象从通用请求转换为 HTTP 请求,然后才能将 getServletPath() 的返回字符串与您想要的任何内容进行比较(“/Test”或“/firstDirectoryInWebDirectory/lala.jsp”或“whatever.html”)。
When I just started learning Java EE, it was casual to mix-up ServletRequest and HttpServletRequest methods. As Bozho said, type casting is necessary to access required methods.
This code will log access time and IP when the servlet named "Test" will be requested:
Basically, the request object is casted from a generic request into HTTP Request and only then the return String of getServletPath() can be compared to whatever you want ("/Test" or "/firstDirectoryInWebDirectory/lala.jsp" or "whatever.html").