File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes include doesn't recognize relative path from sub folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "include doesn Watch "include doesn New topic
Author

include doesn't recognize relative path from sub folder

Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Tomcat 5.5.25

I have an /includes folder on document root level. All JSPs have
this include <%@ include file="/includes/header.jsp" %>

It works fine for jsps on the top level, like /index.jsp but it doesn't work for any jsps that are in sub folders, like /article/index.jsp

I get file not found error
org.apache.jasper.JasperException: /article/index.jsp(8,0) File "/includes/header.jsp" not found

Btw, if I do<%@ include file="../includes/header.jsp" %> I get
File "/../includes/header.jsp" not found

Any idea why?

Thanks!

My configs can be found here:
http://www.coderanch.com/forums/f-56/Tomcat

[ February 14, 2008: Message edited by: Axel Brown ]

[ February 15, 2008: Message edited by: Axel Brown ]

[ February 17, 2008: Message edited by: Bear Bibeault ]
[ February 17, 2008: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I have an /includes folder on document root level.


What, exactly, do you mean when you say "document root level"?
Where is this directory?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Oh, by that I mean appBase path. Includes don't work from folders for either my domain or sub domains or addon domains...

In case of main domain: my "root" is /home/myuser/public_html
In case of addon or subdomain: /home/myuser/public_html/myaddondomain

Even if I include full path inside include, either

or

I get File not found error with "/" in front!

File "/http://www.outervision.com/includes/header.jsp" not found or
File "/home/outerv2/public_html/includes/header.jsp" not found

server.xml part:


[ February 15, 2008: Message edited by: Axel Brown ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The "/" in the include path means the root of this web application (context).
Each <Context .../> entry in your server.xml file is a separate web application.

Are you trying to include files across contexts?
If so, why?

If the applications are identical, why not just use host aliases to point all requests to the same application?
If they are different, I would avoid trying to share resources between them.
Tools like Ant make it very easy to replicate files at build time so each of your applications can complete and self contained.
[ February 16, 2008: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

By the way:
You don't need to bribe people here.
Well formed questions tend to get plenty of attention and yours seems fine.
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
I was desperate... Still, problem not resolved and I moved to old server

No, I'm not trying to include files across contexts.
Applications are different but use the same DB.

Very strange problem. Tomcat treats every sub-folder as it has its own context path or something... As soon as I copy includes folder inside sub-folder, it works.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Axel Brown:

No, I'm not trying to include files across contexts.
Applications are different but use the same DB.


Context and application are synonymous.
As far as includes are concerned, it doesn't matter that they're using the same database.

If you're trying to include a file from a different application you're going to run into trouble.

When you said you moved it to the old server, does that mean that this is working in a different container?
[ February 17, 2008: Message edited by: Ben Souther ]
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Originally posted by Ben Souther:


Context and application are synonymous.
As far as includes are concerned, it doesn't matter that they're using the same database.

If you're trying to include a file from a different application you're going to run into trouble.

When you said you moved it to the old server, does that mean that this is working in a different container?

[ February 17, 2008: Message edited by: Ben Souther ]


Includes fail within the same application when called from sub-folders.
Old server (shared hosting) has similar configuration except Tomcat is I believe 5.5.12 (or 19).
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It really sounds to me that you're trying to include files from other web applications (contexts) which doesn't work.

I think your best bet is to look at the two options mentioned earlier.
1.) Have only one application with aliases to handle addition domains.
2.) Duplicate the files at build time with a tool such as Ant instead of trying to share them.
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Originally posted by Ben Souther:
It really sounds to me that you're trying to include files from other web applications (contexts) which doesn't work.

I think your best bet is to look at the two options mentioned earlier.
1.) Have only one application with aliases to handle addition domains.
2.) Duplicate the files at build time with a tool such as Ant instead of trying to share them.

No sharing, no other applications. Let's forget about multiple contexts for a minute.
I can remove all of them and leave only main domain in server.xml - and still, includes from sub-folders fail.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

And the sub folders are inside your web app?

The root of a web app is the directory that holds the WEB-INF directory.
Where are your include files in relation to that?
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Originally posted by Ben Souther:
And the sub folders are inside your web app?

The root of a web app is the directory that holds the WEB-INF directory.
Where are your include files in relation to that?


Yes, they are inside. Rigt, WEB-INF is in root.
The structure of root (public_html) like this:

articles
includes
WEB-INF
index.jsp

articles folder contains its own jsps.
includes folder contains header.jsp (our shared include between all jsps)

