This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP context help

 
Alfred Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Can anyone please explain to me the concepts of the context in the JSP. I have written a web application running on JBoss. The context-root in the jboss-web.xml is set to /dcuweb. In my .war file, I have a subdirectory /jsp containing all the jsp's and a subdirectory called /images containing all the images. The problem is, in the JSP, how do I link to the images and other JSP's. Do I use <img src="/images/logo.png"/> and the links <a href="/jsp/test.jsp">.
The only way I can get it to work is by including the context-root (/dcuweb) in every link, but should the context-root change, I must change all the JSP's.

Regards
Alfred
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alfred Thomas:
Hi all
The context-root in the jboss-web.xml is set to /dcuweb.


Poss the exact text of jboss-web.xml for above configuration.
 
Alfred Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Here is the jboss-web.xml as requested:

<jboss-web>
<context-root>dcuweb</context-root>
</jboss-web>
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely right in your analysis.
The web server knows nothing of "context" and so treats "/" as its root directory.
So a src of "/images/logo.png" will look for look for "/images/logo.png" rather than "/dcuweb/images/logo.png" which is what you are intending (ie absolute path from your context root)

There are a couple of approaches to take in this case
1 - render the image URLs dynamically - normally with the use of a custom tag. eg struts provides an <html:img page="/images/logo.png"> tag which creates an image tag, including the context-root in the src URL (which it gets from request.getContextPath(). So that would work even if you changed the context name.

The other approach is to use relative links rather than absolute to specify your images.
ie
<img src="../images/logo.png">
The caveat on this approach is that you then also have to specify a <base> tag on your jsps, which tells the server where to resolve relative links from. The base tag probably also needs to be created dynamically. A custom tag again is probably the best approach. One of the struts tags: html:base is very useful in this case.


Hope this helps some,
evnafets
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would avoid page-relative links -- they've only caused me heartache and pain.

I've used the <base> HTML tag approach on occasion, but I usually use the server-relative references as Stefan mentioned that are prefixed with the context path. If you are using JSP 2.0, the context path is easily obtainable via ${pageContext.request.contextPath}.

As in:



If there are going to be a lot of such image references on a page, I create a "shortcut" scoped variable using <c:set>.

E.g.


[ May 29, 2006: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65535
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. If you are not using JSP 2.0, a similar mechanism using scriplet variables and scriplet expressions can be used, albeit with slightly messier notation.
 
Alfred Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan and Bear.

You both described what sounds like good alternatives to what I used. I will try your suggestions within a few days.

Regards,
Alfred
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!