jsp include

K

KevinRobinson

Hi,

Can anyone help me with a problem please.

I am using the <%@ include file="/include/amt_header.jsp" %> code to include
a header into a JSP and it works

fine if the file which uses the include statement is in the same folder that
contains the include folder.

The problem is when a file wishes to use the "/include/amt_header.jsp" file
from a different folder.

So for example if my main file is in a folder "cart" and I want to access
the header file contained in the folder "include" then I can't.

I have tried the following but it won't work <%@ include
file="../include/amt_header.jsp" %>.

I am sure there is a siimple solution to this problem but what.

Thanks in Anticipation.

Kevin
 
A

Andrew Thompson

KevinRobinson wrote:
...
So for example if my main file is in a folder "cart" and I want to access
the header file contained in the folder "include" then I can't.

Where is 'cart'? What is the actual path from the root?
I have tried the following but it won't work <%@ include
file="../include/amt_header.jsp" %>.

Given a leading '/' indicates 'from the root of the site',
if that first string ever worked, then this should also work
(from anywhere within the site).
<%@ include file="/include/amt_header.jsp" %>
 
K

KevinRobinson

If I use <%@ include file="/include/amt_header.jsp" %>

File "/include/amt_header.jsp" not found.
 
W

Wojtek

KevinRobinson wrote :
If I use <%@ include file="/include/amt_header.jsp" %>

File "/include/amt_header.jsp" not found.

This is because the file include is dependant on the OS file system. So
if you are using windows and the JSP pages are on drive X:, then the
above statement will look for the file in X:\include.

You must either place the fully qualified path in the include (highly
not portable, as your development environment paths most likely do not
match your test and production paths, or use relative paths.

Count the directories. I have include directives which are several
layers deep:

<%@ include file="../../../_include/_setup.jspf" %>
 
K

KevinRobinson

Thanks,

the ../ worked eventually.

Just one other related question.

If the header.jsp (in the include folder) contains for example <a
href="/email/join_email_list.jsp"> and if
the jsp that I want to use the header in is in the same directory as the
include folder
then I use <%@ include file="/include/amt_header.jsp" %>.

If the jsp that I want to use the header in is in one folder down then I
use:-
<%@ include file="../include/amt_header.jsp" %>.

If I use a jsp with the header in a different folder say 'cart' and I use
<%@ include file="../include/amt_header.jsp" %> then it works with
hrefs pointing to the root but the <a href="/email/join_email_list.jsp"> no
longer works.

Is there a way round this without having to have 2 header files the second
containing <a href="../email/join_email_list.jsp">

Thanks
 
W

Wojtek

KevinRobinson wrote :
Thanks,

the ../ worked eventually.

Just one other related question.

If the header.jsp (in the include folder) contains for example <a
href="/email/join_email_list.jsp"> and if
the jsp that I want to use the header in is in the same directory as the
include folder
then I use <%@ include file="/include/amt_header.jsp" %>.

If the jsp that I want to use the header in is in one folder down then I
use:-
<%@ include file="../include/amt_header.jsp" %>.

If I use a jsp with the header in a different folder say 'cart' and I use <%@
include file="../include/amt_header.jsp" %> then it works with
hrefs pointing to the root but the <a href="/email/join_email_list.jsp"> no
longer works.

Is there a way round this without having to have 2 header files the second
containing <a href="../email/join_email_list.jsp">

You are mixing up URL paths with OS paths.

In a URL the / means start from the web server root.

In an OS path the / (or \) means start from the drive root.

So your includes would take into consideration where they are in your
path structure, whereas your URLs always use the same text.

The URL <a href="/email/join_email_list.jsp"> will always be the same
regardless of which page it is in.
 
J

jacma983

Thanks,

the ../ worked eventually.

Just one other related question.

If the header.jsp (in the include folder) contains for example <a
href="/email/join_email_list.jsp"> and if
the jsp that I want to use the header in is in the same directory as the
include folder
then I use <%@ include file="/include/amt_header.jsp" %>.

If the jsp that I want to use the header in is in one folder down then I
use:-
<%@ include file="../include/amt_header.jsp" %>.

If I use a jsp with the header in a different folder say 'cart' and I use
<%@ include file="../include/amt_header.jsp" %> then it works with
hrefs pointing to the root but the <a href="/email/join_email_list.jsp"> no
longer works.

Is there a way round this without having to have 2 header files the second
containing <a href="../email/join_email_list.jsp">

As wotjek told you, you are confusing two unrelated things:

- the <a href="../..."> shall be resolved by the Web browser of the
user visiting the site if/when it clicks on the link.

- the 'include' is resolved by your servlet container (e.g. Tomcat)
when it will process your .jsp file

Relatives "includes" (i.e. those not starting with a '/') are always
relative to the directory of the .jsp file they appear in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top