• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Including files from different directories

 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, do not hard-code the contextPath.
Use the getContextPath method of HttpServletRequest


or for pre 2.0 JSP engines
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the JSTL <c:url> tag to help you with constructing URLs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic