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 ":
Registered User
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:
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 );
%>
We can see that in the href attribute of the tag, 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: UseTag (recommended)
Below isIntroduction:
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 useThe href attribute in the tag 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, it usesTags:
The Code is as follows:
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 + "/";
%>
// Here we can directly use the relative path (that is, relative to the base tag)
Login
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:
// SetThe relative path is relative to the path in the base, instead of the Request Path of the browser address ~~~
Login
We can see that the HTML code returned by JSP containsContent.
That is to say, in this HTML file, all "relative links (such as:)" 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 )...