Relative and absolute paths in JSP

Source: Internet
Author: User

1. First, two concepts are clarified:

 

Server path, for example, http: // 192.168.0.1/

Web Application Path, for example, http: // 192.168.0.1/yourwebapp path

 

2. Description of relative paths and absolute paths:

 

In servlet, "/" indicates the directory of the Web application. The relative representation of the physical path. For example, "./" indicates the current directory, and "../" indicates the upper-level directory. This similar representation is also a relative path. This is consistent with the path expression in Linux!

 

3. About the relative paths and absolute paths in JSP/servlet.

 

3.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 means the relative address in JSP and Servlet should be relative to your web application, that is, relative to http: // 192.168.0.1/webapp.

Where it is used:
Forward: request in servlet. getrequestdispatcher (Address); this address is resolved on the server side. Therefore, you must forward it 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: <% response. sendredirect ("/rtccp/user/a. jsp") in JSP; %>

 

3.2 client address

 

The relative addresses of all HTML pages are relative to the server root directory (http: // 192.168.0.1/), rather than (the directory of the Web application under the same directory) http: // 192.168.0.1/webapp. The address of the Form Action attribute in HTML should be relative to the server root directory (http: // 192.168.0.1. JSP: Action = "/webapp/user/. JSP "or action =" <% = request. getcontextpath () %> "/user/. JSP;
Submitting to servlet is actiom = "/webapp/handleservlet" javascript is also parsed on the client, so its relative path is the same as the form.

 

Therefore, in general, it is best to add CSS, JavaScript, action, and other attributes referenced in JSP/html pages.
<% = Request. getcontextpath () %> to make sure all referenced files belong to the directory in the Web application. In addition, try to avoid using similar ". ",". /",".. /.. /"and other similar relative paths relative to the file location, so that when the file is moved, it is easy to cause problems.

 

Simply put:

 

 

All paths request resources from the server. The difference is that the path is resolved on the server and on the client!

 

 

If the path you want to write is resolved on the server side, the root of the path should be in the form of http: // 192.168.0.1/webapp, that is, take the webapp path as the root path. For example, in the struts2 configuration of the oobbs system (the web application name is oobbs:

<Action name = "getforumbyid" class = "forumaction" method = "getforumbyid">
<Result name = "input">/</result>
<Result name = "success">/site/forumlist. jsp </result>
</Action>

The above path is:/site/forumlist. jsp instead of/oobbs/site/forumlist. jsp

 

 

 

If the written path is parsed on the clinet terminal, the root path is changed to http: // 192.168.0.1/, that is, the root path is the server path. At this time, if you want to request some background resources, such as JSP and action, you must start with webapp and write your path!

For example:

Form on the login page:

<Form action = "/oobbs/site/j_spring_security_check" method = "Post">

This path must start with/oobbs!

 

 

 

4. About<C: URL>

 

 

 

 

 

 

Related Article

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.