[英]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.