<html:base>

Renders an HTML <base> element with an href attribute pointing to the absolute location of the enclosing JSP page. This tag is valid only when nested inside an HTML <head> element.

This tag is useful because it allows you to use relative URL references in the page that are calculated based on the URL of the page itself, rather than the URL to which the most recent submit took place (which is where the browser would normally resolve relative references against).

While HTML <base> tag does additional processing of request elements, its basic functionality boils down to the following expression:

 <base href="
   <%= request.getScheme()+ "://"+
       request.getServerName()+":"+
       request.getServerPort()+
       request.getRequestURI()
   %>">

Note: if you access an action from the browser and then forward to a JSP page, then you can use request.getRequestURL() to obtain address of the action, and request.getRequestURI() to obtain address of the JSP page.

For example, consider that the action is defined in struts-config.xml as "/Home", JSP page is stored in directory "pages" as "home.jsp" and context is defined as "myapp". The results obtained from JSP page will be similar to the following:

request.getRequestURL() ==> http://myserver:8080/myapp/Home.do
request.getRequestURI() ==> /myapp/pages/home.jsp

The above behavior is specified in Servlet documentation. It states that request.getRequestURI() points to location that was forwarded to. See this article for insight.

Attributes

Attribute

Description

Type

ref

The reference from which the base uri will created. Possible values are: * page - The base uri will be the jsp page location. (default) * site - The base uri will be the application context path.

String

server

The server name to use instead of request.getServerName().

String

target

The window target for this base reference.

String

Examples

Example:
Browser navigates to the action: "http://localhost:8080/myapp/Home.do" Action forwards to the view: "/pages/home.jsp"

Example 1: <html:base/> is not specified in home.jsp page, HTML <base> tag is not generated

 

Explanation

 

Base, assumed by browser

http://localhost:8080/myapp (address of /myapp/Home.do)

After browser navigated to

http://localhost:8080/myapp/Home.do

location, it received content of home.jsp page in response. From browser's point of view content of home.jsp page is identified by above address. Base tag is not specified, so browser calculates default base location from the address it used to retrieve the resource. Making analogy with filesystem, Home.do is a file located in localhost:8080/myapp directory. Browser uses this "directory" as base for Home.do "file" as well as for other "files".

Root, assumed by browser

http://localhost:8080 (webserver name + port)

Web browser does not have information about applications running on server or about contexts, defined for these applications. From browser's point of view, it read a "file" from

http://localhost:8080

"drive".

Expanding relative path

"About.do" -->

http://localhost:8080/myapp/About.do

Browser concatenates base and a relative address to produce full address

Expanding absolute path

"/About.do" -->

http://localhost:8080/About.do

Browser concatenates root "drive name" with absolute link to produce full address

Example 2: <html:base/> is specified in home.jsp page

 

Explanation

 

Generated HTML <base> tag

<base href="http://localhost:8080/myapp/pages/home.jsp">

Struts generated <base> tag, setting

http://localhost:8080/myapp/pages

as base "directory" for relative links. Despite that browser requested Home.do resource, Struts sets base address according to location of home.jsp file.

Base, assumed by browser

http://localhost:8080/myapp/pages (taken from <base> tag)

Because <base> tag is specified, browser calculates default base location from href attribute. Making analogy with filesystem, home.jsp is a file located in localhost:8080/myapp/pages directory. Browser uses this "directory" as base for home.jsp "file" as well as for other "files" with relative URIs.

Root, assumed by browser

http://localhost:8080 (webserver name + port)

Web browser does not have information about applications running on server or about contexts, defined for these applications. From browser's point of view, it read a "file" from

http://localhost:8080

"drive".

Expanding relative path

"About.do" -->

http://localhost:8080/myapp/pages/About.do

Browser concatenates base and a relative address to produce full address

Expanding absolute path

"/About.do" -->

http://localhost:8080/About.do

Browser concatenates root "drive name" with absolute link to produce full address

Example 3: <html:base/> tag is specified in home.jsp page with optional attributes

Struts <html:base> tag

HTML <base> tag

Explanation

<html:base server="myserver"/>

<base href="http://myserver:8080/myapp/pages/home.jsp">

Struts substitutes server name in href attribute of <base> tag. This may be useful in test environment or for accessing resources from other website using relative links.

<html:base target="myWindow"/>

<base href="http://myserver:8080/myapp/pages/home.jsp" target="myWindow>

Activating any link on this page opens a new document in the frame or window with specified name. If window with specified name does not exist, browser will create a new window. See HTML 4.0 specs for details.

  • No labels