Include JSP file with Java
I know that including and external file in jsp can be done with something like this:
<%@ include file="banner.jsp" %>
But is there a way of doing this inside a java class/开发者_JS百科object?
You can do it inside a servlet (or any class having access to the current request), via the RequestDispatcher
:
request.getRequestDispatcher("/banner.jsp").include(request, response);
Note that you should rarely need to do this. It would mean that you are outputting view content from a servlet, and you should do that mainly in a jsp.
In Servlet you can call:
RequestDispatcher rd = request.getRequestDispatcher("include.jsp");
rd.include(request, response);
There is NO way to do:
<%@ include file="banner.jsp" %>
in java, because - as you can read here that is a static jsp include, which is done at JSP compile time, I wish there was such a thing as static code includes in java.
精彩评论