Directory structure:
------------------------------------------------------------------------------
First case: directly access JSP files
The URL is http: // localhost/Context path/jsp/index. jsp.
To use the go.gif file in index.jsp:
1. Use the correct path
/images/go.gif'/>
Search by the browser: domain name +/Context path/images/go.gif, which can be found.
2. Use relative path
Browser search method: Through the address bar analysis, the images/go.gif file under the previous directory (WebRoot) of index. jsp directory (jsp.
3. Use base href
Writing <% = request. getContextPath () %> is too troublesome. You can add the following code to the top of each jsp file:
<% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <base href = "<% = basePath %>">
Search by the browser: The basePath value is http: // localhost/Context path/, coupled with images/go.gif.
------------------------------------------------------------------------------
Case 2: servlet forwarding to jsp
1. Use relative path
The URL is http: // localhost/Context path/servlet_2 (forwarded to/jsp/index. jsp)
Error:
Based on the/jsp/index. jsp path calculation, obtain
Correct:
Cause:
Index. jsp is stored in the/jsp/index. jsp directory on the server, but the browser does not know the existence of the/jsp/directory after forwarding, because it is not shown in the address bar. Therefore, the server/jsp/directory does not affect the relative path.
Browser search method: Analyze http: // localhost/Context path/servlet_2 in the address bar, and find the images/go.gif file under the directory where servlet_2 is located (/).
2. Use relative path
The URL is http: // localhost/Context path/servlet/ser/servlet_1 (forwarded to/jsp/index. jsp)
"/Servlet/ser/servlet_1 is configured in the web. xml file.
Error:
Based on the/jsp/index. jsp path calculation, obtain
Correct:
Cause:
Index. jsp is stored in the/jsp/index. jsp directory on the server, but the browser does not know the existence of the/jsp/directory after forwarding, because it is not shown in the address bar. Therefore, the server/jsp/directory does not affect the relative path.
Browser search method: Analyze http: // localhost/Context path/servlet/ser/servlet_1 through the address bar, relative to the directory (ser) of servlet_1) the images/go.gif file under the previous directory (/)
3. Use the correct path
/images/go.gif'/>
------------------------------------------------------------------------------
Summary: The relative path is analyzed by the browser through the address bar and has no relationship with the storage path of the server-side files. It uses Servlet. After struts forwards the file to a jsp file, the location where a jsp is stored on the server is/a/B/c/d/f/g. jsp, but after Servlet and struts forwarding, the address bar of the browser may not necessarily be a/B/c/d/f. Therefore, the calculation of relative paths is based on the browser address bar.
In struts2, namespace can be used to ensure the consistency between the directory hierarchy in the browser's address bar and the directory hierarchy on the server side. In this way, programmers can calculate the relative path through the directory hierarchy on the server side, which is also normal in the browser.
However, we understand the principle that even if namespace is not used, we have strong control.