The absolute path and Relative Path Problems in JSP and Servlet have plagued me for several days. After some efforts, I will share some of my experiences with you.
Premise: assume that your HTTP address is http: // 192.168.0.1/your web application is webapp, then your web application URL is http: // 192.168.0.1/webapp/
Directory structure of Web applications:
Webapp/
Web-INF/
Classes/
LIB/
Web. xml
< Servlet-Mapping >
< Servlet-name > Handleservlet </ Servlet-name >
< URL-Pattern > /Handleservlet </ URL-Pattern > This ing is relative to the current Web Application
</ Servlet-Mapping >
User/
A. jsp
B. jsp
Images/
CSS/
JS/
All relative paths start. For example:/image/a.gif,/user/Main. jsp. We all know that the relative path in HTML is like this:
There is an HTML file: a.html, which contains < Link Href = "One.css" REL = "Stylesheet" Type = "Text/CSS" > The href attribute indicates the path of the referenced CSS file.
One.css: one.css and A. hmtl are in the same directory.
User/one.css: unzip one.css is used in the sub-directory user in the directory where a.html is located.
../One.css: one.css is located in the upper-level directory of A. hmtl,
Http://www.cnblogs.com/one.css: one.cssis stored in the hierarchical directory of a.hmtl level,
./: Indicates the same directory as A. hmtl.
We call the preceding relative path HTML relative path.
1. server address
The relative address on the server is relative to the address of your web application. This address is parsed on the server (different from the relative address in HTML and JavaScript, they are parsed by the client browser) That means the relative address in JSP and Servlet should be relative to your web application, that is, relative to http: // 192.168.0.1/webapp.
Where it is used:
Forwarder: request in servlet. getrequestdispatcher (Address); this address is resolved on the server, so you need to forwarder to. JSP should write this: request. getrequestdispatcher ("/user/. JSP ") the absolute address of this/relative to the current web application webapp is: http: // 192.168.0.1/webapp/user/. JSP.
All relative URLs in HTML are relative to http: // 192.168.0.1/, rather than http: // 192.168.0.1/webapp.
The address of the Form Action attribute in HTML should be relative to http: // 192.168.0.1/. Therefore, if the URL is submitted to. JSP: Action = "/webapp/user/. JSP "; submit to Servlet as action ="/webapp/handleservlet"
Javascript is also parsed on the client, so its relative path is the same as the form.
3. Website root directory and CSS Path Problems
We call it a relative path like this/webapp /.... Is the relative path relative to the site root directory.
When CSS is introduced in JSP, if its relative path is relative to the current JSP file, and the JSP is forwarder in a servlet that is different from the JSP path, the CSS style does not work at all. This is because when forwarding in servlet, the CSS path is relative to the Servlet Path rather than the JSP path. So this path cannot be used in JSP at this time: < Link Href = "One.css" REL = "Stylesheet" Type = "Text/CSS" > Or < Link Href = "Http://www.cnblogs.com/one.css" REL = "Stylesheet" Type = "Text/CSS" > HTML relative values similar to href = "one.css" and http://www.cnblogs.com/one.cssare relative values relative to the files that reference this cssfile. When forwarding in servlet, It is the relative path relative to this servlet. Because the JSP path is different from the Servlet Path, such a reference must be wrong.
in this case, the root directory of the site must start with "/" relative to the http: // 192.168.0.1/directory.
therefore, the above error should be corrected to the relative directory of the site root directory similar to href = "/webapp/one.css. In this way, after servlet forwarding and JSP are relative paths relative to the site root directory, the defined CSS style can be correctly used.
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.