0

我在 java web 中遇到 AccessFilter 的问题。当我调用 /main.jspx 时,它会重定向到 login.jsp。但是当我尝试登录时出现了一些错误

public class AccessFilter implements Filter {

    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = ((HttpServletRequest) request).getSession();
        HttpServletResponse res = (HttpServletResponse) response;
        Client client = (Client) session.getAttribute("client");        
        if (client != null) {
            chain.doFilter(request, response);
        } else {
            RequestDispatcher dispatcher = request.getRequestDispatcher(
                    ConfigurationManager.getInstance().getProperty(ConfigurationManager.LOGIN_PAGE_PATH));
            dispatcher.forward(request, response);
        }
    }

    @Override
    public void destroy() {
        this.filterConfig = null;
    }

}

网页.xml:

<filter>
    <filter-name>AccessFilter</filter-name>
    <filter-class>ua.kpi.shop.filter.AccessFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AccessFilter</filter-name>
    <url-pattern>/jsp/main.jspx</url-pattern>
    <url-pattern>/jsp/pokemons.jspx</url-pattern>
</filter-mapping>

错误:HTTP 状态 404 - /PokemonsShop/login.jspx

类型状态报告

消息 /PokemonsShop/login.jspx

描述 请求的资源不可用。

4

2 回答 2

0

看到您的消息时,我想到了两件事:1)您是否检查了客户端对象是否为空?可能是在执行登录操作(方法)时,您没有将客户端正确设置为会话?2)在服务器错误中,它显示“未找到/PokemonsShop/login.jspx”,但在您的过滤器映射中您提到了/jsp/xxx。会不会是因为您的登录页面位于 jsp 文件夹下,并且您正在(在过滤器中)重定向到 /PokemonsShop/login.jspx,它应该位于 webapp 根文件夹下才能访问。希望其中一位能有所帮助

于 2012-12-17T13:12:46.773 回答
0

filterConfig.getServletContext().getRequestDispatcher采用绝对路径而不是request-getRequestDispatcher. 虽然这是否是我不能说的解决方案。

于 2012-12-16T23:08:58.790 回答