Recently in doing Javaweb projects, there are always a variety of path errors, and found that different situations/represent different meanings, resulting in the debugging path wasted a lot of time.
use absolute paths as much as possible in Javaweb projects because using an absolute path is absolutely not an error, and using a relative path may cause errors.
First of all, the meaning of absolute and relative paths in the Javaweb project
Absolute path: A path that is relative to the current Web application root path is that any path must be brought with ContextPath = http://localhost:8080/WebProject/
Relative path: Relative path to current directory is usually directly represented by/
Because the meaning of the / in Javaweb in a relative path differs in different contexts, problems may occur when using relative paths
The following analysis / in Javaweb meaning
The different meanings of the Javaweb project
The Javaweb project / may represent the following meaning
the root path (that is, the absolute path) of the current Web application http://localhost:8080/WebProject/
Have the following conditions
> Request Forwarding [Java] view plain copy request.getrequestdispatcher ("/message/a.jsp"). Forward (Request,response); Mapping servlet access paths in > Web.xml files
[Java] view plain copy <servlet-mapping> <servlet-name>UserServlet</servlet-name> & Lt;url-pattern>/userservlet</url-pattern> </servlet-mapping>
> in various custom labels
the root path of the current Web site http://localhost:8080/
Have the following conditions
> Hyperlinks [java] view plain copy <a href= "></a> > action in the Form [Java] view plain copy <form action= "" method= "POST" ></form> > Request Redirection
[Java] view plain copy response.sendredirect ("");
In combination with the above, we should use absolute paths in the development of Web projects, which should take ContextPath
And ContextPath can be obtained by Request.getcontextpath or Application.getcontextpath.
So when you encounter an action in a hyperlink form or request a redirect, you should precede it with contextpath instead of using the relative path directly/
For the sake of insurance, everyone in the project to appear any access path, such as CSS or JS code call path can use absolute path to prevent problems