0

这是我登录的Servlet POST方法404找不到JSP

/** 
* @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的这个ADDRES存在,这不仅登录后工作。用户的if条件是好的(我通过将其设置为false来检查它)。

为什么会发生?

回答

2

/WEB-INF文件夹中的资源不能公开访问(否则最终用户将能够通过直接打开它来查看敏感信息,如web.xml中的数据源用户名/密码)。

您需要将公用accessibe JSP文件放在/WEB-INF文件夹之外。

LOGGED_IN_URL = "/index.jsp"; 

和重定向为

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

资源/WEB-INF如下仅适用于正向和包括。

+0

今天早上没有更多的咖啡在家里? :)在位于/ WEB-INF的资源上使用'sendRedirect()'不会有效,有或没有/。 – Med

+0

@Med:哎呀,我以某种方式解释他是转发,答案是固定的。 – BalusC

+1

像往常一样真的很快。谢谢! – Med