ServletRequest 获取实际页面的名称

发布于 2024-11-07 09:15:22 字数 203 浏览 0 评论 0原文

我正在使用 FilterdoFilter-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 技术交流群。

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

发布评论

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

评论(2

酒绊 2024-11-14 09:15:22

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 to HttpServletRequest first)

凉宸 2024-11-14 09:15:22

当我刚开始学习 Java EE 时,很容易混淆 ServletRequestHttpServletRequest 方法。正如 Bozho 所说,类型转换对于访问所需的方法是必要的。

当请求名为“Test”的servlet时,此代码将记录访问时间IP

if (((HttpServletRequest)request).getServletPath().equals("/Test")){
            String IP = request.getRemoteAddr();
            System.out.println("Test Servlet:: Logged IP "+ IP + ", Time :" + new Date().toString());
        }
filterChain.doFilter(request,response);

基本上,请求 对象从通用请求转换为 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:

if (((HttpServletRequest)request).getServletPath().equals("/Test")){
            String IP = request.getRemoteAddr();
            System.out.println("Test Servlet:: Logged IP "+ IP + ", Time :" + new Date().toString());
        }
filterChain.doFilter(request,response);

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").

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