index.jsp from root uses /includes/header.jsp and other jsps from articles/ suppose to use the same /includes/header.jsp - but it fails for them (not found), while works for index.jsp from root.
[ February 17, 2008: Message edited by: Axel Brown ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

3 Ideas:

1.) Have you checked your log files for JSP compilation errors?

2.) Can you reproduce this in a small webapp? If so, war it up and
email it to me. I'll try it in another machine.

3.) I just created a small (working) example.
If you like, you can grab the war file here.
Try downloading and running that to see if it works.
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Originally posted by Ben Souther:
3 Ideas:

1.) Have you checked your log files for JSP compilation errors?

2.) Can you reproduce this in a small webapp? If so, war it up and
email it to me. I'll try it in another machine.

3.) I just created a small (working) example.
If you like, you can grab the war file here.
Try downloading and running that to see if it works.


Yeah, I can reproduce it with small webapp. I just deployed your war (thanks btw), which works fine on the root level with index.jsp

As soon as I created folder /articles and copied your index.jsp to it and tried to access like this: www.example.com/articles/index.jsp - include doesn't work.

This is what I get in browser:



and this is from Tomcat's log:
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Also, I see this in tomcat's log after its restart:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

That looks as if the manager and admin applications were removed from the hard drive but not from the config. Why would you have removed these apps? They're really useful. Especially the manager app which could be used in this case to see what contexts are deployed. For example, do you accidentally have an includes context deployed?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

P.S. I removed the "reward" offer. That's rather inappropriate and I don't want any precedents set.
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
Originally posted by Bear Bibeault:
That looks as if the manager and admin applications were removed from the hard drive but not from the config. Why would you have removed these apps? They're really useful. Especially the manager app which could be used in this case to see what contexts are deployed. For example, do you accidentally have an includes context deployed?


I have no idea why an how they were removed... As long as they are not the cause of my issue.

So what about my previous post?

"Yeah, I can reproduce it with small webapp. I just deployed your war (thanks btw), which works fine on the root level with index.jsp

As soon as I created folder /articles and copied your index.jsp to it and tried to access like this: www.example.com/articles/index.jsp - include doesn't work."
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I just added an 'articles' directory to the example app that I sent to you and it worked.

Can you email the small app to me?
ben@javaranch.com

Also, create a small JSP with this in it:
<%=application.getRealPath("/")%>

And see what that prints.
Put in the root directory and in the articles directory.
See if they both print the same thing.
[ February 17, 2008: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm wondering if nesting one host inside another's directory structure like this is causing your problem.

To simplify things, try running with just one host until you get things working.


A Aflatooni
Greenhorn

Joined: May 03, 2006
Posts: 8
I have the same exact problem too. What is the solution? I have found that it is not due to the catalina.policy and web.xml settings.
Older versions of tomcat works, but 5.5.26 says that the file isn't found. It doesn't like any includes that gives relative path like "../test.jsp".
I am having this problem of FreeBSD 6.3.

Thanks
A Aflatooni
Greenhorn

Joined: May 03, 2006
Posts: 8
Just another note:
I managed to duplicate this under windows as well.
It doesn't occur if your host and context are under webapps, but if it is pointing to a directory outside of Tomcat then you can include files from the same directory or sub-directories. But you will get an error is you want to include a file from a higher directory "../xxx".

Previous versions didn't have this problem.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

A Aflatooni,
Please ask you question in a separate thread.
If you think your issue is related to this one, feel free to link to it from yours. Adding your issue to the end of this one (even if similar) is called thread hi-jacking and is very frustrating to both the original poster and the people trying to help with the original question.
Axel Brown
Greenhorn

Joined: Feb 12, 2008
Posts: 25
The solution, which kind of goes against what worked in older versions of Tomcat (I believe 5.5.12), doesn't work for 5.5.25 and 5.5.26 anymore. At least in my case.

So, Context path="" or Context path="/" is no go. I figured a way to make it work by creating a folder ROOT in main domain and each sub-domains and updating Context path as in example below.

So, the folder structure will be like this:

public_html/ROOT
public_html/ROOT/sub-dom1/ROOT
public_html/ROOT/sub-dom2/ROOT
public_html/ROOT/addondomain1/ROOT
...

And all your content should be placed in ROOT of each domain, addon doamin or sub-domain respectively.

Your Host/Context will have to be updated to (note that appBase and docBase stay the same as before):



Also, in Apache's httpd.conf you have to update DocumentRoot to if you want PHP to be working. But this is only if you used JkMount /*
DocumentRoot /home/myuser/public_html/myaddondomain/ROOT

So, it works fine now. Any comments on possible security issues with this structure?
[ February 27, 2008: Message edited by: Axel Brown ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: include doesn't recognize relative path from sub folder