How to calculate the relative path of JSP
- 2020-06-19 11:31:25
- OfStack
Problem description:
To use images in /jsp/ index.jsp files, how do I calculate the relative path? How to calculate the relative path after Servlet and struts forwarding?
Directory structure:
Application name: Demo
demo
----webroot
----images
----go.gif
----jsp
----index.jsp
----css
------------------------------------------------------------------------------
Case 1: Direct access to the JSP file
URL http: / / localhost Context path/jsp/index jsp
To reference the go.gif file in ES51en.jsp:
1. Use the right path
< img src=' < %=request.getContextPath() % > /images/go.gif'/ >
Browser search method: domain name + / Context path/images/go gif, can be found.
2. Use relative paths
< img src='../images/go.gif'/ >
Browser search method: through the address bar analysis, index.jsp directory (WebRoot) under the directory above (WebRoot) images/ go.gif file.
3. base href
write < %=request.getContextPath() % > Too much trouble, you can add the following code at the top of each jsp file
< % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; % > < base href=" < %=basePath% > " >
< img src='images/go.gif'/ >
Browser to find way: the values of basePath http: / / localhost Context path/plus images/go gif, can be found.
------------------------------------------------------------------------------
Case 2: servlet forwards to jsp
1. Use relative paths
URL http: / / localhost Context path/servlet_2 (forwarded to/jsp/index. jsp)
Error:
According to /jsp/ index. jsp path calculation < img src='../images/go.gif'/ >
Correct:
< img src='images/go.gif'/ >
The reason:
index.jsp is saved on the server side under the /jsp/ index.jsp directory, but the browser does not know /jsp/ directory exists after forwarding, because it is not shown in the address bar. So the server /jsp/ directory has no effect on the relative path
Browser search way: by analyzing the address bar http: / / localhost Context path/servlet_2, relative to the servlet_2 directory (/) below to find images/go gif file
2. Use relative paths
URL http: / / localhost Context path servlet/ser ser/servlet_1 (forwarded to/jsp/index. jsp)
"/ servlet ser/ser/servlet_1 is in web xml configuration file
Error:
According to /jsp/ index. jsp path calculation, get < img src='../images/go.gif'/ >
Correct:
< img src='../../../images/go.gif'/ >
The reason:
index.jsp is saved on the server side under the /jsp/ index.jsp directory, but the browser does not know /jsp/ directory exists after forwarding because it is not shown in the address bar. So the server /jsp/ directory has no effect on the relative path
Browser search way: by analyzing the address bar http: / / localhost Context path servlet/ser/ser/servlet_1, relative to the directory with servlet_1 (ser) on the 1 on 1 layer on layer of directory directory 1 layer under the directory (/) images/go gif file
3. Use the right path
< img src=' < %=request.getContextPath() % > /images/go.gif'/ >
------------------------------------------------------------------------------
Conclusion: Relative path is passed by the browser address bar analysis, has nothing to do with the server file storage path, by its is to use Servlet, struts forwarded to a jsp file, stored on the server side a jsp location is/a/b/c d/f/g jsp, but after Servlet struts forwarding, The browser's address bar is not 1 set/a b/c/d f/level. So the relative path calculation is subject to the browser address bar.
struts2 can use namespaces to ensure that the directory level in the browser address bar is 1 compatible with the directory level on the server side, so it is normal for programmers to calculate relative paths through the directory level on the server side in the browser.
But we understand the principle that even if we don't use namespaces, we have a lot of control.
To use images in /jsp/ index.jsp files, how do I calculate the relative path? How to calculate the relative path after Servlet and struts forwarding?
Directory structure:
Application name: Demo
demo
----webroot
----images
----go.gif
----jsp
----index.jsp
----css
------------------------------------------------------------------------------
Case 1: Direct access to the JSP file
URL http: / / localhost Context path/jsp/index jsp
To reference the go.gif file in ES51en.jsp:
1. Use the right path
< img src=' < %=request.getContextPath() % > /images/go.gif'/ >
Browser search method: domain name + / Context path/images/go gif, can be found.
2. Use relative paths
< img src='../images/go.gif'/ >
Browser search method: through the address bar analysis, index.jsp directory (WebRoot) under the directory above (WebRoot) images/ go.gif file.
3. base href
write < %=request.getContextPath() % > Too much trouble, you can add the following code at the top of each jsp file
< % String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; % > < base href=" < %=basePath% > " >
< img src='images/go.gif'/ >
Browser to find way: the values of basePath http: / / localhost Context path/plus images/go gif, can be found.
------------------------------------------------------------------------------
Case 2: servlet forwards to jsp
1. Use relative paths
URL http: / / localhost Context path/servlet_2 (forwarded to/jsp/index. jsp)
Error:
According to /jsp/ index. jsp path calculation < img src='../images/go.gif'/ >
Correct:
< img src='images/go.gif'/ >
The reason:
index.jsp is saved on the server side under the /jsp/ index.jsp directory, but the browser does not know /jsp/ directory exists after forwarding, because it is not shown in the address bar. So the server /jsp/ directory has no effect on the relative path
Browser search way: by analyzing the address bar http: / / localhost Context path/servlet_2, relative to the servlet_2 directory (/) below to find images/go gif file
2. Use relative paths
URL http: / / localhost Context path servlet/ser ser/servlet_1 (forwarded to/jsp/index. jsp)
"/ servlet ser/ser/servlet_1 is in web xml configuration file
Error:
According to /jsp/ index. jsp path calculation, get < img src='../images/go.gif'/ >
Correct:
< img src='../../../images/go.gif'/ >
The reason:
index.jsp is saved on the server side under the /jsp/ index.jsp directory, but the browser does not know /jsp/ directory exists after forwarding because it is not shown in the address bar. So the server /jsp/ directory has no effect on the relative path
Browser search way: by analyzing the address bar http: / / localhost Context path servlet/ser/ser/servlet_1, relative to the directory with servlet_1 (ser) on the 1 on 1 layer on layer of directory directory 1 layer under the directory (/) images/go gif file
3. Use the right path
< img src=' < %=request.getContextPath() % > /images/go.gif'/ >
------------------------------------------------------------------------------
Conclusion: Relative path is passed by the browser address bar analysis, has nothing to do with the server file storage path, by its is to use Servlet, struts forwarded to a jsp file, stored on the server side a jsp location is/a/b/c d/f/g jsp, but after Servlet struts forwarding, The browser's address bar is not 1 set/a b/c/d f/level. So the relative path calculation is subject to the browser address bar.
struts2 can use namespaces to ensure that the directory level in the browser address bar is 1 compatible with the directory level on the server side, so it is normal for programmers to calculate relative paths through the directory level on the server side in the browser.
But we understand the principle that even if we don't use namespaces, we have a lot of control.