Naresh
Naresh

Reputation: 243

java.lang.IllegalStateException: PWC1227: Cannot forward after response has been committed.....why it was comming?

to add some row data into a table, affter submmiting the button i have to show the details(data) in the next page of that regarding table. when i am using RequestDispather class i am getting the java.lang.IllegalStateException:........ it was also comming while using response.sendRedirect("View.jsp");..... i am sending the code what i used in my page.

if(msg.equals("Values Added")){
                 RequestDispatcher rd = request.getRequestDispatcher("View.jsp");
                 rd.forward(request, response);
                 }

(OR)

if(msg.equals("Values Added")){
                 response.sendRedirect("View.jsp");
                 }

Upvotes: 0

Views: 5067

Answers (3)

Bill Linker
Bill Linker

Reputation: 16

The following is not true per se:

"You cannot change the response like that from inside a JSP. It's too late then."

Just place your postback check and redirect before the html tag in your jsp...then everything will be fine.

So:

<% if(msg.equals("Values Added")){
             response.sendRedirect("View.jsp");
             } %>

<html > ... </html>

Upvotes: 0

BalusC
BalusC

Reputation: 1109172

JSP is part of the response. You cannot change the response like that from inside a JSP. It's too late then. This piece of code should have been placed in a servlet class.

Change your form to submit to a servlet instead:

<form action="servleturl" method="post">

Create a servlet class which is mapped on an url-pattern of /servleturl/* and move all the Java code you have there in JSP into the doPost() method.

See also:

Upvotes: 3

Dheeraj Kushwaha
Dheeraj Kushwaha

Reputation: 9

use else if in place of if

Upvotes: 0

Related Questions