The way to solve JSP path problem (JSP file start path, basePath function)
If you use the “relative path” in JSP, there may be problems.
Because of the relative path in web pages, he is looking for resources relative to the address of “URL request”.
What does this sentence mean?
For example:
Suppose we have a project: MyApp
Under this project, there is a JSP folder.
The folder includes:
login.jsp // Landing page
register.jps // Registration page
We enter the address in the browser (Note: content of the address):
http://localhost:8080/MyApp/jsp/login.jsp
At this point, browsers will link to “landing page” (login.jsp).
The following part of the code is included in the login.jsp file:
<a href=”jsp/register.jsp”>Registered users < /a>
Then, if we click this link, we will see the following error links in the browser address bar:
http://localhost:8080/MyApp/jsp/jsp/register.jsp
Look at ~ ~ ~
Why is there a “/jsp/jsp/register.jsp”?
Because the relative link in web pages is determined by the URL path you request.
That is:
Because the request path here is: http://localhost:8080/MyApp/jsp/login.jsp
Then the browser will find JSP / register. JSP in this path (http://localhost:8080/MyApp/jsp/)
So there will be the following mistakes:
http://localhost:8080/MyApp/jsp/jsp/register.jsp
The above problem is caused by the difference between the calling page and the URL of the called page.
Such errors often occur when forward is operated between 2 pages.
Because forward is in the background, and it is transparent to the client. That is, URL does not change, and data content is returned by another page. )
So how to solve this problem?
(1) method 1: direct use of absolute path (not recommended)
On the JSP page side, get the absolute address of the project (if your project is called MyApp, the address you get is http://localhost:8080/MyApp/):
The code is as follows:
<!– **************Methods: one * * * * * * * * * * * * * * * * * –>
<%@ page language=”Java” pageEncoding=”GBK” contentType=”text/html;charset=gbk” isELIgnored=”false”%>
<%
String path = request.getContextPath();
// Get the address of this project (for example: http://localhost:8080/MyApp/) assign to basePath variable.
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
// Put “project path basePath” in pageContext and read it later with EL expression.
pageContext.setAttribute(“basePath”,basePath);
%>
<html>
<head> </head>
<body>
<a href=”${pageScope.basePath}jsp/register.jsp”>
</body>
</html>
<!– *************************************–>
We can see that in the href attribute of tag < a>, we have adopted it directly.
“The project path ${pageScope.basePath} “plus” jsp/register.jsp “.
Thus forming an absolute path (i.e.: http://localhost:8080/MyApp/jsp/register.jsp).
But one bad thing about doing this is that we have to add “${pageScope. basePath}” before each link.
If this is done, it will be a terrible thing.
(Two) method two: use <, base>, tags in HTML (recommended)
The following is an introduction to <, base> in HTML:
base The element can specify the benchmark URL for all links in the page.
By default, the links in the page (including the addresses of the stylesheet, script, and image) are relative to the address of the current page (that is, the request URL in the browser’s address bar).
We can use <, base>, href attribute in the tag to set all the “relative benchmark URL”.
What does that mean? Let’s take a look at the code.
This is the code for the JSP side.
The following code is very similar to the JSP code in method 1 above.
But here we do not adopt the ${pageScope.basePath}+ “relative path address” method.
Instead, we use the <, base> in the HTML file;
The code is as follows:
<!– *************JSPCode ******************–>
<%@ page language=”java” pageEncoding=”GBK” contentType=”text/html;charset=gbk” isELIgnored=”false”%>
<%
String path = request.getContextPath();
// Get the full path of the project (assuming your project is called MyApp, the address you get is http://localhost:8080/MyApp/):
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%>
<html>
<head>
<!– baseYou need to put –> in head.
<base href=” <%=basePath%>”>
</head>
// Here we can directly use the relative path (that is, relative to the base tag).
<a href=”jsp/login.jsp”>Login </a>
</html>
<!– *************************************–>
After reading the above code, maybe you still have some doubts.
But when you see it, the following code may suddenly become clear.
When we execute the above JSP code, we can see it in the browser, and the HTML code that he returns to the client:
After executing the above JSP, the HTML code returned is as follows:
<html>
<head>
<base href=”http://localhost:8080/MyApp/”>
</head>
// With & lt; base & gt; set, the relative path is the path in base, not the request path for the browser address.
<a href=”jsp/login.jsp”>Login </a>
</html>
We can see that the HTML code returned by the JSP contains & lt; base href = “http://localhost:8080/MyApp/” & gt; content.
That is, in this HTML file, all “relative links” (e.g. & lt; a href = “jsp / login. jsp” & gt;) encountered are relative to base
The path (http://localhost:8080/MyApp/), so we can use relative links without worrying.
Errors that cannot be found on pages due to forward operations or different request addresses ~ (HTTP: 404).