【问题标题】:404 not found jsp404 找不到jsp
【发布时间】:2012-12-25 22:43:24
【问题描述】:

这是我的登录 Servlet 的 post 方法

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String login = request.getParameter("login").trim();
    String password = request.getParameter("password");

    User user = getUsersDao().login(login, DigestUtils.shaHex(password));

    if (user == null) {
        request.setAttribute("login", login);
        request.setAttribute("error", "Wrong username or password.");
        forward(request, response, LOGIN_JSP);
    } else {
        request.getSession().setAttribute(USER_SESSION, user);
        response.sendRedirect(LOGGED_IN_URL);
    }
}

LOGGED_IN_URL is "WEB-INF/jsp/index.jsp";
并且 index.jsp 存在于这个地址上,这仅在登录后才起作用。用户的 if 条件是好的(我通过将其设置为 false 来检查它)。

为什么会这样?

【问题讨论】:

    标签: jsp servlets redirect http-status-code-404 response.redirect


    【解决方案1】:

    /WEB-INF 文件夹中的资源不可公开访问(否则最终用户只需直接打开即可看到web.xml 中的数据源用户名/密码等敏感信息)。

    您需要将可公开访问的 JSP 文件放在 /WEB-INF 文件夹之外。

    LOGGED_IN_URL = "/index.jsp";
    

    并重定向如下

    response.sendRedirect(request.getContextPath() + LOGGED_IN_URL);
    

    /WEB-INF 中的资源仅可用于转发和包含。

    【讨论】:

    • 今天早上家里没有咖啡了? :) 在 /WEB-INF 定位的资源上使用 sendRedirect() 是行不通的,不管有没有 /。
    • @Med:哎呀,我以某种方式解释他在转发,答案是固定的。
    • 真的很快,像往常一样。谢谢!
    猜你喜欢
    • 2015-05-30
    • 1970-01-01
    • 2020-03-08
    • 2015-10-18
    • 2020-09-21
    • 1970-01-01
    • 2019-07-30
    • 2018-07-04
    • 2015-05-09
    相关资源
    最近更新 更多