基于浏览器用户代理重定向的正确方法?
我正在构建一个基于 Spring MVC 的应用程序,我想根据用户的浏览器将用户重定向到网站的特定部分。
我使用应用于 /site/home.jsp 的过滤器来读取用户代理以确定浏览器类型。
HttpServletRequest req = (HttpServletRequest) request;
String uaString = req.getHeader("User-Agent");
此外,我想重定向用户如下:
- Firefox:重定向到 /site/firefox/home.jsp
- IE:重定向到 /site/ie/home.jsp
- 未知:重定向到 /site/UnsupportedBrowser.jsp
我的困惑是什么是正确的如何从我的 BrowserDetector 过滤器重定向用户?
1)简单地重定向用户?
resp.sendRedirect("/AppName/site/ie/home.jsp");
2) 使用 HTTP 临时重定向?
resp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
resp.setHeader("Location", "/AppName/site/ie/home.jsp");
3)服务器端重定向?
RequestDispatcher request_Dispatcher=request.getRequestDispatcher("/ie/home.jsp");
request_Dispatcher.forward(request,response);
4)还有其他正确的方法吗?
I am building a Spring MVC based application where I want to redirect user's to specific portion of the site based on their browser.
I am using a Filter applied to /site/home.jsp to read the User-Agent to determine the browser type.
HttpServletRequest req = (HttpServletRequest) request;
String uaString = req.getHeader("User-Agent");
Further I want to redirect users as below:
- Firefox: redirect to /site/firefox/home.jsp
- IE: redirect to /site/ie/home.jsp
- Unknown: redirect to /site/UnsupportedBrowser.jsp
My confusion is what is the correct way to redirect the user from my BrowserDetector filter?
1) Simply redirect the user?
resp.sendRedirect("/AppName/site/ie/home.jsp");
2) Use an HTTP Temp redirect?
resp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
resp.setHeader("Location", "/AppName/site/ie/home.jsp");
3) Server side redirect?
RequestDispatcher request_Dispatcher=request.getRequestDispatcher("/ie/home.jsp");
request_Dispatcher.forward(request,response);
4) Any other correct way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第三个想法可能是一个坏主意,因为它很可能会导致您必须对每个请求执行此检查,这是低效的(只是轻微的,但小的低效率可能会增加)。
想法的第 1 号和第 1 号2 也不是最好的方法,因为两者都会导致临时重定向 (307) 响应,而您可能想要的是永久重定向 (301)。这是因为有问题的浏览器始终是相同的 - FF 和 IE 从不共享永久移动列表,因此即使两个浏览器由同一客户端计算机使用,也不会导致问题。您应该使用 301 再次考虑到效率 - 如果浏览器总是直接转到正确的位置,那么您的服务器要做的工作就会减少。
总而言之,我认为想法 2 是最接近的,但您应该使用它:
请记住,用户代理字符串可能会被欺骗,并且不能 100% 依赖。
这是我个人的看法,YMMV...
Idea number 3 is probably a bad idea, as it will most likely result in you having to perform this check for every request, which is inefficient (only slightly, but small inefficiencies can mount up).
Idea's number 1 & 2 are also not the best approach, because both will result in a Temporary redirect (307) response, wheras what you probably want is a permanent redirect (301). This is because the browser in question will always be the same - FF and IE never share there list of permanent moves, so even if both browsers are used by the same client machine this will not cause a problem. You should use 301 for reasons of, again, efficiency - if the browser always goes directly to the correct place, it is less work for your server to do.
To sum up, I think idea 2 is the closest, but you should use this instead:
Keep in mind that User-Agent strings can be spoofed and cannot be 100% relied upon.
This is my personal opinion, YMMV...
首先也是最重要的一点:不要进行服务器端浏览器嗅探!
由于一堆不同的原因,服务器端浏览器嗅探是一个坏主意,但这只是其中的几个:
更好的方法是使用渐进增强或优雅降级。
话虽这么说,一种更可靠的选择是在浏览器中进行嗅探,然后相应地将它们重定向到客户端。 Modernizr 将告诉您他们的浏览器具有哪些功能,然后您可以决定为他们提供哪些服务。
First and foremost: DON'T DO SERVER-SIDE BROWSER SNIFFING!
Server-side browser sniffing is a bad idea for a bunch of different reasons, but here's just a few of them:
A much better approach is to use Progressive Enhancement or Graceful Degradation.
That being said, one option that works more reliably is to do the sniffing in the browser and then redirect them client-side accordingly. Modernizr will tell you what capabilities their browser has, and from there you can decide what to serve them.