Relative slash when using a base tag

I am trying to learn more about how paths are interpreted using JSP.

When using a basic HTML tag in combination with a relative URL, is it pretty standard to end a basic href with a slash?

The reason I ask is because I often have to express things in terms of the context root in the JSP (for example, for the action of the form), so the form is sent to the appropriate servlet since I matched it in web.xml.

In most cases, I would just use ${pageContext.request.contextPath} every time until I find out about the base tag.

However, I assume that since the slash in the JSP is interpreted by the server as the root of the web application, I cannot have my own base, for example:

 <base href="/foo"/> 

and then in the form:

 <form action="/extension"> ... </form> 

As this will cause the form to be submitted by mydomain.com/extension instead of mydomain.com/foo/extension . Do I understand correctly that since slash is of particular importance, you usually need to end the base HREF with a backslash to get the desired effect?

thanks

+6
source share
1 answer

Not really.

Browsers don't just combine your base href attributes and relative URLs. Instead, the <base> sets the base URL of the document, which is then used to dynamically create a new path.

You can see an interesting example in the HTML WHATWG specification:

 <base href="http://www.example.com/news/index.html"> <p>Visit the <a href="archives.html">archives</a>.</p> 

Note that the href attribute ends only with the file name.

This means that if you don’t attach the trailing slash to the base href attribute, foo will be considered the last, mutable part of the path and will essentially be deleted. As an example:

 <base href="http://www.example.com/news/index.html" /> <img src="ball.png" /> <!-- GET http://www.example.com/news/ball.png --> 

 <base href="http://www.example.com/news/" /> <img src="ball.png" /> <!-- GET http://www.example.com/news/ball.png --> 

 <base href="http://www.example.com/news" /> <img src="ball.png" /> <!-- GET http://www.example.com/ball.png --> 

 <base href="http://www.example.com/" /> <img src="ball.png" /> <!-- GET http://www.example.com/ball.png --> 

 <!-- If served from http://www.example.com: --> <base href="/" /> <img src="ball.png" /> <!-- GET http://www.example.com/ball.png --> 
+7
source

Source: https://habr.com/ru/post/975840/


All Articles