Java Servlet Filter重定向问题 [英] Java Servlet Filter redirect problem

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

问题描述

我的身份验证过滤器出现问题。当筛选器重定向到登录页面时,登录JSP中不会显示任何图像。但是,如果我在登录后手动进入登录页面,则会显示图像。

I'm having a problem with my authentication filter. When the filter redirects to the login page, no images gets displayed in the login JSP. However, if I go to the login page manually after I'm logged in, the images are displayed.

我不明白为什么会这样!我感谢任何帮助。 : - )

I don't understand why this is happening! I appreciate any help. :-)

AuthFilter:

AuthFilter:

if (authorized == null && path.indexOf("Auth") == -1 && path.indexOf("Login") == -1 ) {
        httpResponse.sendRedirect("Login");  
        return;  
} else {  
        chain.doFilter(request, response);  
}

登录servlet:

// Just using a servlet in case I want more data sent to the jsp
Dispatcher.dispatch("views/login.jsp", request, response);

login.jsp:

login.jsp:

<img src="images/logo.png" />

jsp是正常,所有必需的HTML标签都存在。 images文件夹位于项目的默认web文件夹中,与所有其他jsp和javascripts一起。

The jsp is otherwise "normal", all required HTML tags are present. The "images" folder is in the default "web" folder of the project, alongside all the other jsp's and javascripts.

提前感谢您的帮助。 :)

- Stian

Thanks in advance for any help. :)
- Stian

推荐答案

这是因为相对路径。


  • 您的登录位于上下文的根目录

  • 您的图片可能是 / views / images /

  • 转发时,浏览器只知道请求的网址。

  • your Login is in the root of the context
  • your images probably are /views/images/
  • when you forward, the browser knows only the requested URL.

所以当你转发时,图像是在 / images 上寻找的(因为它们是相对于当前地址而不是 / views / images /

So when you forward, the images are sought at /images (because they are relative to the current address) instead of /views/images/

如何解决它。两个选项:

How to resolve it. Two options:


  • 不要从你的servlet转发;重定向

  • 不要从过滤器重定向到servlet;直接重定向到登录页面

更新:
确保图像不受过滤器的影响。两个选项:

Update: Make sure the images are NOT affected by the filter. two options:


  • 它们不应与过滤模式匹配

  • 重定向不应该发生过滤器中的.png,.jpeg,.css等。用 check.getRequestURI()

  • they should not be matched by the filter pattern
  • redirection should not happen for .png, .jpeg, .css, etc. in the filter. check this with request.getRequestURI()

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

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