简体   繁体   中英

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/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.

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.

In Servlet you can call:

RequestDispatcher rd = request.getRequestDispatcher("include.jsp");
rd.include(request, response); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM