Absolute and relative addresses

Source: Internet
Author: User

In computer operation learning and Internet use, we often have access to the concept of absolute and relative addresses. Here, we will focus on the Internet. What are the differences between absolute and relative addresses when using websites?

Absolute address: An Independent address on the Internet. This address can be used on any website to directly access the target webpage. Contains the primary domain name and directory address.

Relative address: relative to the website address, when the domain name changes, the "absolute address" of the relative address also changes.

Assume that two websites A: www.seowhy.com B: www.baidu.com

Each of these two websites has a webpage under the root directory, for example, 404.html.

On these two websites, we make the same link/404.html (relative address)

Website a directs to www.seowhy.com/404.html

Website B points to www.baidu.com/404.html

If you want to direct/404.html on website a to website B, you need to write www.baidu.com/404.html (absolute address ).

The above is a general talk about the differences between the two. Recently, when I was studying javaweb, I learned more about the usage and differences between the two. I will explain the differences using specific examples below.

I believe everyone is familiar with request forwarding in servlet. I will list two forwarding methods below.

 

ServletContext context = getServletContext();RequestDispatcher dispatcher = context.getRequestDispatcher(path);dispatcher.forward(request,response);

 

Both methods achieve request forwarding and achieve the same effect ;. The difference between the two is that on the path, the former path must be an absolute path, and the latter path can be either an absolute path or a relative path. The absolute path is the path starting with "/", "/" indicates the URL entry of the current web application, and the relative path refers to the path relative to the current file, it does not start. The following is an example.

Examcodeservlet:

RequestDispatcher dispatcher = request.getRequestDispatcher("/register.jsp");dispatcher.forward(request, response);

The servlet above exists in a Java program under the mywebapp project. The absolute path is used above, And/Represents localhost: 8080/mywebapp. Therefore, requests are forwarded to localhost: 8080/mywebapp/register. jsp.

If you change the path to "register. jsp", this situation is different. Let's take a look at the configuration of examcodeservlet in Web. xml.

 

<servlet>    <description>This is the description of my J2EE component</description>    <display-name>This is the display name of my J2EE component</display-name>    <servlet-name>examCode</servlet-name>    <servlet-class>com.app.examCodeServlet</servlet-class></servlet><servlet-mapping>    <servlet-name>examCode</servlet-name>    <url-pattern>/examCode</url-pattern></servlet-mapping>

 

The addressing path of the URL of examcodeservlet adopts/examcode. As we have mentioned above, the usage of/will not be repeated here.

You can get the URL: localhost: 8080/mywebapp/examcode, and its physical path is D:/mywebapp/src/COM/APP/examcodeservlet. java, to clarify this problem, let's go back to the previous question and change the path to "register. JSP, first from the web. find the addressing path of the current servlet in XML. the addressing path is localhost: 8080/mywebapp/examcode, and the root directory is localhost: 8080/mywebapp. The actual path value is localhost: 8080/mywebapp/register. JSP. The meaning of relative paths should also be understood. It means that the Address URL is under the same directory.

Next I want to talk about the path problem in the redirection.

Response. sendredirect (PATH );

There is also an absolute and relative path problem, but it is a little different from the above request forwarding. Here, "/" also indicates the absolute path, but not the absolute path relative to the web application, but the URL relative to the root directory path of the current server, "/success. JSP refers to localhost: 8080/success. JSP, the use of relative paths is the same as request forwarding. There is also a path starting with "http: //", which represents a complete URL.

I don't know if you find that URLs without web pages all mean lower-layer content rather than using \. I think this is because \ is prone to escape character ambiguity. However, it is strange to say that the disk paths of our computers are represented by \. We can escape them \. Of course, you cannot use/to find the disk path. The system will also be automatically converted "\\".

 

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.