"<%@ include file" broblem - Tomcat

girionis
girionis used Ask the Experts™
on
 Right, we are migrating our web content from Resin to Tomcat web server.

  All of our JSP pages use include directives for common files (like headers, footers, front pages etc). All of the files reside under the "wtgContent" folder.

  In Resin I could map a folder to a URL, for example the c:/wtgContent to /wtgContent and use it like:

<%@ include file = "/wtgContent/CityGuide/English/Common/JavaFrontPage.inc"%>

  Unfortunatelly I am unable to do something similar with Tomcat.

1) I have used the <Context> tag (in the server.xml file but with no luck.
2) I have put the whole "wtgContent" folder under Tomcat's "webapps" folder but with no luck.
3) From the JSP reference I read that the include directive is always relative to URL but there must be a solution since Resin does it.
4) I have tried everything I could thing of, it still does not work.

  All I want to do is to map an absolute path (e.g. "c:\content\wtgContent" to a relative path (e.g. "/wtgContent" so whenever the

<%@ include file = "/wtgContent/CityGuide/English/Common/JavaFrontPage.inc"%>

  is encountered Tomcat will actually look within the:

c:\content\wtgContent\CityGuide\English\Common\JavaFrontPage.inc

  I believe it is something very simple but I am stuck at the moment.

  Any help is much appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018

Author

Commented:
 Sorry, forgot to mention. We are using Tomcat 4.1 under Windows NT with JDK1.3
You should be able to set up a context with an absolute path as well.  I use the following to include the manager application in a number of different virtual hosts using :-

<Context path="/manager" docBase="c:\Apache Group\Tomcat 4.1\server\webapps\manager" debug="0" privileged="true">

Have you tried using :-

<Context path="/wtgContent" docBase="c:\content\wtgContent" debug="0">

I think this should work.  Once you've added this and restarted Tomcat if your page still doesn't work can you try accessing "http://localhost:8080/wtgContent/CityGuide/English/Common/JavaFrontPage.inc" directly.  Do you see your code ?  If you do then the context is working properly but you can't access it from your other context. One thing you may need to do to get this working is to use the crossContext attribute of the Context element.  This is what the documentation says about this attribute

"Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null."

By creating a separate context you are in effect creating a different webapp so you may need to set this attribute to make it work ie <Context path="/wtgContent" docBase="c:\content\wtgContent" debug="0" crossContext="true">

If all of this fails try changing the debug level of the context to 99 and restarting tomcat.  Then try accessing your document again and post the contents of the log file.  It may shed some light on what's going wrong.

Hope this helps.
Distinguished Expert 2018

Author

Commented:
 garethwebbley thank you for your hints but none of them did the truck. I am still getting the

"org.apache.jasper.JasperException: /City.jsp(102,2) File "wtgContent/CityGuide/English/Common/JavaFrontPage.inc" not found"

  error message.

  I have tried all tyour suggestions. In my server.xml file I have the following line:

"<Context path="/wtgContent" docBase="c:\Tomcat4.1\webapps\wtgContent" debug="99" crossContext="true"/>"

  I have played around with the Context and tried several changes but none of them worked. When I call the file from the browser doing:

"http://127.0.0.1:8080/wtgContent/CityGuide/English/Common/JavaFrontPage.inc"

  I can see the front page properly.

  The line I am calling the file within the City.jsp file is the following:

"<%@ include file = "wtgContent/CityGuide/English/Common/JavaFrontPage.inc"%>"

  The only possible solution I found so far is to put the entirely wtgContent folder under each webapps, which is not the best solution since the files are shared amongst all the jsp pages and every change to a footer file would necessitate a change to all of them manually.

  Any more suggestions?
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Distinguished Expert 2018

Author

Commented:
 I have also tried:

"<%@ include file = "/wtgContent/CityGuide/English/Common/JavaFrontPage.inc"%>"

  with no luck. Basically I have tried every possible solution but it does not work.
with debug set to 99 did the log shed any light on the problem ?  You should see the steps the server is going through to resolve you request.

also is crossContext="true" set on the context containing the jsp page making the call ?
This behavior is by design. I'm not sure but I don't think tomcat will allow you to do this. even if it does, you might face the same problem again on anther servlet container.

I would suggest:
1. use dynamic include (<jsp:include>)
2. map a servlet to /wtgContent in every webapp
3. create that servlet which loading files based on request URI

it is not difficult to do. although you lost some benefits of static include, you gain the flexibility. you never need to worry about where the file you it, be it on local disk or remote http server.
Distinguished Expert 2018

Author

Commented:
 garethwebbley I have tried several setting of the debug option but still no luck. I will try some more and let you know. It is very weird though because the same works for images but not for include files!!

  kennethxu I have thought about the solutions you propose but I want to leave the JSP source code intact. I prefer to resolve it through any means of configuration settings.
have you tried setting crossContext="true" on the context containing the jsp page making the call.  The reason for your problem I believe is that in order to do the include a requestDispatcher is required from the context containing the file to be included which is different to the context with the JSP page.

The include is happening on the server before the page goes to the browser.  For the images the browser is seeing the page finding <IMG> tags and sending separate requests for the image files.

In your server.xml you should have two contexts the one containing the jsp pages and the other containing the include file.  Try making the settings for these two contexts the same ie debug="99" and crossContext="true".

If the problem is still there can you post the log file contents.
Distinguished Expert 2018

Author

Commented:
 Yes I have tried setting crossContext="true"

>In your server.xml you should have two contexts the one containing the jsp pages and the other containing the include file.  Try making the settings
>for these two contexts the same ie debug="99" and crossContext="true".

  What exactly do you mean here? I only have one context, the one containing the context for my include files. I thought this context was shared amongst the rest of the web applications since the same is happening with the images context and it works fine.
Can you post the contents of your server.xml.

Each context that is added to server.xml is considered a separate web application which each have a separate context and, for security purposes, cannot make server side calls to each other (unless crossContext is set to true.)  The include mechanism within jsp uses the include() method of a requestDispatcher which is obtained from the servletContext.  If crossContext is set to "false" (the default) then trying to obtain a context for the page to be included will fail.

You do not have to specify each context separately unless you want to apply non-default properties to a particular context.  Tomcat on startup will look in the webapps directory and create contexts for any directories underneath this that do not have explicit contexts defined in server.xml.

Under your webapps directory you should at least have a ROOT directory.  If this does not have an explicit context defined in server.xml then TOmcat is creating a context for it at startup.  Try adding the following line to your server.xml

<Context path="" docBase="ROOT" debug="99" crossContext="true"/>

This will explicitly define the ROOT context and allow it to obtain the context of other web applications.

Distinguished Expert 2018

Author

Commented:
 Right, these are some of the lines of my server.xml file, the ones of interest to us:

<Context path="" docBase="ROOT" debug="99" crossContext="true" />

     <Context path="/wtgContent" docBase="c:\Tomcat4.1\webapps\wtgContent" debug="99" crossContext="true" />

     <Context path="/AdsForCityGuide" docBase="c:\Tomcat4.1\webapps\AdsForCityGuide" debug="99" corssContext="true" />

<!-- Tomcat Examples Context -->
                    <Context path="/examples" docBase="examples" debug="0" reloadable="true" crossContext="true">
                         <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_examples_log." suffix=".txt" timestamp="true"/>
                         <Ejb name="ejb/EmplRecord" type="Entity" home="com.wombat.empl.EmployeeRecordHome" remote="com.wombat.empl.EmployeeRecord"/>

  A file called "City.jsp" under the "AdsForCityGuide" folder tries to include a file which is under the "wtgContent" context by doing: <%@ include file = "/wtgContent/CityGuide/English/Common/JavaFrontPage.inc"%>

  It still does not work and Tomcat throws a JasperException that it cannot find the file. The only way I can include a file is to have the wtgContent folder relative to each of my webapps, which means I will need to copy it under every single webapp.

  I am starting to believe that there is no way to do exactly what I want with Tomcat. I guess this feature is apllication-specific and not a standard part of the JSP/Servlets specification.

  I will do some more research on this but I doubt I will find a solution (if there is any). I have even posted on the tomcat mailing list but noone knew/knows.

  Thank you. Oh, by the way, is there any reference site that explains the sever.xml file and its tags/attributes in detail? I can't seem to find it under the standard docs that come with tomcat.

  Thank you.
in the servlet specification, it is clearly stated that both static and dynamic include must be within the current context space.

tomcat's crossContext does not mean let you do this thing. crossContext just allows your web application access to other web application's ServletContext object, like tomcat's admin context must has access to other context in order to manage them.
Distinguished Expert 2018

Author

Commented:
 So kennethxu is a few words it is impossible to do it with Tomcat.
actually, with what I have suggested, you don't have to change your jsp too much, only change <%@include to <jsp:include
Distinguished Expert 2018

Author

Commented:
 It's all right, I copied the folder in each web app seperatetly and it worked fine. The include files do not change that often (only once a year) they are just mainly footers. Thank you for your help. I will give points to both of you. I will accept garethwebbley comment as correct (since he has helped me aunderstand the whole concept behind the Context tag) and I will post another question with the same poitns for you :-).

  Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial