<jsp:include problem>

Discussions

Web tier: servlets, JSP, Web frameworks: <jsp:include problem>

  1. <jsp:include problem> (3 messages)

    Hi people.


    I had a problem with a <jsp:include> in my jsp.

    I wrote a servlet to control the process in my application. My servlet throws to a page called main.jsp. In this page, a put a <jsp:include> directive. If the contents of the directive is a static page, i.e. "index.html" , the program runs successfully, but, if I put a call to another servlet (i.e. which generate a news to the client) , I receive this exception message:

    java.lang.IllegalStateException: Response has already been committed

    If I call the page main.jsp direct into my browser, the include directive works fine and call my servlet.

    Here is a piece of my control servlet:
    ...

          jsp="main.jsp";
          RequestDispatcher dispatcher;
          dispatcher= context.getRequestDispatcher(jsp);
          dispatcher.forward(req,res);
      }
    }
      
    And here is my main.jsp

    ------main.jsp
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ page import="com.nexion.voyager.components.usuarioBean" %>



    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
      <HEAD>
      <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
      <LINK rel="stylesheet" type="text/css" href="css/webiz.css">
        <TITLE>
          WeBiz
        </TITLE>
        
      </HEAD>
      <BODY topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
    ...
      <jsp:include page="/busca_cliente" flush="true"></jsp:include>

           </TD>

          </TR>
          <TR>
            <TD height="5%" valign="TOP" class="tabela_header">
              
            </TD>
          </TR>
        </TABLE>
        
      </BODY>
    </HTML>

    Threaded Messages (3)

  2. <jsp:include problem>[ Go to top ]

    try
    dispatcher.include(req,res);
    instead
    Usha
  3. <jsp:include problem>[ Go to top ]

    I already change the code but I had the same problem.
  4. <jsp:include problem>[ Go to top ]

    one common cause of IllegalStateException in the response is when you recursively include or forward.

    your control servlet forwards/includes the request to main.jsp

    however, in main.jsp you are including the response of the control servlet.

    so if someone accesses main.jsp, and no exception was caught, then you'd see the response of:

    main.jsp including servlet forwarding to main.jsp including servlet forwarding main.jsp etc. BAD!

    or

    main.jsp inluding servlet including main.jsp including servlet including main.jsp etc. BAD!

    If you would like to access the request object in a JSP from a forwarded page, you simple use the implicit HttpServletRequest object.

    So in the JSP you can say:

    // if in servlet before forward:
    // request.setAttribute("msg", "something stored in HttpServletRequest");
    String s = (String)request.getAttribute("msg");

    or

    // if a parameter was passed through the servlet
    request.getParameter("fromServlet");