The problem of absolute path and relative path in Javaweb project

Source: Internet
Author: User

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.