0

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");
                 }
jmj
  • 237,923
  • 42
  • 401
  • 438
Naresh
  • 245
  • 1
  • 7
  • 18

3 Answers3

3

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Sorry i my project we not using servlets presentation and bussiness.. only for ajax. – Naresh Dec 09 '10 at 12:16
  • 2
    JSP is the wrong place for business. JSP is for presentation only. Servlet is for business. Report it to your manager. If he disagree, then you have two options: 1) Bad luck, live with it, do it all in a single large JSP with lot of conditional includes instead. 2) Look for a better job where there are no stupid restrictions like that. – BalusC Dec 09 '10 at 12:19
  • very simple and helpful explanation +1 – jmj Dec 09 '10 at 12:28
  • Sorry..!u miss understood my comment. we r developing in the bussiness in seperate layer total the project bulid by spring hibernate with Desing pattern............! – Naresh Dec 09 '10 at 13:01
  • 1
    I think you misunderstood how a well designed JSP/Servlet/Spring/Hibernate web application look like. – BalusC Dec 09 '10 at 13:26
0

use else if in place of if

  • their was no chance to else because before that some Service method was executing it was returning String value(which was storing on msg variable) depending on that message only i have to show the next page........... Thanks for u reply...! can u send any thing else – Naresh Dec 09 '10 at 12:12
0

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>