一尘不染

在servlet中包含jsp的内容

jsp

我有这个servlet:

public class SaveImage extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.println("<html>");
            ...

            // I want to include here the content of this jsp:
            // /WEB-INF/mybox.jsp
            // (also, with the full context of the servlet)

            ...
            out.println("</html>");
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这样做有问题吗(响应已经提交?),我该怎么做?


阅读 294

收藏
2020-06-08

共1个答案

一尘不染

request.getRequestDispatcher("/WEB-INF/my.jsp").include(request, response);

但是您不应该使用servlet来输出html。只需使用jsp,<jsp:include />或者<%@ include file=".." %>

2020-06-08