Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 579373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:21:37+00:00 2026-05-13T14:21:37+00:00

This method throws java.lang.IllegalStateException: Cannot forward after response has been committed and I am

  • 0

This method throws

java.lang.IllegalStateException: Cannot forward after response has been committed

and I am unable to spot the problem. Any help?

    int noOfRows = Integer.parseInt(request.getParameter("noOfRows"));
    String chkboxVal = "";
    // String FormatId=null;
    Vector vRow = new Vector();
    Vector vRow1 = new Vector();
    String GroupId = "";
    String GroupDesc = "";
    for (int i = 0; i < noOfRows; i++) {
        if ((request.getParameter("chk_select" + i)) == null) {
            chkboxVal = "notticked";
        } else {
            chkboxVal = request.getParameter("chk_select" + i);
            if (chkboxVal.equals("ticked")) {
                fwdurl = "true";
                Statement st1 = con.createStatement();
                GroupId = request.getParameter("GroupId" + i);
                GroupDesc = request.getParameter("GroupDesc" + i);
                ResultSet rs1 = st1
                        .executeQuery("select FileId,Description from cs2k_Files "
                                + " where FileId like 'M%' and co_code = "
                                + ccode);
                ResultSetMetaData rsm = rs1.getMetaData();
                int cCount = rsm.getColumnCount();

                while (rs1.next()) {
                    Vector vCol1 = new Vector();
                    for (int j = 1; j <= cCount; j++) {
                        vCol1.addElement(rs1.getObject(j));
                    }
                    vRow.addElement(vCol1);
                }
                rs1 = st1
                        .executeQuery("select FileId,NotAllowed from cs2kGroupSub "
                                + " where FileId like 'M%' and GroupId = '"
                                + GroupId + "'" + " and co_code = " + ccode);
                rsm = rs1.getMetaData();
                cCount = rsm.getColumnCount();

                while (rs1.next()) {
                    Vector vCol2 = new Vector();
                    for (int j = 1; j <= cCount; j++) {
                        vCol2.addElement(rs1.getObject(j));
                    }
                    vRow1.addElement(vCol2);
                }

                // throw new Exception("test");

                break;
            }
        }
    }
    if (fwdurl.equals("true")) {
        // throw new Exception("test");
        // response.sendRedirect("cs2k_GroupCopiedUpdt.jsp") ;
        request.setAttribute("GroupId", GroupId);
        request.setAttribute("GroupDesc", GroupDesc);
        request.setAttribute("vRow", vRow);
        request.setAttribute("vRow1", vRow1);
        getServletConfig().getServletContext().getRequestDispatcher(
                "/GroupCopiedUpdt.jsp").forward(request, response);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:21:37+00:00Added an answer on May 13, 2026 at 2:21 pm

    forward/sendRedirect/sendError do NOT exit the method!

    A common misunderstanding among starters is that they think that the call of a forward(), sendRedirect(), or sendError() method would magically exit and "jump" out of the method block, hereby ignoring the remnant of the code. For example in a servlet:

    protected void doXxx(...) {
        if (someCondition) {
            response.sendRedirect(...);
        }
    
        dispatcher.forward(...); // This is STILL invoked when someCondition is true!
    }
    

    Or in a filter:

    public void doFilter(...) {
        if (someCondition) {
            response.sendRedirect(...);
        }
    
        chain.doFilter(...); // This is STILL invoked when someCondition is true!
    }
    

    This is thus actually not true. They do certainly not behave differently than any other Java methods (expect of System#exit() of course). When the someCondition in above example is true and you’re thus calling forward() or doFilter() after sendRedirect() or sendError() on the same request/response, then the chance is big that you will get the exception:

    java.lang.IllegalStateException: Cannot forward after response has been committed

    This also applies to the inverse condition. If the if statement calls a forward() and you’re afterwards calling sendRedirect() or sendError(), then below exception can be thrown:

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

    To fix this, you need either to add a return; statement afterwards

    protected void doXxx(...) {
        if (someCondition) {
            response.sendRedirect(...);
            return;
        }
    
        dispatcher.forward(...);
    }
    

    … or to introduce an else block.

    protected void doXxx(...) {
        if (someCondition) {
            response.sendRedirect(....);
        }
        else {
            dispatcher.forward(...);
        }
    }
    

    To naildown the root cause in your code, just search for any line which calls a forward(), sendRedirect() or sendError() without exiting the method block or skipping the remnant of the code. This can be inside the same servlet before the particular code line, but also in any servlet or filter which was been called before the particular servlet.

    In case of sendError(), if your sole purpose is to set the response status, use setStatus() instead.

    Do not write any string before forward/sendRedirect/sendError

    Another probable cause is that the servlet writes to the response while a forward() will be called, or has been called in the very same method.

    protected void doXxx() {
        out.write("<p>some html</p>");
    
        // ...
    
        dispatcher.forward(); // Fail!
    }
    

    The response buffer size defaults in most server to 2KB, so if you write more than 2KB to it, then it will be committed and forward() will fail the same way:

    java.lang.IllegalStateException: Cannot forward after response has been committed

    Solution is obvious, just don’t write to the response in the servlet. That’s the responsibility of the JSP. You just set a request attribute like so request.setAttribute("data", "some string") and then print it in JSP like so ${data}. See also our Servlets wiki page to learn how to use Servlets the right way.

    Do not write any file before forward/sendRedirect/sendError

    Another probable cause is that the servlet writes a file download to the response after which e.g. a forward() is called.

    protected void doXxx() {
        out.write(bytes);
    
        // ...
     
        dispatcher.forward(); // Fail!
    }
    

    This is technically not possible. You need to remove the forward() call. The enduser will stay on the currently opened page. If you actually intend to change the page after a file download, then you need to move the file download logic to page load of the target page. Basically: first create a temporary file on disk using the way mentioned in this answer How to save generated file temporarily in servlet based web application, then send a redirect with the file name/identifier as request param, and in the target page conditionally print based on the presence of that request param a <script>window.location='...';</script> which immediately downloads the temporary file via one of the ways mentioned in this answer Simplest way to serve static data from outside the application server in a Java web application.

    Do not call forward/sendRedirect/sendError in JSP

    Yet another probable cause is that the forward(), sendRedirect() or sendError() methods are invoked via Java code embedded in a JSP file in form of old fashioned way <% scriptlets %>, a practice which was officially discouraged since 2003. For example:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            ... 
        </head>
        <body>
            ...
            <% response.sendRedirect(...); %>
            ...
        </body>
    </html>
    

    The problem here is that JSP internally immediately writes template text (i.e. HTML code) via out.write("<!DOCTYPE html> ... etc ...") as soon as it’s encountered. This is thus essentially the same problem as explained in previous section.

    Solution is obvious, just don’t write Java code in a JSP file. That’s the responsibility of a normal Java class such as a Servlet or a Filter. See also our Servlets wiki page to learn how to use Servlets the right way.

    See also:

    • What exactly does "Response already committed" mean? How to handle exceptions then?

    Unrelated to your concrete problem, your JDBC code is leaking resources. Fix that as well. For hints, see also How often should Connection, Statement and ResultSet be closed in JDBC?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unrolled Linked List In computer programming, an unrolled linked list… May 13, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer The usual way to avoid cycles is to use weak… May 13, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer Does it have to be plain text or can you… May 13, 2026 at 10:45 pm

Related Questions

Is it possible to create a web service operation using primitive or basic Java
My build process builds and copies a .war file to $TOMCAT_HOME/webapps. 75% or more
I am using Java NIO for my socket connections, and my protocol is text
Is there a way to read a ByteBuffer with a BufferedReader without having to
I'm solving Sphere's Online Judge Prime Generator using the Sieve of Eratosthenes. My code

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.