Home > OS >  How to create portable urls in jsp webpages over servers
How to create portable urls in jsp webpages over servers

Time:10-08

I am moving my website to a new server the original is at http://www.jthink.net , the new one is temporarily at https://test.jthink.net this has exposed the fact that my webpages have http://www.jthink.net hard code in the header and footers.

The website mainly comprises jsp pages, it was suggested I moved to relative links, but this will not work because the headers are included in every page with

<%@ include file="/layout/inc/pagestart.jsp" %>

and the webpages may vary in no of levels, so if they share the header relative paths will only work for pages with same no of levels of folder structure.

http://www.jthink.net/songkong/en/index.jsp

http://www.jthink.net/songkong/index.jsp

So I need to use absolute links but not hardcode it to www.jthink.net or test.jthink.net so I can use same page on either.

So I tried

<link rel="icon" href="<%=request.getServerName()%>/songkong/images/songkong32.png" type="image/png" />

but that resolved to

<link rel="icon" href="test.jthink.net/songkong/images/songkong32.png" type="image/png" />

and didn't work

So I then tried

<link rel="icon" href="https://<%=request.getServerName()%>/songkong/images/songkong32.png" type="image/png" />

but that is hardcoding https so wouldn't work on existing site.

What is right way to do this please ?

CodePudding user response:

The solution was to use server relative links, if the url starts with a single / that resolves to the protocol and name of the current server

e.g If on page http://www.example.net/index.jsp a url of / would mean http://www.example.net/, whereas if on page https://www.example.net/index.jsp a url of / would mean https://www.example.net/

I think important to note the difference with relative links here, as it is confusing. I was recommended to use relative links before, but i considered relative links to be those that start with .. (meaning parent directory), or . (meaning current directory) as on unix and these are not good because can get get different results depending on what page you are on within server

e.g

If on page http://www.example.net/index.jsp a url of .. would mean http://www.example.net/, whereas if on page http://www.example.net/test/index.jsp a url of .. would mean http://www.example.net/test

  • Related