我正在使用jsp从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}" />


请提出建议,如何摆脱这种情况。

最佳答案

从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}" />

10-08 09:08
查看更多