File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Changing scriplet to jstl url value tag changes path of a href target Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Changing scriplet to jstl url value tag changes path of a href target" Watch "Changing scriplet to jstl url value tag changes path of a href target" New topic
Author

Changing scriplet to jstl url value tag changes path of a href target

margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    6

This is the jsp code. The servlet compiles and runs without errors until I click on the first a href link. First link is using jstl, second is original and the way the first link looked before I changed it to a jstl tag.





Below is how the page is rendered in the browser. Why did the jstl tag pickup the extra directory "/ch11download"?

And how did the scriptlet climb up one directory and then go down a parallel directory to find the *.mp3 file?



This is the short picture of the directory structure

/netbeans
--> book_apps
----> musicStore
------>/sound/8601/no_difference.mp3
------>/sound/8601/star.mp3
-->ex_stats
----> ch11download
------> web
-------->8601_download.jsp, file containing the code shown above
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    6

I have disabled HTML this time. Thanks. TIA. Margaret.


Here is the JSP code:

<tr><td>You Are a Star</td>
<td>
<a href="<c:url value='/musicStore/sound/${product.code}/star.mp3' />">MP3</a>
</td>
</tr>
<tr><td>Don't Make No Difference</td>
<td><a href="/musicStore/sound/${product.code}/no_difference.mp3">MP3</a></td>
</tr>

Here is the rendered html:

<tr><td>You Are a Star</td>
<td>
<a href="/ch11download/musicStore/sound/8601/star.mp3">MP3</a>
</td>
</tr>
<tr><td>Don't Make No Difference</td>
<td><a href="/musicStore/sound/8601/no_difference.mp3">MP3</a></td>
</tr>

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

/chapter11download is the context path for the web app.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    6

So the JSTL tag will always add the context path? And the scriptlet will always ignore the path and search for a file match?

This is an exercise in a text book and it said to change all the scriptlets to JSTL. I haven't hit anything that did not work before in the text so this surprises me.

Is there some environmental variable that determines if the context is added, or could Netbeans (7) be adding it?

Thanks...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

The <c:url> adds the context path. It's supposed to, and it's not a problem.

The context path is needed on any URL that is generated from the browser: images, scripts, stylesheets, links, form actions, and so on.

When the app is deployed as the root web app, the context path is the empty string. But a web app should never hard-code the context path (be it the empty string or otherwise). The fact that <c:url> does it for us means we don't have to do it ourselves.

P.S. I have no idea what Netbeans does or doesn't do, but you can't rely on Netbeans doing anything when a web app is deployed into the real world.
margaret gillon
Ranch Hand

Joined: Nov 12, 2008
Posts: 318
    
    6

Hi Bear,

Thank you for explaining how the tag works, it is very helpful.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

My pleasure.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing scriplet to jstl url value tag changes path of a href target