3

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?

jmj
  • 237,923
  • 42
  • 401
  • 438
Devin Dixon
  • 11,553
  • 24
  • 86
  • 167
  • 1
    You want to include jsp in java clsas ????????????? – jmj Apr 11 '11 at 17:12
  • @Jigar Joshi: including a jsp view from a Servlet is a perfectly reasonable thing to do. – Asaph Apr 11 '11 at 17:13
  • @Jigar: Ordinarily I would mock you for those question marks, but in this case I feel you were justified :) – josh.trow Apr 11 '11 at 17:14
  • 2
    Note that the `@include` is not able to include external files. The file has got to be in the same webcontent in order for it to work. – BalusC Apr 11 '11 at 17:14
  • 2
    To further @BalusC's comment, `` from the JSTL *is* able to include external files, so you can use that instead. – Asaph Apr 11 '11 at 17:15
  • what exactly is your ultimate goal with this? – Bozho Apr 11 '11 at 17:17
  • Like @BalusC's said is probably enough for what you seem to need if you want more info about it check this page (or by a man and google it...) http://publib.boulder.ibm.com/infocenter/wchelp/v5r6/index.jsp?topic=/com.ibm.commerce.developer.doc/refs/rsdjspbpinclude.htm – Chris Apr 11 '11 at 21:13

3 Answers3

5

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    That's correct, but note that this is not the best practice. Inclucing other servlet in a servlet makes sense, but including a JSP in a servlet makes little sense. You'd rather forward the request/response to a JSP which in turn includes other JSP. – BalusC Apr 11 '11 at 17:13
  • @BalusC - I just added that note. – Bozho Apr 11 '11 at 17:14
  • @BalusC and @Bozho: I have found that when I use `forward()` instead of `include()`, the resultant jsp page loses the context of the client's original request and if I try to use the request object within the jsp page to construct absolute urls, I get the jsp page's url instead of what the client requested. I ran into this issue a while back while building a mail-this-link-to-a-friend feature. – Asaph Apr 11 '11 at 17:20
  • @Asaph: There are other ways for that. – BalusC Apr 11 '11 at 17:24
  • @BalusC: What is the preferred way to accomplish that? – Asaph Apr 11 '11 at 17:25
0

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.

Community
  • 1
  • 1
Edoardo
  • 4,485
  • 1
  • 27
  • 31
0

In Servlet you can call:

RequestDispatcher rd = request.getRequestDispatcher("include.jsp");
rd.include(request, response); 
lukastymo
  • 26,145
  • 14
  • 53
  • 66