所以我使用的是注释(而不是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>
答案 0 :(得分:0)
删除开头的正斜杠:
RequestDispatcher rd=request.getRequestDispatcher("WEB-INF/Welcome.jsp");
或试试这个:
RequestDispatcher rd=request.getRequestDispatcher("../WEB-INF/Welcome.jsp");
或首先尝试获取上下文:
RequestDispatcher rd=getServletContext().getRequestDispatcher( "WEB-INF/Welcome.jsp" );