从 Servlet/Filter 重定向不起作用 [英] Redirection from Servlet/Filter does not work

查看:92
本文介绍了从 Servlet/Filter 重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有重定向问题 - 它根本不适用于有效路径.现在我在 Servlet 中使用页面转发,但我需要在过滤器中重定向.

I have a problem with redirection - it simply does not work for valid paths. Right now I use page forwarding in the Servlet, but I need redirection in a filter.

所有页面都位于pages"文件夹中,扩展名为 .jspx

All the pages reside in the 'pages' folder and have a .jspx extension

我尝试了以下方法(此路径适用于转发):

I've tried the following (this path works with forwarding):

httpResponse.sendRedirect("/pages/login.jspx");

浏览器 url 是 http://[localhost]/pages/login.jspx,它显示了 Tomcat 的 404 页面,上下文路径(在我的情况下是/hotel")从网址,所以,如果我添加它:

browser url is http://[localhost]/pages/login.jspx, and it shows Tomcat's 404 page, the context path (in my case it's '/hotel') is missing from the url, so, if I add it:

httpResponse.sendRedirect("/hotel/pages/login.jspx");

重定向没有发生,浏览器url没有改变,我看到了浏览器的404页面(这个程序不能显示网页).

redirect does not happen, browser url does not change, and I'm shown the browser's 404 page (This program cannot display the webpage).

我做错了什么?

用于测试的过滤器具有以下映射:

The filter which is used to test this has the following mapping:

@WebFilter(filterName = "SecurityFilter", urlPatterns = "/*")

推荐答案

重定向的 URL 确实与最初请求的 URL 相关.要动态添加上下文路径,建议使用 HttpServletRequest#getContextPath() 而不是对其进行硬编码,因为上下文路径值可以通过特定于服务器的配置在外部进行更改.

The redirected URL is indeed relative to the initially requested URL. To dynamically prepend the context path it's recommended to use HttpServletRequest#getContextPath() instead of hardcoding it, because the context path value can be changed externally by server-specific configuration.

至于你的具体问题,我不确定我是否正确理解浏览器的 404 页面",也许你的意思是浏览器默认错误页面,当服务器无法访问或请求被重定向到无限循环(不过应该在浏览器默认错误页面的实际消息中明确说明,至少 Chrome 和 Firefox 是这样做的).

As to your concrete problem, I'm not sure if I understand "browser's 404 page" properly, perhaps you mean the browser-default error page which can occur when the server is unreachable or when the request has been redirected in an infinite loop (that should however have been made clear in the actual message of the browser default error page, at least Chrome and Firefox do that).

鉴于您的过滤器映射到 /*,它实际上是在无限循环中重定向,因为登录页面的请求 URL 反过来也匹配过滤器的 URL 模式.

Given that your filter is mapped on /*, it's actually redirecting in an infinite loop because the request URL of the login page in turn also matches the URL pattern of the filter.

您需要要么将过滤器放在覆盖登录页面的更具体的 URL 模式上,例如在 /secured/* 上,所有受限页面都被移到那里(或将其映射到 /pages/* 并将登录页面放在那里), 修复您的过滤器,如下所示:

You'd need either to put the filter on a more specific URL pattern which does not cover the login page, e.g. on /secured/* where all restricted pages are been moved in there (or map it on /pages/* and put the login page outside there), or to fix your filter as follows:

String loginURL = request.getContextPath() + "/pages/login.jspx";

if (needsToRedirect && !request.getRequestURI().equals(loginURL)) {
    response.sendRedirect(loginURL);
}
else {
    chain.doFilter(request, response);
}

这篇关于从 Servlet/Filter 重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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