在 servlet 中包含 jsp 的内容

Include the content of a jsp in a servlet(在 servlet 中包含 jsp 的内容)

本文介绍了在 servlet 中包含 jsp 的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have this 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();
        }
    }
}

Is there a problem doing it (response already committed?), how can I do this?

解决方案

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

But you should not a servlet for outputting html like that. Just use a jsp, with either <jsp:include /> or <%@ include file=".." %>

这篇关于在 servlet 中包含 jsp 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 servlet 中包含 jsp 的内容

基础教程推荐