开发者

Include JSP file with Java

开发者 https://www.devze.com 2023-02-24 09:49 出处:网络
I know that including and external file in jsp can be done with something like this: <%@ include file=\"banner.jsp\" %>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号