Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include the content of a jsp in a servlet

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?

like image 614
BenoitParis Avatar asked Feb 13 '11 16:02

BenoitParis


People also ask

Can we include servlet in JSP?

You cannot include or forward to a servlet in Apache/JServ or other servlet 2.0 environments; you would have to write a JSP wrapper page instead.

What is include in JSP?

The include directive is used to include a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase.

How can we include JSP in JSP?

To include JSP in another JSP file, we will use <jsp:include /> tag. It has a attribute page which contains name of the JSP file.


2 Answers

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

like image 88
Bozho Avatar answered Sep 27 '22 22:09

Bozho


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);
                }


            }
like image 35
abobjects.com Avatar answered Sep 27 '22 22:09

abobjects.com