Relative path, absolute path, deployment path (html/css/servlet/jsp)

Source: Internet
Author: User
Tags vcard

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

So deliberately write a summary of the path settings:

This involves only the path problem of the Web application, and the path of the local system is not discussed.

1. Road strength can be divided into absolute path and 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 you can see a difference in which the "/" slash has a different meaning in the front and back:

  The root path of the server (for example: http://localhost:8080/)

Backend---(for example: http://localhost:8080/yourWebAppName/) of the app's root path

3. Relative path (without "/" at the beginning)

As an example, if the current path is http://localhost:8080/myWebApp/user/login.jsp

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

That is, using a relative path, the path to the file under the directory where the current page path belongs is accessed .

  This feature is the same on both the front and back ends.

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 (whether it is the absolute path or relative path currently specified).

4. Related APIs for access to path information in Jsp/servlet

Below is a server using 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

Here is the call to the relevant API and the resulting results

 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 section)

Request.getSession.getServletContext (). Getrealpath (""): True path to E:\projects\openfire_src\target\openfire\plugins\admin\webapp//root directory
Request.getsession ( ). Getservletcontext (). GetResource ("/"). ToString (): File:/e:/projects/openfire_src/target/openfire/plugins/admin /webapp///must begin with '/'

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

The ServletContext serverinfo : jetty/7.0.1.v20091125//server information
Request.pathinfo ():/maidatabase/user-vcard-view.jsp//In the request path between the servlet path and the query data Additional path information

Servletcontext.getcontextpath ()://Apply context path (standard)
Request.getcontextpath ()://Application context to which request is directed /yourwebappname (because the scenario here is to use the entire application server as an application, 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" &GT;*********&LT;/FORM&GT; and the access address of the page is Http://localhost:8080/yaso/login
<form action= "/abc.do" method= "post" &GT;*********&LT;/FORM&GT; and the access address of the page is Http://localhost:8080/yaso/login
Relative paths or absolute paths can be used here, where relative paths are relative to the currently visited page. When the form is submitted, the information in the browser's address bar becomes http://localhost:8080/yaso/abc.do
If an absolute path is used, "/" refers to the root of the server rather than the root of the application. If you change the above form to action= "/abc.do", the information in the browser address bar becomes http://localhost:8080/abc.do when the form is submitted
You can use the Getcontextpath () method if you want to use an absolute path.

2. Only absolute paths can be used in <url-pattern> tags in <servlet-mapping> in Web. XML, 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 use of pattern-matching *.do in <url-pattern>, but it should still be understood as starting/starting.) )

3.RequestDispatcher view = Request.getrequestdispatcher ("/abc.jsp");
RequestDispatcher view = Request.getrequestdispatcher ("abc.jsp");
Relative paths and absolute paths can be used here. A relative path is the path displayed on the address bar when executing to the servlet where the code resides.
If an absolute path is used, then "/" represents the root of the app.

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

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

Relative path, absolute path, deployment path (html/css/servlet/jsp)

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.