Solution to JSP path problems... Hope to help some friends ..

Source: Internet
Author: User

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 )...

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.