aspose file tools*
The moose likes JSP and the fly likes relative path confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "relative path confusion" Watch "relative path confusion" New topic
Author

relative path confusion

Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Hi Ranchers,
I am having lots of confusion over setting up relative paths for my pages.

I have the following jsp.



I want to pass contextPath to the javascript function shown below.



But I am getting error because its trying to pass "/actualContextPathName".

I would like to know how to use context path if i need to include...

1) Image
2) JS
3) Html
4) Another Jsp
5) css
6) using window.showmodal in js

I have created different folders for js, html, jsp, images, css etc in WebContent and i am using websphere 5.1.

Any help and clarification is appreciated.

Thanks and regards,
Karan
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Karan Jain:

But I am getting error because its trying to pass "/actualContextPathName".

I'm not sure what you mean by that exactly.

In JSP 2.0 a link, relative to the contextPath, would be built this way:


In JSP 1.2:



Does that help?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

And be aware that you can never "pass" info from JSP to JavaScript. JSP is evaluated on the server, JavaScript on the client.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Hi Ben,
Thanks for the reply.


But I am getting error because its trying to pass "/actualContextPathName"


I mean to say that i am trying to pass the context path to the js function and as there is a forward slash "/" it might be giving the problem. I want to use the context path in the js function to open a jsp page.

If you see my code, i am using request.getContextPath() only.

But my confusion is still there because i thought that we can use the context path with any resource we are including but it does not seem like that.

If you see the code above in my previous post, I needed to include jsp in a different way than html and so forth.
I understanding was from wherever I am calling/including the resource contextpath should still work.

So in my code I should able to use:
/Contextpath/jsp/test.jsp
/Contextpath/html/test.html
/Contextpath/css/test.css etc.

It would be great if you can explain it further.

Thanks Bear for letting me know about that jsp, js stuff.

Actually I am very new to all these stuff.

Thanks and regards,
Karan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

I'm not sure exactly what your confusion is, but yes, for all client-initiated requests (images, css, JS files, etc) it is apporpriate for the URLs to all start with the context path.

The context path maps to the root of the web application (the folder that holds WEB-INF).

So if your images are in a folder named images at the same level as WEB-INF:

/contextpath/images/image.gif

will address an image in that folder.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

As Bear mentioned.
JSP and Javascript exist in two different worlds.
The easiest way to picture it (for me anyway) is to think "the JSP writes the Javascript"

So, if you want a Javascript variable named contextPath that contains the same value as the jsp's request.getContextPath(), you will need to write it.



If you look at the generated output from running this (View -> View HTML Source in your browser) you would see:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

This article may help with the JSP <--> JavaScript issue.
Karan Jain
Ranch Hand

Joined: May 30, 2007
Posts: 82
Thanks a lot Bear and Ben...
Now I think my confusion is gone for most part and i implemented it and working fine... thanks a lot for you help and patience...

regards,
Karan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: relative path confusion