[英]response.sendRedirect is not working
我有兩個jsp。 一個是login_first.jsp
,另一個是main.jsp
。 在login_first.jsp
提交后,我調用main.jsp
。 它工作正常。
我在main.jsp
中有注銷按鈕,它將控制發送回login_first.jsp
。 它執行login_first.jsp
但頁面未加載。 請幫忙。
login_first.jsp
<%@ page session="false" %>
<%
try {
HttpSession session = request.getSession(true);
if ("Submit".equals(request.getParameter("SubmitButton"))) {
session.setAttribute("userLoggedIn", "true");
response.sendRedirect("main.jsp");
return;
} else {
session.setAttribute("userLoggedIn", "false");
session.invalidate();
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body>
<form name="loginForm" method="post">
<table>
<tr>
<td><input type="submit" name="SubmitButton" value="Submit" class=button/></td>
</tr>
</table>
</form>
</body>
<%
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("login_first.jsp");
return;
}
%>
</html>
主要.jsp
<%@ page session="false" %>
<%
try {
HttpSession session = request.getSession(false);
if (session != null && "true".equals(session.getAttribute("userLoggedIn"))
&& !"Logout".equalsIgnoreCase(request.getParameter("logout"))) {
// do work
} else {
if (session != null) {
session.setAttribute("userLoggedIn", "false");
}
response.sendRedirect("login_first.jsp");
return;
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body>
<form name="creditCardForm" target="formresponse" autocomplete="off" method="post">
<table width="50%" border=0 cellpadding=3 cellspacing=1>
<tr>
<td>
<div align="right">
<input name="logout" type="submit" class=button value="Logout">
</div>
</td>
</tr>
</table>
<iframe name="formresponse" width="0" height="0" style="visibility:hidden"></iframe>
</form>
</body>
<%
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("login_first.jsp");
return;
}
%>
</html>
首先,兩個頁面都設置了“session=false”,但您正在嘗試獲取/設置 Session 中的屬性。您確定它完全按預期工作嗎?
其次,您只能在將任何數據發送回客戶端之前(在刷新緩沖區之前)調用 response.sendRedirect() 。 你確定你的情況正在發生嗎?
盡管 scriptlet 已過時,但我相信您應該檢查有關 JSP 文件所在位置以及是否需要在重定向 URL 上使用前導/
的文檔; 例如/first_login.jsp
而不是first_login.jsp
。
我剛剛發現 main.jsp 中的target="formresponse"
出現問題。 所以我已將此注銷移至單獨的表單並添加了操作。
但是相同的 sendRedirect 從 first_login.jsp 開始工作。 從 main.jsp 到 login_first.jsp 不工作。
一個原因可能是您的 main.jsp 在某個子文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.