0

We have a web app running locally, so in many jsp's I have references to something like http://localhost:8080/MyApp/index.html. We now want to create a deployment, but it seems inconvenient to go through many files of different languages (html, jsp, xml, etc.) and manually change the localhost URL's to the actual deployment URL - say 192.168.0.4:8080/MyApp/index.html.

What do people do here? Ideally I'd like the option of using both URL's so that I can maintain a local version of the webapp to run on my machine, and also have a deployed machine running on my network. One idea was to use a branch/tag in SVN as the deployment and change the URL's only in that branch. This may work for dev + deploy servers, but is there a more general way to abstract URL's used in a webapp?

Any thoughts would be appreciated!

Tony R
  • 11,224
  • 23
  • 76
  • 101

3 Answers3

1

Don't hardcode the domainname nor the contextpath.

Resolve them dynamically. This information can be obtained from HttpServletRequest. E.g.

String base = request.getRequestURL().toString().replace(request.getRequestURI().substring(1), request.getContextPath());
// ...

Use it like follows

<a href="${base}/index.html">

Or use the HTML <base> tag. See also Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP for some hints.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Great, thanks. What would you recommend I do in Java code that's in a separate server application (not Tomcat) that needs the same URL? Environment variable? – Tony R Aug 09 '11 at 17:41
0

It should generally be unnecessary to reference the full URL (including the protocol, host, port, and even the context root) within a web application when you are linking to another resource within the same application. Rather, you should be able to use relative URLs. For example, rather than referencing "http://localhost:8080/MyApp/index.html", you should be able to reference "index.html."

shelley
  • 7,206
  • 4
  • 36
  • 63
0

I used this solution:

${pageContext.request.contextPath}/some/path/index.html

BalusC linked to another post that suggested this, but didn't have it in his answer, so I'm adding my own.

Tony R
  • 11,224
  • 23
  • 76
  • 101
  • That wasn't what you were asking in the concrete question. – BalusC Nov 02 '11 at 11:45
  • It isn't? Sorry, I'm happy to give you credit (I +1'd), but I think this is exactly what I was asking? Using ${pageContext.request.contextPath} abstracts the context... – Tony R Jan 07 '12 at 09:48