Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative URL slash when using base tag

Tags:

html

jsp

I'm trying to learn more about the way paths are interpreted with a JSP.

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

The reason I am asking is because I often need to express things in terms of the context root within a JSP (for a form action for example), so that the form is routed to the appropriate servlet as I've mapped it in web.xml.

Most of the time I would just use ${pageContext.request.contextPath} every time, until I learned about the base tag.

However, I'm guessing that since in a JSP the slash is interpreted by the server as the root of the web application, I can't have my base be, for example:

<base href="/foo"/>

and then in the form have:

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

Since this would cause the form to be submitted to mydomain.com/extension instead of mydomain.com/foo/extension. Am I understanding this correctly, that since slash has special meaning, you generally need to end the base HREF with a backslash to get the desired effect?

Thanks

like image 640
user1154644 Avatar asked Sep 25 '14 15:09

user1154644


People also ask

How do you define an HTML base tag to specify base URL for all relative URLs in a document?

The <base> tag specifies the base URL and/or target for all relative URLs in a document. The <base> tag must have either an href or a target attribute present, or both. There can only be one single <base> element in a document, and it must be inside the <head> element.

What is base URL and relative URL?

A URL specifies the location of a target stored on a local or networked computer. The target can be a file, directory, HTML page, image, program, and so on. An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point.

How do you use a base tag?

The HTML base tag is used to specify a base URI, or URL, for relative links. This URL will be the base URL for every link on the page and will be prefixed before each of them. For example, if the URL specified by the base tag is “www.xyz.com” and then every other URL on the page will be prefixed by, “www.xyz.com/”.

How do I give a href a relative URL?

To link pages using relative URL in HTML, use the <a> tag with href attribute. Relative URL is used to add a link to a page on the website. For example, /contact, /about_team, etc.


Video Answer


1 Answers

Not really.

Browsers do not simply concatenate your base href attributes and relative URLs. Instead, <base> tag sets the Document base URL which is then used to dynamically generate a new path.

You can see an interesting example in the WHATWG HTML 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 even ends with a filename.

This means that if you do not put a trailing slash to your base href attribute, foo will be treated as the last, volatile part of the path and will essentially be removed. By way of 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  -->
like image 77
Denis Avatar answered Sep 21 '22 10:09

Denis