0

I have a tomcat myeclipse servlet project, if /w HTML links i use "/" or with CSS I use background: url(/); the link generated ends up being localhost: 8080/myReference, problem is my entire project is under the context /hs, so the reference generated should be localhost:8080/hs/myReference. I don't want to use the HTML base tag, because I need this root changed for both CSS,HTML & JS.

Here is the server.xml entry we have:

<Context path="/hs" docBase="hs" reloadable="true" crossContext="true">
  <Resource name="jdbc/hsDS" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="0" maxWait="10000" username="3" password="1" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/healthsafety?autoReconnect=true" /> 
  </Context>

This has been driving me crazy, I need to get this sorted so can use "/" absolute references in HTML,CSS,JS, any help would be much appreciated!

williamsandonz
  • 15,864
  • 23
  • 100
  • 186

1 Answers1

2

If you really don't want to use the HTML <base> tag for some unobvious reason, you'd need to prepend ${pageContext.request.contextPath} in every single resource URL which you'd like to make relative to the domain root. This will print the current context path dynamically.

E.g.

<head>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/default.css" />
    <script src="${pageContext.request.contextPath}/js/default.js"></script>
</head>
<body>
    <img src="${pageContext.request.contextPath}/img/logo.png" />
    <a href="${pageContext.request.contextPath}/page.jsp">link</a>
    <form action="${pageContext.request.contextPath}/servlet"><input type="submit" /></form>
</body>

Using JSTL <c:set> to give it a shorter alias may soften the pain.

As to CSS background images, just put those images in a subfolder of the CSS folder and use the path without leading /. E.g. /css folder for CSS files and /css/images folder for CSS images. Then you can consistently use url('images/name.ext') for CSS images.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • but that only works for the HTML, the css can't use that dynamic reference and it will manually need the /hs prefix in it. Which will then fail in production, because in production there is no /hs context – williamsandonz Mar 22 '12 at 05:02
  • is it possible to bind an alternative URL to to the localhost:8080/hs URL? E.G simuate what the windows hosts.file does, by going www.local.hs.Company.co.nz localhost:8080/hs – williamsandonz Mar 22 '12 at 05:04
  • As per your second comment, I think you need to realize that it's the client (the webbrowser) who has got to resolve those URLs based on the request URL, not the server. – BalusC Mar 22 '12 at 05:06
  • Heya. Thanks for your answer. Yes that CSS problem would get the ball rolling for me. Trouble is Javascript is going to have the same problem. (And it's a heavy JS app as its jquerymobile). I'm currently prefixing all URL's with the context root using JS which is annoying as ***!. I also don't want to put images inside the css folder as its semantically incorrect. – williamsandonz Mar 22 '12 at 05:07
  • 1
    Semantically incorrect? CSS images are CSS images, right? I didn't mean to say that you should put **all** images in the CSS folder. The ones which you reference by `` should of course not go in there. Think for a second. As to JavaScript, well you could take benefit of the fact that URLs are always resolved relative to the request URL of the parent file, not to the local disk file system structure in the server. – BalusC Mar 22 '12 at 05:08
  • I just want to continue to structure my file system based on how I like things. (Which is to have one image directory). Not trying to be annoying here, was hoping for a more elegant solution. I do realise it's the browser, I've never had this problem with .NET visual studio or PHP /rails sites though. Thanks for your help! – williamsandonz Mar 22 '12 at 05:14
  • I wonder if I can use '/' as my context rather than '/hs' and then smack each of myeclipse projects on a different port – williamsandonz Mar 22 '12 at 05:16