Servlet 无法访问 WEB-INF 中的 .jsp 文件

Servlet can't access .jsp file inside WEB-INF

所以我正在使用注释(不是 web.xml)。我的 servlet 看起来像这样(非常简单,如果用户在表单中键入 FooUser 和 FooLast,它会显示 Welcome.jsp

否则重定向到同一页面 Login.jsp。

@WebServlet("/ServletLogin")
public class ServletLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getParameter("Username").equals("FooUser") && request.getParameter("Lastname").equals("FooLast")) {
            RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/Welcome.jsp");
            rd.forward(request,response);
        }
        else {
            RequestDispatcher rd=request.getRequestDispatcher("Login.jsp");  
            rd.forward(request,response);
        }
    }
}

我的Login.jsp看起来像这样

<!DOCTYPE html>
<html lang="en">
<body class="login">
    <div>
        <form action="ServletLogin" method="post">
            <h1>Login Form</h1>
            <div>
                <input type="text" class="form-control" placeholder="Username"
                    name="Username" required="" />
            </div>
            <div>
                <input type="password" class="form-control" placeholder="Password"
                    name="Password" required="" />
            </div>
            <div>
                <input class="btn btn-info" type="submit" value="Log in" />
            </div>
        </form>
    </div>
    </div>
    </div>
</body>
</html>

删除开头的正斜杠:

 RequestDispatcher rd=request.getRequestDispatcher("WEB-INF/Welcome.jsp");

或者试试这个:

RequestDispatcher rd=request.getRequestDispatcher("../WEB-INF/Welcome.jsp");

或先尝试获取上下文:

RequestDispatcher rd=getServletContext().getRequestDispatcher( "WEB-INF/Welcome.jsp" );