开发者

How to abstract Tomcat URL's for deployment?

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,开发者_如何学C but is there a more general way to abstract URL's used in a webapp?

Any thoughts would be appreciated!


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.


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."


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