简体   繁体   中英

response.sendRedirect is not working

I have two jsps. One is login_first.jsp , another one is main.jsp . After submit in login_first.jsp , I call main.jsp . It works fine.

I have logout button in main.jsp which sends control back to login_first.jsp . it executes login_first.jsp but page is not loading. Please help.

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>

First off, both pages have 'session=false' set but you're trying to get/set attributes in Session. Are you sure it's working at all as intended?

Second, you can only call response.sendRedirect() before any data was sent back to the client (before buffer flushed). Are you sure it's happening in your case?

Although scriptlets are outdated, I believe you should check against the documentation on where your JSP files are located and if you need to use a leading / on your redirect URLs; eg /first_login.jsp instead of first_login.jsp .

I just found that the target="formresponse" in main.jsp gives problem. So I have moved this logout to a separate form and added action.

But the same sendRedirect is working from first_login.jsp. From main.jsp to login_first.jsp is not working.

One reason could be that your main.jsp is in some subfolder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM