AabinGunz
AabinGunz

Reputation: 12347

JSP redirects to another page even before button is clicked

I have a JSP Page which I am using to redirect to another url (cross domain), so using jQuery I placed a button click event under which response.sendRedirect should trigger, but as soon as the page loads it redirects it does not even wait for alert. What is the problem here? Please help.

on body load it calls load()

<script language="JavaScript">
    function load(){
        <%
        String message=(String)request.getAttribute("error");
        if(message != null){
            out.println("alert(\"" + message + "\");");
        }
        %>

    }

    /*a range of options (products) based on which it decides redirection or form submission
    * will lead to the new webapp*/
    var productArray=["iirDefault","ilmDefault","mdmDefault"];
    $(document).ready(function(){       

    });

    /*Button event handler for the intermediate screen for product selection after login*/
    $('#btnLaunchCSM').click(function(event){
        event.preventDefault();

        var selectedProduct=$('input[name=product_name]:checked').val();
        if(jQuery.inArray(selectedProduct, productArray)!=-1)
        {
            e_add=$("#emailaddress").val();
            p_id=$('input[name=project_id]:checked').val();
            alert(e_add+", "+p_id);
            <% 
            String e_add=request.getParameter("email_id");
            String p_id=request.getParameter("project_id");
            response.sendRedirect("http://abhishek:9090/abc/view/loginProxy.jsp?email_id="+e_add+"&project_id="+p_id); 
            %>


        }
        else
        {
            $("#launchSSO").submit();
        }

    });
</script>

Upvotes: 0

Views: 1810

Answers (1)

BalusC
BalusC

Reputation: 1109222

You seem to expect that JSP scriptlet and JavaScript runs in sync as you have coded it. This is not true. Java/JSP runs in webserver and produces HTML. HTML/JS runs in webbrowser. Rightclick page in browser and View Source. If Java/JSP has done its job right, you should not see any single line of it in the generated HTML/JS output.

You need to send a (ajax) HTTP request to your server instead. You can use jQuery for this or just a plain form submit. Or you can just perform the redirect fully in JS. Replace that smelly <% %> block by:

window.location = "http://abhishek:9090/abc/view/loginProxy.jsp?email_id="+e_add+"&project_id="+p_id; 

Upvotes: 2

Related Questions