Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix Cannot call sendRedirect() after the response has been committed? [duplicate]

Tags:

I am trying to pass values from servlet to jsp page using the code below:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      PrintWriter out = response.getWriter();     try {         System.out.println("try");          String taskid=request.getParameter("id");            convty = new Connectivity();         con = convty.setConnection();         st=con.createStatement();          query="select * from task_table";          rset=convty.getResultSet(query, con);         while(rset.next()) {             System.out.println("while");             lst.add(rset.getString("task_id"));             lst.add(rset.getString("date"));             lst.add(rset.getString("project_name"));             }         rset.close();         System.out.println("after while");      } catch(Exception e) {          RequestDispatcher dd= request.getRequestDispatcher("error.jsp");         dd.forward(request, response);      } finally {         System.out.println("finally1");         HttpSession sessionvar = request.getSession();         sessionvar.setAttribute("TaskData", "lst");          response.sendRedirect("usertaskpage.jsp");          lst.clear();         out.close();                 } } 

when I run my page I am getting:

Error :

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:

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

but I got the same error.

How to fix this error?

like image 734
MMMMS Avatar asked Oct 14 '14 04:10

MMMMS


People also ask

Can you call sendRedirect after response is committed?

you can't call sendRedirect(), after you have already used forward().

What is the use of sendRedirect () method?

sendRedirect() method redirects the response to another resource, inside or outside the server. It makes the client/browser to create a new request to get to the resource. It sends a temporary redirect response to the client using the specified redirect location URL.

What is the difference between sendRedirect () and forward ()?

SendRediret() When we use the forward method request is transferred to other resources within the same server for further processing. In case of sendRedirect request is transferred to another resource to a different domain or different server for further processing.

How do you pass variables in response sendRedirect?

If you forward the request, you can use setAttribute to put your variable in the request scope, which can easily be retrieved in your JSP. request. setAttribute( "temp" , yourVar); RequestDispatcher dispatcher = request.


1 Answers

you have already forwarded the response in catch block:

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

so, you can not again call the :

response.sendRedirect("usertaskpage.jsp"); 

because it is already forwarded (committed).

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

    String page = "";     try {      } catch (Exception e) {       page = "error.jsp";     } finally {       page = "usertaskpage.jsp";     }  RequestDispatcher dd=request.getRequestDispatcher(page); dd.forward(request, response); 
like image 68
Nomesh DeSilva Avatar answered Sep 21 '22 01:09

Nomesh DeSilva