In the JSP, if you use relative paths, you have
There may be a problem.
Because of the "relative path" in the Web page, he is looking for resources relative to "URL request Address".
What does it mean by this sentence?
As an example:
If we have a project: MYAPP
Under this project, there is a JSP folder
This folder includes the following:
login.jsp//Landing page
REGISTER.JPS//Registration page
We enter the address in the browser (note: The contents of the address):
http://localhost:8080/MyApp/jsp/login.jsp
At this time, the browser will link to the "Landing page" (login.jsp)
The following "partial code" is included in the login.jsp file:
<a href= "jsp/register.jsp" > Registered users </a>
Then, if we click on this link, it will appear in the browser address bar, the following error link:
http://localhost:8080/MyApp/jsp/jsp/register.jsp
See ~
Why does "/jsp/jsp/register.jsp" appear?
Because, the "relative link" in the Web page is relative to the URL path that you requested.
That
Because the request path here is: http://localhost:8080/MyApp/jsp/login.jsp
Then, the browser will be in this path (ie: http://localhost:8080/MyApp/jsp/) to find
jsp/register.jsp
Therefore, the following error message appears:
http://localhost:8080/MyApp/jsp/jsp/register.jsp
The above problem is caused by the different URL of the calling page and the page being called.
This type of error also often occurs when a "forward" (forward) operation is performed between 2 pages.
Because forward is done in the background, it is transparent to the client. (that is, the URL does not change, and the number
The content is returned from another page ... )
So how do we solve this problem?
(i) method one: Direct use of absolute path (not recommended)
At the JSP page end, get the absolute address of this project (if your project is called MyApp, then you get to the ground
Address is http://localhost:8080/MyApp/):
The code is as follows:
<!--************** Method one *****************--
<%@ page language= "java" pageencoding= "GBK"
Contenttype= "TEXT/HTML;CHARSET=GBK" iselignored= "false"%>
<%
String path = Request.getcontextpath ();
Get the address of this project (ex: http://localhost:8080/MyApp/) assigned to BasePath
Variable
String basepath = request.getscheme () + "://" +request.getservername ()
+ ":" +request.getserverport () +path+ "/";
Put "project path BasePath" into PageContext, and read it later with an El expression.
Pagecontext.setattribute ("BasePath", BasePath);
%>
<body>
<a href= "${pagescope.basepath}jsp/register.jsp" >
</body>
<!--*************************************-->
We can see that within the href attribute of the label <a>, we are directly using the
"This project Path ${pagescope.basepath}" plus "jsp/register.jsp",
Thus constituting an absolute path (i.e.: http://localhost:8080/MyApp/jsp/register.jsp)
But there's a very bad thing about this, and that is that we have to add in front of each link
"${pagescope.basepath}"
It would be a horrible thing to do.
(ii) method two: Using <base> tags in HTML (recommended)
Here is an introduction to the <base> in HTML:
base element to specify the base URL for all links in the page
By default, links in pages (including the address of style sheets, scripts, and images) are relative to the current
The address of the page (that is, the request URL in the browser's address bar).
We can use the href attribute in the <base> tag to set all the "relative base URLs".
What does that mean? Let's take a look at the code.
This is the JSP-side code
The following code (very similar to the JSP code in "method one" above)
But here we are not using the ${pagescope.basepath}+ "relative path Address" method,
Instead, the <base> tags in the HTML file are used:
The code is as follows:
<!--*************jsp code ******************-->
<%@ page language= "java" pageencoding= "GBK"
Contenttype= "TEXT/HTML;CHARSET=GBK" iselignored= "false"%>
<%
String path = Request.getcontextpath ();
Get the full path to the project (assuming that your project is called MyApp, then the address you get is
http://localhost:8080/MyApp/):
String basepath = request.getscheme () + "://" +request.getservername ()
+ ":" +request.getserverport () +path+ "/";
%>
<!--base needs to be put in the head--
<base href= "<%=basePath%>" >
Here we can directly use the relative path (i.e.: relative to the base tag)
<a href= "jsp/login.jsp" >login </a>
<!--*************************************-->
Probably read the above code, perhaps you still have some doubts *_*~~
However, when you see, the following code, it may be enlightened (*^__^*) hehe ....
When we go to execute the JSP code above, we can view it in the browser, he returns
HTML code to the client:
After executing the above JSP, the HTML code returned is as follows:
<base href= "http://localhost:8080/MyApp/" >
After <base> is set, the relative path is the path in base, not the browser
The address Request path ~ ~ ~
<a href= "jsp/login.jsp" >login </a>
We can see the HTML code returned by the JSP, which contains the <base
href= "http://localhost:8080/MyApp/" > Content.
That is, in this HTML file, all "relative links" (For example: <a
href= "jsp/login.jsp" >) ", are all relative to base
The path (ie: http://localhost:8080/MyApp/), so we can make use of relative
Link, without worrying,
Forwarding operation (forward) or the request address is different caused by the page can not find the error ~ (That is:
http:404) ...
Original address: Http://blog.csdn.net/liuboscu/archive/2009/08/23/4475520.aspx
BasePath and path in JSP (absolute path relative path)