How to understand the "<base href=" <%=basePath%> ">" of JSP pages

Source: Internet
Author: User

The following code is commonly found in JSP files:

What does it <base href="<%=basePath%>"> mean here?

I looked up the base tag in HTML on the W3school website and explained the following:
<base>The label specifies the default address or default destination for all links on the page.

Typically, the browser extracts the corresponding element from the URL of the current document to fill in the whitespace in the relative URL.

Using <base> tags can change this. The browser will then no longer use the URL of the current document and use the specified base URL to resolve all relative URLs. This includes <a>、、<link>、<form> the URL in the tag.

Note: The <base> label must be inside the head element.

In a NetEase blog to see the explanation of this base statement, the link is as follows:
The base tag in the JSP paging file, Jerry

The original text reads as follows:
When we use IDE tools to generate JSP pages, we usually include the following two snippets of code,

<%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> 

They are definitely not useless code, as detailed below:

The base tag is a base link tag and is a single tag. The default value of the parameter used to change all the link tags in the file. it can only be applied to tags and between. All relative paths on your Web page will be preceded by the address that the base link points to.

Important attributes:
href-set the link address of the prefix

target-setting file display window, same as target in a tag

Simple example:

When a link is added, the file that jumps out is http://localhost/x.htm or http://localhost/y.htm, which is the address that the base link points to before the file in the relative path. If the link in the destination file does not specify a target property, the target property in the base tag is used.

Often used in frame structure, such as the left and right two frames, the left side of the frame in the file is displayed in the frame. As long as the base tag is used to write its target property value to the right frame name, it is no longer necessary to specify the target property for each connection in the left-frame file.

When used, the BASE element must appear inside the HEAD of the document before any references to the external source.

In addition, if the page is directed to a servlet and the servlet is a JSP page forward to, the path to the servlet should first be found if the relative path is written. That is, the path in the Url-pattern configured in Web. XML, such as: Suppose there is a x.jsp placed in the WebApplication root directory, While the home page index.jsp is submitted to the servlet, the URL configured by the servlet to distribute forward to X.jsp,servlet is as follows

<url-pattern>/servlet/TestServlet</url-pattern>

 

After the servlet completes the forward turn, if not <base href="<%=basePath%>"> , the x.jsp <script type="text/JavaScript" src="script/check.js"></script> will fail because the servlet's access path is http://localhost/webapp/servlet/ Testservlet then the Web server will go to http://localhost/webapp/servlet/script/ Go down to find check.js at this time there must be no this file, so if you encounter such a situation is recommended to use absolute path will not be wrong.

<script type= "Text/javascript" src= "<%=path%>/script/check.js" ></script>

 

Personal Supplement: Target property

//============================================================================

//============================================================================

//============================================================================

Today in writing a JSP Web page, href can not be used, all the href mouse put up front of the reality is "http:///", unexpectedly there are three "/", and the front also did not show "localhost:8080" to find the majority of days to find out, The last two lines of code that were automatically generated by Eclipse were accidentally deleted,
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
I have not used path and basepath, why this, finally found that the original in Search the Internet, the original base href is not only such a point of effect, especially in the framework.


The information is as follows:

The base tag is a base link tag and is a single tag. The default value of the parameter used to change all the link tags in the file. It can only be applied between tags All relative paths on your Web page will be preceded by the address that the base link points to.

Important attributes:
Href
Set the link address of the prefix

Target
Set the window of the file display, with the target in the a tag

Simple example:


<base href= "http://www.baidu.com" target= "_blank" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>base Marker </title>
<link rel= "Shortcut Icon" href= "ANI. CUR ">
<body>
<a href= "77.htm" target= "_self" >77</a>
<a href= "88.htm" >88</a>
</body>
When a link is added, the file that jumps out is http://www.baidu.com/77.htm or http://www.baidu.com/88.htm, which is the address that the base link points to before the file in the relative path. If the link in the destination file does not specify a target property, the target property in the base tag is used.
Often used in frame structure, such as the left and right two frames, the left side of the frame in the file is displayed in the frame. As long as the base tag is used to write its target property value to the right frame name, it is no longer necessary to specify the target property for each connection in the left-frame file.

When used, the BASE element must appear inside the HEAD of the document before any references to the external source.

This element in Microsoft? Available in HTML for Internet Explorer 3.0, available in scripts in Internet Explorer 4.0.

This element is not rendered.

This element does not need to close the label.





The use of this tag is to solve the relative path problem when programming, such as some CMS, because each page path is different, he will give you to generate <a href= "/sdsd/dsd.html" >sddsds</a> and so on, if I debug in the local, Will certainly open a directory in the local, so chaos, you can generate it relative path, such as <a href= "sdsd/dsd.html" >sddsds</a&gt, as long as the head part of the <base href=http:// Localhost/abc/> can be.

So, this tag is primarily intended to solve some of the relative path problems of Web programming.

Of course, this base also has a usage, such as in the head section adds such a line: <base href= "_blank" >, is the default all links opens in the new window.


You can also understand this:
This is the set base path, BasePath is the variable
Simple static Web page if you set such as: <base href= "http://www.baidu.com", then you will have the following HREF attribute on the basis of the above, such as: <a href= "/http Www.baidu.com/xxx.htm "></a> You now just need to write <a href=" xxx.htm "></a>

How to Understand "<base href=" <%=basePath%> ">" of JSP pages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.