将jsp的内容包含在servlet中

发布于 2024-10-17 08:11:32 字数 716 浏览 1 评论 0原文

我有这个 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();
        }
    }
}

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2024-10-24 08:11:32
request.getRequestDispatcher("/WEB-INF/my.jsp").include(request, response);

但你不应该使用这样的 servlet 来输出 html。只需使用 jsp,使用 <%@ include file=".." %>

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=".." %>

并安 2024-10-24 08:11:32

谢谢奥佐,你帮助我让这个已有 2 年历史的待定项目最终成型。谢谢。
实际上,要将tomcat的请求从sun web服务器7重定向到应用程序服务器,由于jsps不直接显示在tomcat中,技术是在app.config中使用passthrough并让tomcat处理请求。

            import java.io.IOException;

            import javax.servlet.RequestDispatcher;
            import javax.servlet.ServletContext;
            import javax.servlet.ServletException;
            import javax.servlet.http.HttpServlet;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;

            /**
             * Servlet implementation class MY... Parvez Ahmad Hakim
             */
            public class MY extends HttpServlet {
                private static final long serialVersionUID = 1L;

                /**
                 * @see HttpServlet#HttpServlet()
                 */
                public MY() {
                super();
                // TODO Auto-generated constructor stub
                }

                /**
                 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
                 */
                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    


                    String pageName =request.getParameter("req");       
                    if(pageName==null){ 
                        pageName="IC_LIC_Login.jsp";// default page
                    }
                    request.getRequestDispatcher(pageName).include(request, response);



                }

                /**
                 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
                 */

                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
                    String pageName =request.getParameter("req");       

                    request.getRequestDispatcher(pageName).include(request, response);
                }


            }

THANKS ozho , YOU HAVE HELPED ME TO give final shape to 2 year old pending project. Thanks.
Actually to redirect the request of tomcat from sun web server 7 to application server, since the jsps are not shown in tomcat directly, technique is to use a passthrough in app.config and let the tomcat handle the requests.

            import java.io.IOException;

            import javax.servlet.RequestDispatcher;
            import javax.servlet.ServletContext;
            import javax.servlet.ServletException;
            import javax.servlet.http.HttpServlet;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;

            /**
             * Servlet implementation class MY... Parvez Ahmad Hakim
             */
            public class MY extends HttpServlet {
                private static final long serialVersionUID = 1L;

                /**
                 * @see HttpServlet#HttpServlet()
                 */
                public MY() {
                super();
                // TODO Auto-generated constructor stub
                }

                /**
                 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
                 */
                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    


                    String pageName =request.getParameter("req");       
                    if(pageName==null){ 
                        pageName="IC_LIC_Login.jsp";// default page
                    }
                    request.getRequestDispatcher(pageName).include(request, response);



                }

                /**
                 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
                 */

                protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
                    String pageName =request.getParameter("req");       

                    request.getRequestDispatcher(pageName).include(request, response);
                }


            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文