You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Behrang Saeedzadeh <be...@saeedzadeh.com> on 2002/09/18 15:48:46 UTC

A very strange behaviour

Hi

I'm designing a new web application and I have stored my files in the D:\Tests\Web\HeaderTest directory and I have defined the following Context in the server.xml:

<Context 
      path="/headertest" 
      docBase="D:\Tests\Web\HeaderTest" 
      debug="0" 
      privileged="true"
 />

I suppose this tells the Tomcat that the root directory for my webapp is D:\Tests\Web\HeaderTest or http://localhost:8080/headertest.

But if I define a link in a page in my webapp such as:

<a href="/home.jsp">Link </a> 

it refers to a wrong document: http://localhost:8080/home.jsp which is unavailable.

Does anybody know what's wrong with this?

Also another problem is that when I include a file using the include action or even the include directive the links defined in the included document changes so they don't refer to their original destinations anymore. For example, I want a header.html to be included in all the pages throughout my web site but if I include the header.html in subdirectories of my web app, the links get broken.

I have tried lots of guesses and I have read some parts of JSP 1.2 Spec that are about includes and ... but I have not found the answer.

All helps are appreciated.
Thanks in advance.

Re: A very strange behaviour

Posted by Jacob Kjome <ho...@visi.com>.
Hello Behrang,

Well, that's not "strange behavior", it is exactly what one should
expect.  Keep in mind that your context is at:
http://localhost:8080/headertest

As the browser see it, it is one directory in from the root of the
web.  The server understands this as a context rather than just
another directory.  If you want to tell the server than you want to
reference a file from this same context, you need to provide the name
of the context in your linked path.

so instead of:
<a href="/home.jsp">home</a>

you need:
<a href="/headertest/home.jsp">home</a>

Now, you might as "well, what if I change the same of the context?"
"Now I am stuck with a hardcoded context path in my jsp."  There are a
couple solutions.

1. dynamically write in the context name at runtime so no matter what
context name you deploy your app under, it will always refer to the
correct context.

2. refer to your pages in a relative fashion.  When you reference "/",
you reference the root of the web.  However, if you just did "./",
that says "whatever directory I am in, start from there and find my
document."

3. deploy your app to the root of the web.  Look at Tomcat's ROOT
application and how it is deployed.

All solutions have their issues.  #1 hobbles you with having to place
all that dynamic writing of context's.  #2 makes you always have to
know what directory your page is in and how it is relative to all the
other pages.  And, if you move the page to another directory,
especially deeper or more shallow in the tree, you will have to modify
the links to point to the new relative locations of all the files.  #3
can be deceiving because it looks like the problem is solved, but if
you count on this solving your problem, you have to make sure that
everyone who installs your app does so as the root application and you
can't always count on that.

Another way to solve this issue is to use MVC where you have a servlet
mapping and some event handling system that knows where the pages are.
However, you still have to provide the reference to the  current
context in the link that points to your controller servlet.  So,
you'll never solve this issue fully, but you have a number of options
to go with to help deal with it.

Hope that helps.

Jake


Wednesday, September 18, 2002, 8:48:46 AM, you wrote:

BS> Hi

BS> I'm designing a new web application and I have stored my files in the D:\Tests\Web\HeaderTest directory and I have defined the following Context in the server.xml:

BS> <Context 
BS>       path="/headertest" 
BS>       docBase="D:\Tests\Web\HeaderTest" 
BS>       debug="0" 
BS>       privileged="true"
BS>  />

BS> I suppose this tells the Tomcat that the root directory for my webapp is D:\Tests\Web\HeaderTest or http://localhost:8080/headertest.

BS> But if I define a link in a page in my webapp such as:

BS> <a href="/home.jsp">Link </a> 

BS> it refers to a wrong document: http://localhost:8080/home.jsp which is unavailable.

BS> Does anybody know what's wrong with this?

BS> Also another problem is that when I include a file using the include action or even the include directive the links defined in the included document changes so they don't refer to their original
BS> destinations anymore. For example, I want a header.html to be included in all the pages throughout my web site but if I include the header.html in subdirectories of my web app, the links get
BS> broken.

