我有一個servlet來處理/Login
。使用Servlet登錄後重定向到index.jsp
String tempUsername = request.getParameter("username").trim();
String tempPassword = request.getParameter("password").trim();
String message = null;
if(tempUsername != null && tempPassword != null) {
String username = tempUsername;
String password = tempPassword;
Staff staff = new Staff();
try {
Facade facade = new Facade();
Staff result = facade.getStaff(username, password);
if(result != null) {
// response.sendRedirect(request.getContextPath()+"/Home"); // Success but didn't redirect to `/Home`
System.out.println("Success");
}
else {
// request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
System.out.println("Fail");
}
}
catch(SQLException | NamingException e) {
// SQL Error
}
finally {
request.setAttribute("message", message);
}
}
else {
// Empty field(s)
}
我還有一個servlet來處理/Home
如果登錄成功,其中index.jsp
將被顯示。
但是,目前如果登錄成功,頁面仍然停留在只有空白頁的/Login
,而不是重定向到/Home
。
登錄成功後,如何重定向到/Home
?
我是使用Servlet和Eclipse的新手。我剛開始學習J2EE和Eclipse。仍在探索。
- 編輯(S):
- 我改變了
forward()
和sendRedirect()
到println()
。當我使用錯誤的用戶名和密碼時,打印出「失敗」。但是當我使用正確的細節時,沒有打印出來。
我是使用Servlet的新手。以前,我正在將這些請求指向'jsp',而不是'Servlet'。我不知道爲什麼頁面不顯示,即使我有'index.jsp'。我嘗試過使用'forward()'和'sendRedirect()',但兩者都指向空白頁,使用相同的目錄'/ Login'而不是'/ Home'。 – Chin 2014-08-31 05:37:12
@Chin'forward()'不會更改url(但您可以嗅探瀏覽器並查看正在進行的調用)。空白頁面可能是'/ home'上發生的某種錯誤 - 但由於您沒有發佈該頁面的代碼,因此很難說出可能導致該行爲的原因。 – alfasin 2014-08-31 07:10:16