If "relative path" is used in JSP, problems may occur.
Because the "relative path" in the webpage is relative to the "URL request address" to find resources.
What does the above sentence mean?
For example:
Assume that we have a project: MyApp
Under this project, there is a JSP folder
This folder includes:
Login. jsp // login page
Register. JPs // registration page
Enter the address in the browser (Note: The content of the address ):
Http: // localhost: 8080/MyApp/JSP/login. jsp
At this time, the browser will link to the "login page" (login. jsp)
The login. jsp file contains the following "code ":
<A href = "JSP/register. jsp"> Registered User </a>
If we click this link, the following error link will appear in the address bar of the browser:
Http: // localhost: 8080/MyApp/JSP/register. jsp
See ~~
Why does "/JSP/register. jsp" appear?
Because the "Relative Link" in the webpage is determined by the URL path you requested.
That is:
The Request Path is http: // localhost: 8080/MyApp/JSP/login. jsp.
Then, the browser will find JSP/register. jsp in this path (http: // localhost: 8080/MyApp/JSP /).
Therefore, the following error occurs:
Http: // localhost: 8080/MyApp/JSP/register. jsp
The above problem is caused by different URLs between the called page and the called page,
Such errors often occur when two pages are forwarded.
Because forward is performed in the background, it is transparent to the client. (That is, the URL does not change, but the data content is returned from another page ...)
How can this problem be solved?
(1) Method 1: use absolute paths directly (not recommended)
On the JSP page, obtain the absolute address of the project (If your project is called MyApp, the obtained address is http: // localhost: 8080/MyApp /):
The Code is as follows:
<! -- ************** Method 1 ******************** -->
<% @ Page Language = "Java" pageencoding = "GBK" contenttype = "text/html; charset = GBK" iselignored = "false" %>
<%
String Path = request. getcontextpath ();
// Obtain the address of the project (for example, http: // localhost: 8080/MyApp/) and assign the value to the basepath variable.
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
// Put "project path basepath" into pagecontext and read it in El expression later.
Pagecontext. setattribute ("basepath", basepath );
%>
<HTML>
<Head> <Body>
<A href = "$ {pagination. basepath} JSP/register. jsp">
</Body>
</Html>
<! -- *********************************** -->
We can see that in the href attribute of tag <A>, we directly use
"This project path $ {pagination. basepath}" and "JSP/register. jsp ",
To form an absolute path (http: // localhost: 8080/MyApp/JSP/register. jsp)
But there is a bad thing to do, that is, we must add "$ {pagination. basepath}" before each link }"
It would be terrible to do so.
(2) Method 2: Use the <base> tag in HTML (recommended)
The following is an introduction to <base> in HTML:
The base element specifies the reference URLs of all links on the page.
By default, links in the page (including the addresses of style sheets, scripts, and images) are relative to the address of the current page (that is, the request URL in the browser address bar ).
We can use the href attribute in the <base> label to set all "relative reference URLs ".
What does the above mean? Let's take a look at the code ~~
This is the JSP code.
The following code (similar to the JSP code in method 1 above)
However, the $ {pagination. basepath} + "relative path address" method is not used here,
Instead, the <base> tag in the HTML file is used:
The Code is as follows:
<! -- ********************************* -->
<% @ Page Language = "Java" pageencoding = "GBK" contenttype = "text/html; charset = GBK" iselignored = "false" %>
<%
String Path = request. getcontextpath ();
// Obtain the full project path (assume that your project is named MyApp, the obtained address is http: // localhost: 8080/MyApp /):
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<HTML>
<Head>
<! -- Base needs to be placed in head -->
<Base href = "<% = basepath %>">
</Head>
// Here we can directly use the relative path (that is, relative to the base tag)
<A href = "JSP/login. jsp"> login </a>
</Html>
<! -- *********************************** -->
After reading the above Code, you may still have some questions *_*~~
However, when you see the following code, it may suddenly become clear (* ^__ ^ .......
After executing the preceding JSP code, we can view the HTML code returned to the client in the browser:
After the preceding JSP is executed, the returned HTML code is as follows:
<HTML>
<Head>
<Base href = "http: // localhost: 8080/MyApp/">
</Head>
// After <base> is set, the relative path is relative to the path in the base, instead of the Request Path of the browser address ~~~
<A href = "JSP/login. jsp"> login </a>
</Html>
The HTML code returned by JSP contains <base href = "http: // localhost: 8080/MyApp/">.
That is to say, in this HTML file, all "relative links (for example, <a href =" JSP/login. jsp ">)" are relative to the base
PATH (http: // localhost: 8080/MyApp/), so we can use the relative link instead of worrying,
The page cannot be found due to different forwarding operations (forward) or request addresses ~ (Http: 404 )...
Reprinted: http://hi.baidu.com/zhuyuli521/blog/item/3b9982ecfbea17deb31cb1b0.html