• Register
0 votes
1.8k views

Problem :

I am trying to pass the values from the servlet to jsp page

But when I run the page I am facing below error:

Error as below :

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
    org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:483)
    src.Taskservlet.doPost(Taskservlet.java:108)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

I tried to use the below code as well:

RequestDispatcher dd=request.getRequestDispatcher("usertaskpage.jsp");

dd.forward(request, response);

But still I got a same error.

How can I fix above error?

7 5 2
3,870 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

It seems you have already forwarded a response in catch block with below code:

RequestDispatcher dd = request.getRequestDispatcher("error.jsp");
dd.forward(request, response);

So,now you must not again call the :

response.sendRedirect("usertaskpage.jsp");

As it is already forwarded (committed).

So what you can really do is: keep the string to assign where you need to forward a response.

    String page = "";
    try {
 
    } catch (Exception e) {
      page = "error.jsp";
    } finally {
      page = "usertaskpage.jsp";
    }
RequestDispatcher dd=request.getRequestDispatcher(page);
dd.forward(request, response);

 OR

Cause of IllegalStateException exception is the java servlet is attempting to write to a response after a response has been committed. It is always better to ensure that the no content is added to a response after a forward or the redirect is done to avoid the IllegalStateException. It can be done by including the ‘return’ statement immediately next to a forward or redirect statement.

9 7 4
38,600 points

Related questions

0 votes
1 answer 511 views
511 views
Problem : I got following exception from my servlet: java.lang.illegalstateexception: response has already been committed
asked Oct 31, 2019 peterlaw 6.9k points
0 votes
1 answer 4 views
4 views
Problem: Is anyone available to assist me and resolve this issue? java.lang.illegalstateexception: cannot call senderror() after the response has been committed
asked 5 days ago Ifra 24.4k points
0 votes
1 answer 20 views
0 votes
1 answer 69 views
69 views
Problem : I am trying to run Java Applet and getting bellow error. java.lang.illegalstateexception: failed to create a child event loop
asked Oct 31, 2019 peterlaw 6.9k points
0 votes
1 answer 631 views
631 views
Problem : I am just a beginner in java coding. Recently i got this error and was unable to solve it. Please help me with this. exception in thread "main" java.lang.illegalstateexception: scanner closed
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 16 views
16 views
How is this problem caused and how can I solve it? I am using Mojarra 2.1.2.
asked Jan 24 TeamScript 13.5k points
0 votes
1 answer 16 views
16 views
Problem: How do I import the javax.servlet API in my Eclipse project?
asked Mar 15 ummesalma 24.5k points
0 votes
1 answer 14 views
14 views
Does anybody know *why* that includes has to be used?
asked Jan 20 TeamScript 13.5k points
0 votes
1 answer 36 views
36 views
I am unable to spot the problem. Any help?
asked Jan 1 TeamScript 13.5k points
0 votes
1 answer 1 view
1 view
Problem: I am currently implementing a forgot password function in a Java project. I tried this in my code but on my reset password page, I didn't get the email id of the user who wants to change the password. Can you pleas help me the solve this problem or get any other option to do this.
asked Mar 24 ummesalma 24.5k points