因此,每当用户提供无效的用户名/密码时,我都试图让我的Servlet重定向到我的/login.jsp。但是,当我这样做时,它会重定向到错误的网址,并给出未找到的404错误。它应该重定向到:http://localhost:8030/KwetterApp/login.jsp,但是重定向到:http://localhost:8030/login。
@WebServlet(name = "LoginServlet", urlPatterns = { "/login" })
public class LoginServlet extends HttpServlet {
UserService service = new UserService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String username = request.getParameter("Username");
String password = request.getParameter("Password");
User user = service.checkLogin(username, password);
if (user != null) {
session.setAttribute("userList", service.getAllUsers());
session.setAttribute("user", user);
if (user.getRole().equals(Role.ADMIN)) {
response.sendRedirect("manage.jsp");
}
else {
//ToDo
}
}
else {
response.sendRedirect("login.jsp");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}