response.sendRedirect不起作用

时间:2012-04-04 11:48:33

标签: java jsp response

我有两个jsps。一个是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>

main.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>

4 个答案:

答案 0 :(得分:0)

首先,两个页面都设置了'session = false',但是你试图在Session中获取/设置属性。你确定它按预期工作吗?

其次,在将任何数据发送回客户端之前(缓冲区刷新之前),您只能调用response.sendRedirect()。你确定它正在发生在你的情况下吗?

答案 1 :(得分:0)

虽然scriptlet已过时,但我相信您应该检查JSP文件所在位置的documentation,以及是否需要在重定向网址上使用前导/;例如/first_login.jsp代替first_login.jsp

答案 2 :(得分:0)

我刚发现main.jsp中的target="formresponse"存在问题。所以我已将此注销移动到单独的表单并添加了操作。

答案 3 :(得分:0)

  

但是相同的sendRedirect正在使用first_login.jsp。从   main.jsp到login_first.jsp无效。

一个原因可能是你的main.jsp在某个子文件夹中。