HttpServletRequest - 如何获取引用 URL? [英] HttpServletRequest - how to obtain the referring URL?

查看:28
本文介绍了HttpServletRequest - 如何获取引用 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Java Servlet 中记录链接到我的站点的 URL.

解决方案

可在 HTTP referer 标头.您可以通过以下方式在 servlet 中获取它:

String referrer = request.getHeader("referer");//是的,传说中的拼写错误.

但是,您需要意识到这是一个客户端控制的值,因此可以被欺骗为完全不同的东西,甚至被删除.因此,无论它返回什么值,您都不应将其用于后端的任何关键业务流程,而应仅用于展示控制(例如隐藏/显示/更改某些纯布局部分)和/或统计信息.

如果有兴趣,可以在 维基百科中找到有关拼写错误的背景.>

I need to log URLs that are linking to my site in a Java Servlet.

解决方案

It's available in the HTTP referer header. You can get it in a servlet as follows:

String referrer = request.getHeader("referer"); // Yes, with the legendary misspelling.

You, however, need to realize that this is a client-controlled value and can thus be spoofed to something entirely different or even removed. Thus, whatever value it returns, you should not use it for any critical business processes in the backend, but only for presentation control (e.g. hiding/showing/changing certain pure layout parts) and/or statistics.

For the interested, background about the misspelling can be found in Wikipedia.

这篇关于HttpServletRequest - 如何获取引用 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