The absolute path and relative path of java_web development (learning notes) __java

Source: Internet
Author: User
Tags file url
First, forwarding: 1. Purpose of forwarding

is in order to achieve multiple page interaction in the process of request data sharing, that is, carry data forwarding, calculate a request, address bar address is not changed, is the server to do things, the browser is "in the Dark". 2. Implementation of forwarding

When using an absolute path, '/' represents a Web project

The URL in ①servletcontext.getrequestdispatcher (String URL) can only use absolute paths, because ServletRequest has a concept of relative paths; ServletContext objects have no concept.
'/' on behalf of HTTP://LOCALHOST:8080/project name/, want to skip to specified file URL so write
Request.sendredirect ("/") in the servlet;
'/' stands for http://localhost:8080/

Response.sendredirect (URL) jumps to the specified URL address to produce a new request, so the parameter to pass is only added after the URL, such as:
Url?id=1.
Request.getrequestdispatcher (URL). Forward (Request,response) forwards the request directly to the specified URL, so the request
Can directly obtain the data of the last request, that is, request forwarding, the requests object always exists and will not be recreated. and
Sendredirect () Creates a new request object, so the data in the previous request is lost.

Forward occurs inside the server and is response to another page of the browser without the knowledge of the browser at all. When the page received the request is not directly from the browser sent, may have been request.setattribute in the request to put the data. On the page you go to
The surface can obtain data directly with Request.getattribute.
The URL in the servletrequest.getrequestdispatcher (String URL) can use a relative path.

Here is an example from a website to help you understand later:
3.4.1. Absolute and relative paths
If we use URL URLs that start with "/", then this URL is called an absolute path.
If we use URL URLs that are not "/", then this URL is called a relative path.
3.4.1.1. Relative path
On the relative path, the performance of the two is the same.
Look at the example of lingo-sample/03-03/, if we go to ask for relative/forward.jsp or redirect.jsp, and then jump from here to the result/result.jsp below it.
Examples of forward:
<%request.getrequestdispatcher ("result/result.jsp"). Forward (request, response);%>

The relative path here is result/result.jsp.
Because the test.jsp just requested is under/03-03/relative/, so our current path is/03-03/relative/, and when we perform forward we will look for the result/under the current path. Result.jsp, then forward the request after it is found.
Examples of redirect:
<%response.sendredirect ("result/result.jsp");%>

The relative path here is also result/result.jsp.
Because the test.jsp you just requested is under/03-03/relative/, our current path is/03-03/relative/, and the current path will be added to the redirect when we execute it result/ result.jsp, the result is sent to the browser as a redirected address, and the browser then requests/03-03/relative/result/result.jsp to get a response.
3.4.1.2. Absolute path
Problems arise, the absolute path in forward and redirect in the difference, or just the case, but the use of absolute path when the wording will be different.
Examples of forward:
<%request.getrequestdispatcher ("/relative/result/result.jsp"). Forward (request, response);%>

The absolute path here is/relative/result/result.jsp.
In a local test, forward the http://localhost:8080/03-03/as the root path, calculating the absolute path on its basis.
This is determined by the way the JSP is deployed, and there are a number of projects in the WebApp that allow these projects to be run independently of each other, without the request being moved from one project directly to another within the server. To prevent this from happening, the path to the project is simply the root directory when the forward is executed, and the developer does not see the other items and there is no problem.
Examples of redirect:
<%response.sendredirect ("/03-03/absolute/result/result.jsp");%>

The absolute path here is/03-03/absolute/result/result.jsp.
In a local test, redirect the http://localhost:8080/as the root path, calculating the absolute path on its basis.
Because redirect will allow the browser to launch a new request, so it will not disrupt the relationship between multiple projects in the server, there is no need to limit it, if you need to jump between multiple projects, you can only use redirect. However, because of the new request, the last requested data will be lost, if there is any important data, remember to reset.
3.4.2. Forward caused no picture to be found
Cannot find the picture, cannot find the JS script, cannot find the CSS style sheet, all belongs to this question.
To demonstrate this problem, it is very easy to meet only two conditions:
The JSP page before and after forward is not in a directory.
The forward JSP page uses relative paths to refer to some resources, pictures, JS scripts, CSS stylesheets, and more.
In 03-04, you simulate an environment in which you enter http://localhost:8080/03-04/and choose "problematic":

Open 03-04 to see the following directory structure:
|–+ 03-04
|-index.jsp
|-test.jsp
|–+ result
|-success.jsp
|-failure.jsp
|-lingo.png

The page we saw just now is failure.jsp, and the part of it that shows the picture is:

At this time there is doubt, Lingo.png and failure.jsp clearly in the same directory, why can not display.
Now, on the picture that can't be displayed, click the right mouse button and select Properties, let's take a look at the request address of the picture:

The location of the picture was originally http://localhost:8080/03-04/result/lingo.png, but the requested address was http://localhost:8080/03-04/lingo.png. The problem is to lose the middle/result.
Try again on the index.jsp "No Problem":

This time we see the page is success.jsp, it shows the part of the picture is:

Results by hand and result this section of the path can be displayed after the picture.
This problem also goes back to the browser to the HTML processing, in the HTML contains pictures, CSS stylesheets, JS script, video and so on external resources, all need the browser to launch a request to the server again.
If these external resources use a relative path, the browser will send the complete HTTP request to the server on the basis of the current request path plus the relative path. In this example, we request http://localhost:8080/03-04/test.jsp, the current path to the browser is HTTP://LOCALHOST:8080/03-04/, Failure.jsp the relative path of the picture is lingo.png, then the result of stitching is http://localhost:8080/03-04/lingo.png.
Do not blame the browser is too stupid, because the use of forward when the browser is not aware of these changes. It has always been believed that since the request is test.jsp, the return of nature is test.jsp content, then use test.jsp as the current path to calculate the relative path of course there is no problem. Is that we cheated the browser, in the server secretly changed the request flow, returned the contents of other pages.
Clear the above request process, you know how to deal with this problem.
The first method: do not use forward between different directories to do request forwarding, to ensure that the current path does not change.
The second way: Modify the picture path as in the previous example, or change it all to an absolute path.
Please make a choice based on your actual needs.

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.