【问题标题】:Cannot forward.Response already committed. Error无法转发。响应已提交。错误
【发布时间】:2012-08-20 05:42:38
【问题描述】:

我正在使用 jsp 调用 servlet

//My servlet code is:
public void doGet(HttpServletRequest request, HttpServletResponse response)
       {
           String template="test";   
           abcViewBean punchOutCan = new abcViewBean();
           punchOutCan.setPunchOutCanonicalRes(template);
           try {
            request.getRequestDispatcher("/PunchOutCanonicalError.jsp").forward(request,response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       }

我的 JSP 代码是:

<jsp:include page="/PunchOutCanonicalServlet" flush="true"/>  
<c:out value="${punchOutCan.punchOutCanonicalRes}" />

请建议,如何摆脱这个。

【问题讨论】:

    标签: java jsp servlets


    【解决方案1】:

    从 servlet 的 doGet 中排除(删除)此语句,因为您要在 JSP 中导入响应。

    request.getRequestDispatcher("/PunchOutCanonicalError.jsp")
        .forward(request,response);
    

    doGet 必须是:

    @Override
    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response)
                        throws ServletException,IOException{
           String template="test";   
           abcViewBean punchOutCan = new abcViewBean();
           punchOutCan.setPunchOutCanonicalRes(template);
           //You can push the bean object into request via setAttribute
           //e.g
           //request.setAttribute("punchOutCan",punchOutCan);
    }
    

    还有JSP代码,

    <jsp:include page="/PunchOutCanonicalServlet" flush="true"/>  
    <c:out value="${punchOutCan.punchOutCanonicalRes}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-08
      • 2023-04-07
      • 1970-01-01
      • 2016-09-11
      • 2012-08-25
      • 2013-08-15
      • 2013-06-25
      • 1970-01-01
      相关资源
      最近更新 更多