Alexander Marcussen
Alexander Marcussen

Reputation: 155

Issue with sendRedirect()

Im creating a web interface were is the user can type in som things in a formular: But when i push the ok button i get this error code:

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

My code:

<%!
String validate (String input) {
        String error = "";
        int receptnr = 0;
        String error1 = "";
        int varenr = 0;
        try {
                    receptnr = Integer.parseInt(input);
                    varenr= Integer.parseInt(input);

        } catch (Exception e) {
                    error = error +"Receptnummeret skal være et tal";
                    error1 = error1 +"Varenummer skal være et tal";
        }
        if (receptnr > 99999999||receptnr<1||varenr > 99999999||varenr<1)
                        error = error + " Receptnummeret skal være mellem 1 og 99999999 år";
                        error1 = error1 + " Varenummeret skal være mellem 1 og 99999999 år";
        return error;


}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<title>Indtastningsformular</title>
<body>
<%
        if (request.getMethod().equals("POST")) { // brugeren har tastet på submit

                    String receptnr = request.getParameter("receptnr");
                    String error = validate(receptnr);
                    String varenr = request.getParameter("varenr");
                    String error1 = validate(varenr);

                    if (error.equals(""))
                    {
                                    System.out.print(receptnr);
                                    response.sendRedirect("VisOKData.jsp?receptnr_resultat="+receptnr);


                    }
                    else

                                    response.sendRedirect("VisError.jsp?error_resultat="+error);
                /*  
                    if(error1.equals("")){
                        System.out.print(varenr);
                        response.sendRedirect("VisOKData.jsp?varenr_resultat="+varenr);

                    }
                    else
                        response.sendRedirect("VisError.jsp?error_resultat="+error);
                 */                 
        }
%>
        <form method="POST" action="InputForm.jsp">
            Receptnr:
                        <input type="text" name="receptnr" value="Indtast dit receptnr her">
            <br />          
            Varenr:
                        <input type="text" name="varenr" value="Indtast dit varenr her">
                        <br />
                                                        <input type="submit" value="OK">
        </form>

What im trying to do is to give the formular some conditions and then redirect the page to an error message or an ok message

Upvotes: 0

Views: 635

Answers (1)

Mitul Maheshwari
Mitul Maheshwari

Reputation: 2647

It's Simple because you ar redirecting the page twice.

if your first (if) condition true or false, the page is already redirected to other page.

after that how it will redirect again.

so solution is that convert two condition's into one. like that :-

 if (error.equals(""))
{
    System.out.print(receptnr);
   if(error1.equals(""))
     {
        System.out.print(varenr);
        response.sendRedirect("VisOKData.jsp?varenr_resultat="+varenr);
     }
   else
    {
       response.sendRedirect("VisError.jsp?error_resultat="+error);
    }
}
else
 {
     response.sendRedirect("VisError.jsp?error_resultat="+error);
 }

Upvotes: 3

Related Questions