0

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

请建议,如何摆脱这个。

4

1 回答 1

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}" />
于 2012-08-20T06:30:54.043 回答