Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

relative path confusion

 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And be aware that you can never "pass" info from JSP to JavaScript. JSP is evaluated on the server, JavaScript on the client.
 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This article may help with the JSP <--> JavaScript issue.
 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic