Example analysis of base tag usages in JSP paging file _jsp programming

Source: Internet
Author: User

This article analyzes the base tag usage in the JSP paging file. Share to everyone for your reference, specific as follows:

When we use the IDE tool 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:

The base mark is a base link tag and is a single token. The parameter's default value used to change all the link markers in the file. It can only be applied between tags

Important attributes:

HREF---set prefix link address

Target---Set the window displayed by the file, with Target in the a tag

Simple example:

 
 

When a link is made, 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 the target attribute, the target attribute in the base tag is used.

It is often used in frame structures, such as the left and right two frames, and the connections in the files in the left frame are shown in the frame on the right-hand side. As long as the base tag is used to write its target property value as the right frame name, it is no longer necessary 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 turns to a servlet, and the servlet is forward to a JSP page, you should find the path to the servlet first if you write a relative path. That is, the path in the Url-pattern configured in Web.xml, such as: Suppose a x.jsp is placed under the WebApplication root directory, While the home page index.jsp is submitted to the servlet, the URL configured by Serlet to distribute forward to X.jsp,servlet is as follows:

Copy Code code as follows:
<url-pattern>/servlet/TestServlet</url-pattern>

After the servlet completes the forward turn, if there is no <base href= "<%=basePath%>" > x.jsp <script type= "Text/javascript" Script/check.js "></script> will fail because the servlet access path is http://localhost/webapp/servlet/ Testservlet then the Web server will go to http://localhost/webapp/servlet/script/ Go down and find check.js. At this time there must be no this file, so, if you encounter such a situation suggested using an absolute path will not be wrong

Copy Code code 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.