How to understand <base href= "<%=basePath%>"---Reprint

Source: Internet
Author: User

Original link http://316325524.blog.163.com/blog/static/6652052320111118111620897/

"Base href"

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+ "/";
%>

My supplementary:

To deepen your understanding, print out the above code in pieces one by one:

<%out.println ("request.getscheme () = = =" +request.getscheme () + "<br/>");
Out.println ("request.getservername () = = =" +request.getservername () + "<br/>");
Out.println ("request.getserverport () = = =" +request.getserverport () + "<br/>");
Out.println ("request.getcontextpath () = = =" +path);
%>

The results of the page output are:

Request.getscheme () ===http
Request.getservername () ===localhost
Request.getserverport () ===8080
Request.getcontextpath () ===/news

This will tell you what BasePath is.

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:


< html>
< head>
< base href= "http://www.baidu.com" target= "_blank" >
< meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
< Title>base tags </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%>"---Reprint

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.