The absolute path and Relative Path Problems in Servlet and JSP 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, where the href attribute indicates the path of the referenced css file. One.css: Your one.css and a. hmtl are in the same directory as user/one.css: Your one.css, where the sub-directory of a.html is located ....... /One.css: one.css is located in the upper-level directory of a. hmtl ,...... /...... /One.css: one.css is located in the upper-level directory of 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 is to say, the relative address in Servlet and JSP 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.
SendRedirect: in JSP
2. Client address
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
When the JSP forwarder in the Servlet with different paths is used, 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. Therefore, this path cannot be used in JSP at this time: or similar to href = "one.css" and ...... /...... The html relative path of/one.css is relative to the file that references this css. When forwarding in Servlet, It is the relative path relative to this Servlet. Because the Servlet and JSP paths are different, such a reference must be wrong.
Therefore, the site root directory 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 "/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.
- Rebuild Struts2 JSP pagination
- Three methods for displaying Chinese Characters in JSP Web programs
- Java JSP uses Google's Translate to develop APIs
- Detailed analysis of nine implicit JSP objects
- Introduction to JSP JavaBean development and release Environments