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

Source: Internet
Author: User

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)
At the JSP page end, get the absolute address of this project (if your project is called MYAPP, then the address obtained is http://localhost:8080/MyApp/):
The code is as follows:

<!--************** method One ***************** -<%@ Page Language="Java"pageencoding="GBK"ContentType="TEXT/HTML;CHARSET=GBK"iselignored="false"%><%StringPath=Request.getcontextpath ();//get the address of this project (for example: http://localhost:8080/MyApp/assign a value to the basepath variableStringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";//will be"Project Path BasePath"put in the PageContext and read it later with the El expression. Pagecontext.setattribute ("BasePath", basepath);%><HTML><Head> </Head><Body><ahref= "${pagescope.basepath}jsp/register.jsp"></Body></HTML><!-- ************************************* - 

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
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"%><%StringPath=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/):StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><HTML><Head><!--base needs to be put in the head -<Basehref= "<%=basePath%>"></Head>//Here we can directly use relative path (i.e.: relative to base tag)<ahref= "jsp/login.jsp">Login</a></HTML><!-- ************************************* - 

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:

<HTML><Head><Basehref= "http://localhost:8080/MyApp/"></Head>//Set the<Base>The relative path is followed by the path in base, not the request path of the browser address .<ahref= "jsp/login.jsp">Login</a></HTML> 

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

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/liuboscu/archive/2009/08/23/4475520.aspx

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.