Analysis of base tag usage instances and base instance analysis in JSP page files

Source: Internet
Author: User

Analysis of base tag usage instances and base instance analysis in JSP page files

This document analyzes the usage of the base tag in the JSP page file. We will share this with you for your reference. The details are as follows:

When we use IDE tools to generate JSP pages, we usually include the following two pieces of code,

<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>

They are definitely not useless code, as detailed below:

A base tag is a base link tag and a single tag. Used to change the parameter values of all link tags in the file. It can only be used between the flag

Important attributes:

Href --- set the link address with the prefix

Target --- set the display window of the file, which is the same as the target in Mark.

Simple Example:

<Html> 

After the link is clicked, the pop-up file is http: // localhost/x.htm or http: // localhost/y.htm, it adds the base link to the address before the files in these relative paths. If the target attribute is not specified for the link in the target file, use the target attribute in the base tag.

It is often used in the framework structure, such as the two frameworks on the left and right, to display the connections in the files in the framework on the left in the framework on the right. As long as the base tag is used, the target attribute value is written to the right frame name, so you do not need to specify the target attribute for each connection in the file in the left frame.

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

In addition, if the page is switched to a Servlet, And the Servlet is a forward jsp page, if you write the relative path, you should first find the Servlet Path, that is, web. path in url-pattern configured in xml, for example, assume that there is an x. jsp is placed under the webapplication root directory, and the homepage index. jsp is submitted to the servlet, And the Serlet distributes forward to x. the url configuration of jsp and Servlet is as follows:
Copy codeThe Code is as follows: <url-pattern>/servlet/TestServlet </url-pattern>

If <base href = "<% = basePath %>"> x. <script type = "text/javascript" src = "script/check. js "> </script> will expire, because the Servlet access path is http: // localhost/webapp/servlet/TestServlet, then the web server will go to http: // localhost/webapp/servlet/script/go to check. js does not have this file at this time. Therefore, if you encounter such a situation, it is recommended that you use the absolute path.Copy codeThe Code is as follows: <script type = "text/javascript" src = "<% = path %>/script/check. js"> </script>

I hope this article will help you with JSP program design.

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.