BS> I have tried lots of guesses and I have read some parts of JSP 1.2 Spec that are about includes and ... but I have not found the answer.

BS> All helps are appreciated.
BS> Thanks in advance.



-- 
Best regards,
 Jacob                            mailto:hoju@visi.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: A very strange behaviour

Posted by Behrang Saeedzadeh <be...@saeedzadeh.com>.
Lots of thanks for your response, Miguel

I think that you mean that I have to change the /home.jsp to ../home.jsp,
isn't it?

Thanks again,
Behrang S.


----- Original Message -----
From: "Miguel Angel Mulero Martinez" <mi...@mad.tecsidel.es>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Wednesday, September 18, 2002 6:27 PM
Subject: RE: A very strange behaviour


> This is normal. The problem is the browser, that translates "/home.jsp" to
> server/home.jsp and makes this petition to tomcat.
> If you want change that, use relative paths.
>
>
>
> -----Mensaje original-----
> De: Behrang Saeedzadeh [mailto:behrang@saeedzadeh.com]
> Enviado el: miƩrcoles, 18 de septiembre de 2002 15:49
> Para: tomcat-user@jakarta.apache.org
> Asunto: A very strange behaviour
>
> Hi
>
> I'm designing a new web application and I have stored my files in the
> D:\Tests\Web\HeaderTest directory and I have defined the following
> Context in the server.xml:
>
> <Context
>       path="/headertest"
>       docBase="D:\Tests\Web\HeaderTest"
>       debug="0"
>       privileged="true"
>  />
>
> I suppose this tells the Tomcat that the root directory for my webapp is
> D:\Tests\Web\HeaderTest or http://localhost:8080/headertest.
>
> But if I define a link in a page in my webapp such as:
>
> <a href="/home.jsp">Link </a>
>
> it refers to a wrong document: http://localhost:8080/home.jsp which is
> unavailable.
>
> Does anybody know what's wrong with this?
>
> Also another problem is that when I include a file using the include
> action or even the include directive the links defined in the included
> document changes so they don't refer to their original destinations
> anymore. For example, I want a header.html to be included in all the
> pages throughout my web site but if I include the header.html in
> subdirectories of my web app, the links get broken.
>
> I have tried lots of guesses and I have read some parts of JSP 1.2 Spec
> that are about includes and ... but I have not found the answer.
>
> All helps are appreciated.
> Thanks in advance.
>
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: A very strange behaviour

Posted by Miguel Angel Mulero Martinez <mi...@mad.tecsidel.es>.
This is normal. The problem is the browser, that translates "/home.jsp" to
server/home.jsp and makes this petition to tomcat.
If you want change that, use relative paths.



-----Mensaje original-----
De: Behrang Saeedzadeh [mailto:behrang@saeedzadeh.com]
Enviado el: miƩrcoles, 18 de septiembre de 2002 15:49
Para: tomcat-user@jakarta.apache.org
Asunto: A very strange behaviour

Hi

I'm designing a new web application and I have stored my files in the
D:\Tests\Web\HeaderTest directory and I have defined the following
Context in the server.xml:

<Context
      path="/headertest"
      docBase="D:\Tests\Web\HeaderTest"
      debug="0"
      privileged="true"
 />

I suppose this tells the Tomcat that the root directory for my webapp is
D:\Tests\Web\HeaderTest or http://localhost:8080/headertest.

But if I define a link in a page in my webapp such as:

<a href="/home.jsp">Link </a>

it refers to a wrong document: http://localhost:8080/home.jsp which is
unavailable.

Does anybody know what's wrong with this?

Also another problem is that when I include a file using the include
action or even the include directive the links defined in the included
document changes so they don't refer to their original destinations
anymore. For example, I want a header.html to be included in all the
pages throughout my web site but if I include the header.html in
subdirectories of my web app, the links get broken.

I have tried lots of guesses and I have read some parts of JSP 1.2 Spec
that are about includes and ... but I have not found the answer.

All helps are appreciated.
Thanks in advance.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>