Relative path, absolute path, and servlet mapping path

Source: Internet
Author: User
Tags vcard

When the front and back of the integration, often there will be inconsistent path or path error, the result is that the CSS is invalid, servlet or JSP page map is not.

So deliberately write a summary of the path settings:

This only concerns the path of the Web application, as the path to the local system is not discussed.

1. The road strength may divide into the absolute path and the relative path

2. Absolute path (beginning with "/")

Front End: http://localhost:8080/myWebApp/user/login.jsp

/mywebapp/user/login.jsp

Back end:/user/login.jsp

Here we can see a difference where the "/" slash has different meanings at the front and back ends:

Front-End--> (represents) the root path of the server (for example: http://localhost:8080/)

Back-end--> (representing) the root path of the application (for example: http://localhost:8080/yourWebAppName/)

3. Relative path (beginning without "/")

For instance, if the current path is http://localhost:8080/myWebApp/user/login.jsp

So this time to visit the "main.jsp" page, that is to visit the http://localhost:8080/myWebApp/user/main.jsp

In other words, using a relative path, you are accessing the file path under the directory of the current page path.

This feature is the same both at the front and back end.

In Java, either a JSP or a servlet, you can get Requesturl () from the Request object, which returns a StringBuilder of the absolute path of the page you are currently accessing.

It is through this path that the application server comes to the page path that the user wants to access.

In general, when using forward, a new path is requested by modifying the path, regardless of whether it is currently specified as an absolute or relative path.

4. Related APIs for obtaining path information in Jsp/servlet

Below is a server that uses jetty as the underlying. There is a servlet mapped to/plugins

The page we visited is: http://localhost:9090/plugins/maidatabase/user-vCard-view.jsp

The following is a call to the relevant API and the results obtained

The Base Path (scheme+servername+serverport+requestcontext): http://localhost:9090//application path

Request.reqeusturl (): http://localhost:9090/plugins/maidatabase/user-vCard-view.jsp//Request Path
Request.requesturi ():/plugins/maidatabase/user-vcard-view.jsp//request path (relative to server root portion)

Request.getSession.getServletContext (). Getrealpath (""): E:\projects\openfire_src\target\openfire\plugins\admin\ True path of WebApp//root directory
Request.getsession (). Getservletcontext (). GetResource ("/"). ToString (): file:/e:/projects/openfire_src/target/ openfire/plugins/admin/webapp///Here must be '/' Start

Request.getservletpath (): Mapping path for/plugins//servlet

The ServletContext serverinfo:jetty/7.0.1.v20091125//server information
Request.pathinfo (): Additional path information between the servlet path and the query data in the/MAIDATABASE/USER-VCARD-VIEW.JSP//request Path

Servletcontext.getcontextpath ()://Apply context path (standard)
Request.getcontextpath ()://Request requests the application context path pointed to is generally the application path, that is,/yourwebappname (because the scenario here is the entire application server as an application, so the application context is "")

5. Attached to someone else's article

This article is well summed up and concise:
1. <form action= "abc.do" method= "POST" >*********</form&gt, and the access address of the page is Http://localhost:8080/yaso/login
<form action= "/abc.do" method= "POST" >*********</form&gt, and the access address for the page is Http://localhost:8080/yaso/login
Here you can use a relative path or an absolute path, where the relative path is relative to the currently accessed page. When the form is submitted, the information in the browser's address bar changes to Http://localhost:8080/yaso/abc.do
If an absolute path is used, then "/" refers to the root of the server rather than the root of the application. If you change the above form to action= "/abc.do," when the form is submitted, the information in the browser's address bar becomes http://localhost:8080/abc.do
You can use the Getcontextpath () method if you want to use an absolute path.

2. The <url-pattern> tag in the <servlet-mapping> in Web.xml can only use an absolute path, that is, you must start with "/". It can be understood that an application corresponds to a web.xml, so the absolute path here represents the root of the application, not the root of the server. (A special case is the *.do that uses pattern matching in <url-pattern>, but should still be understood as starting from/.) )

3.RequestDispatcher view = Request.getrequestdispatcher ("/abc.jsp");
RequestDispatcher view = Request.getrequestdispatcher ("abc.jsp");
Here you can use relative and absolute paths. A relative path is the path that appears on the address bar when you execute to the servlet where the code resides.
If an absolute path is used, then "/" represents the root of the application.

4.RequestDispatcher view = Getservletcontext (). Getrequestdispatcher ("/abc.jsp");
You must use an absolute path here, "/" to represent the root of the application. It can be understood that the request forwarding on the ServletContext, if the use of relative path, you can not know exactly what position relative to the application, can only be judged by the absolute path. Because it is a method invoked on the application context, "/" represents the root of the application.

The getResourceAsStream (String) in 5.servletContext, where the argument should be an absolute path beginning with "/", where "/" represents the root of the application.

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.