Paths in Java Web projects, with relative and absolute paths
What is a relative path? A simple example
We have Project test post address http://localhost:8080/test/
Page qiantai/login.jsp page link <a href= "index.jsp"/>
The final address of the relative addressing link of the JSP page is http://localhost:8080/Test/qiantai/index.jsp
A relative path is a process that first takes the path to the current file, which is http://localhost:8080/test/qiantai/
Then splicing the link to the address index.jsp so the result is as above ... qiantai/index.jsp
In the CSS, JS reference to prevent background forwarding caused by the problem, should try to use the absolute path
The first scenario
JSP page comes with two path parameters path and BasePath
At the beginning of the page, there is this code
<%
String path = Request.getcontextpath (); Get current path such as http://localhost:8080/test/qiantai/
Get the address of this project such as http://localhost:8080/test/assignment to BasePath
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
Put the project path BasePath into the PageContext and read it later with the EL expression
Pagecontext.setattribute ("BasePath", basepath);
%>
Link instead <a href="${pagescope.basepath}qiantai/index.jsp">
Thus constituting an absolute path http://localhost:8080/test/qiantai/index.jsp
&NBSP; But there's a downside to this: we have to add in front of each link &NBSP, " ${pagescope.basepath} "&NBSP; &NBSP;&NBSP; &NBSP;
Second scenario
Using the base tag in HTML (recommended)
Introduction to Base :base element can specify the datum of all links in the page
URL 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). &NBSP, &NBSP;&NBSP;
We can use the href attribute in the <base> tag to set all the "relative base URLs".
<!-- base needs to be put in the head--
<BASE&NBSP; href= " http://localhost:8080/ test/" > or <base href=" <%=basePath%> "/>
This allows you to use the relative path directly <a href= "index.jsp"/>
after setting the <base>, the relative path is the path in base, and no longer the request path of the browser address ~ ~ ~
< Span style= "Background-color:rgb (255,255,255)" > We can see the HTML code returned by the JSP, including the <base href= " http://localhost:8080/ test/" > content
< Span style= "font-size:12px" > that is, all &NBSP encountered in this HTML file; " relative links (for example: <a href= " login.jsp " Span style= "word-wrap:normal; Word-break:normal; line-height:21px; Color:rgb (50,62,50); Font-family:simsun; font-size:14px ">>)", all relative to base
< Span style= "word-wrap:normal; Word-break:normal; line-height:21px; Color:rgb (50,62,50); Font-family:simsun "> So there's no need to worry about forwarding operations (forward) or errors caused by different request addresses (i.e.: http: Span style= "word-wrap:normal; Word-break:normal; line-height:21px; Color:rgb (50,62,50); Font-family:simsun; font-size:14px ">404 ) ...
can also be very good to avoid due to the forwarding of CSS or JS reference address error problem, that the path of the page is base-based.
Path problems in Java, JSP Web projects