GeeCON Prague 2014*
The moose likes JSP and the fly likes Including files from different directories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Including files from different directories" Watch "Including files from different directories" New topic
Author

Including files from different directories

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hi,

From my jsp pages I include a Header file.

From that Header file I include different pages (menues, images) depending on which jsp pages I request.

My problem is that if the requested jsp pages is placed in diffent directories the Header file can�t find the different pages to be included?

Lets say I request /root/canteen/info/index.jsp

- The include page path in the Header file have to look like this:

<jsp:include page="../../myfiles/test1.jsp"/>

Otherwise if the I request /root/canteen/info/admin/index.jsp

- The include page path in the Header file have to look like this:

<jsp:include page="../../../myfiles/test1.jsp"/>

How can I come around this path problem if I want to use the same Header file?
[ October 17, 2005: Message edited by: Jeppe Fjord ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61306
    
  66

Using page-relative URLs is a really bad idea. It creates artificial coupling between your pages and makes it difficult to refactor the structure of your site.

Rather, use complete URLs that reference relative to the context root. For example:



The above assumes that the "/root" in your example URL is a folder in the web app and not the context path (if it is the context path, it would be omitted).

Also note that included JSP fragments are conventionally given the file type of .jspf to distinguish them from full JSP files.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270

Rather, use complete URLs that reference relative to the context root. For example:

<%@ include file="/root/canteen/common.jspf" %>

The above assumes that the "/root" in your example URL is a folder in the web app and not the context path (if it is the context path, it would be omitted).

Okay the above works everytime I make an include file. But if I have a href link or want to show an image in my common.jspf, then it doesn�t work if I use complete URLs with a reference relative to the context root?

- i.e.:
<img src="/bitmaps/logo/companylogo.gif"> Does not work!

<img src="../../bitmaps/logo/companylogo.gif"> That works!

Where the dir looks like:

Why can�t I use use complete URLs with a reference relative to the context root when having a href link or an image?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61306
    
  66

References from image, script and link tags need to be server-relative. In other words, they must include the context path.

Remember, referenes in include directives and the like are made from within the web application on the server side of things. However, img tags (and the like) make references from the client browser. In such external references, the context path must be included.
[ October 17, 2005: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

But, do not hard-code the contextPath.
Use the getContextPath method of HttpServletRequest


or for pre 2.0 JSP engines


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Thank you Ben.

The question below is answered...


References from image, script and link tags need to be server-relative. In other words, they must include the context path.

Then what do I do if I one day change the context name or need to make a copy of the application and give the context a different name for each new copy? Then I still have the problem about maintaining the code? The same problem that if I use relative URL in the include file tag?
[ October 17, 2005: Message edited by: Jeppe Fjord ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61306
    
  66

Then what do I do if I one day change the context name


No one said anything about hard-coding the context path. In fact, that would be a poor thing to do for the very reasons that you've outlined.

Under JSP 2.0 you can obtain the context path with



Under JSP 1.2:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61306
    
  66

You can also use the JSTL <c:url> tag to help you with constructing URLs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Including files from different directories