Ways to solve JSP path problems (JSP file at the beginning of path, BasePath effect)

Source: Internet
Author: User

Original: http://blog.csdn.net/mingxunzh/article/details/4627185

If you use relative paths in a JSP, you may have problems.

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 under 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 data content is returned from another page ...) )


So how do we solve this problem?

(i) method one: Direct use of absolute path (not recommended)  
on the JSP page, get the absolute address of the project (if your project is called MYAPP, the address you get is http://localhost:8080/MyApp/):  
code 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 (e.g.  http://localhost:8080/myapp/) assigned to basepath variable  
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; 
//Put "project path BasePath" into PageContext and later read it with El expression.  
Pagecontext.setattribute ("BasePath", basepath);  
%> 
< Head> <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 bad thing about this, which is that we have to add "${pagescope.basepath}" to each link in front of it.
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 the page (including the address of the style sheet, script, and image) are relative to the current page's address (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 &NBSP;
The following code (very similar to the JSP code in method one above)  
But here we are not using ${pagescope.basepath}+ "relative path Address" method,  The
instead uses the <base> tag in the HTML file:  
Code as follows:  
<!--*************jsp code ******************-->  
<%@ page language= "java" pageencoding= "GBK" contenttype= "TEXT/HTML;CHARSET=GBK" iselignored= "false"% > 
<% 
String path = Request.getcontextpath ();  
//Get the Project full path (assuming 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 placed in head--> 
<base href= "<%=basePath%>" >&NBSP;
// 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 through the JSP code above, we can see in the browser the HTML code that he returns to the client:
After executing the above JSP, the HTML code returned is as follows:
<base href= "http://localhost:8080/MyApp/" >
After setting the <base>, the relative path is the path in base, and no longer the request path of the browser address ~ ~ ~
<a href= "jsp/login.jsp" >login </a>

We can see the HTML code returned by the JSP, including the <base href= "http://localhost:8080/MyApp/" > Content.
That is, in this HTML file, all "relative links" (ex: <a href= "jsp/login.jsp" >) are encountered relative to the base
The path (ie: http://localhost:8080/MyApp/), so we can make use of relative links without worrying,
Forwarding operation (forward) or the request address is different caused by the page can not find the error ~ (ie: http:404) ...

Ways to solve JSP path problems (JSP file at the beginning of path, BasePath effect)

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.