Client and server-side path problems and resource acquisition

Source: Internet
Author: User

Path

1 Path-related operations
----------------------------------------------------
Hypertext links
Form
Forward
Contains
redirect
<url-pattern>
ServletContext Getting Resources
Class Get Resources
ClassLoader Getting Resources

2 Client Path
----------------------------------------------------
hyperlinks, forms, and redirects are client paths, and the client path can be divided into three ways:
Absolute path;
A relative path beginning with "/";
A relative path that does not begin with "/";
For example: hyperlinks and forms in http://localhost:8080/hello1/pages/a.html are as follows:
Absolute path: <a href= "http://localhost:8080/hello2/index.html" > Links 1</a>
Client path: <a href= "/hello3/pages/index.html" > Links 2</a>
Relative path: <a href= "index.html" > Links 3</a>
Absolute path:
<form action= "http://localhost:8080/hello2/index.html" >
<input type= "Submit" value= "Form 1"/>
</form>
Client path:
<form action= "/hello2/index.html" >
<input type= "Submit" value= "Form 2"/>
</form>
Relative path:
<form action= "index.html" >
<input type= "Submit" value= "Form 3"/>
</form>
* Link 1 and Form 1: Nothing to say, it uses absolute paths;
* Link 2 and Form 2: Start with "/", relative to the host, and the current a.html of the same host, that is, the final access to the page is http://localhost:8080/hello2/index.html;
* Link 3 and Form 3: Do not start with "/", relative to the current page path, that is, a.html all paths, that is, the final access path is: http://localhost:8080/hello1/pages/index.html;

REDIRECT 1:
public class Aservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.sendredirect ("/hello/index.html");
}
}

Assume that the path to access Aservlet is: Http://localhost:8080/hello/servlet/AServlet
Because the path begins with "/", it is relative to the current host, which is http://localhost:8080/hello/index.html.

REDIRECT 2:
public class Aservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.sendredirect ("index.html");
}
}

Assume that the path to access Aservlet is: Http://localhost:8080/hello/servlet/AServlet
Because the path does not start with "/", it is relative to the current path, i.e. http://localhost:8080/hello/servlet/index.html

2.1 Recommended use of "/"
It is strongly recommended to use the path that begins with "/", which means that hyperlinks and forms in the page start with "/", followed by the name of the current app, and then the access path:
<form action= "/hello/servlet/aservlet" ></form>
<a href= "/hello/b.html" > Links </a>

Where/hello is the name of the current app, which also means that if the app name is modified in the future, then all paths in the page will have to be modified, which is really a problem. The solution to this problem will be introduced in the JSP!

Redirects in the servlet are also recommended to start with "/". In the same vein, give the name of the app! For example:
Response.sendredirect ("/hello/bservlet");

Where/hello is the name of the current application, and if the app name is modified in the future, the path to all redirects is also modified, and the solution to this problem is to use Request.getcontextpath () to get the app name.
Response.sendredirect (Request.getcontextpath () + "/bservlet");

3 Server-side path
----------------------------------------------------
The server-side path must be a relative path and cannot be an absolute path. However, there are two forms of relative paths:
* Start with "/";
* Do not start with "/";

Where request forwarding, request inclusion are server-side paths, the difference between the server-side path and the client path is:
* Client path begins with "/": relative to the current host;
* Server-side path begins with "/": relative to current application;

Forwarding 1:
public class Aservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Request.getrequestdispatcher ("/bservlet"). Forward (request, response);
}
}

Assume that the path to access Aservlet is: Http://localhost:8080/hello/servlet/AServlet
Because the path begins with "/", it is http://localhost:8080/hello/BServlet relative to the current application.

Forwarding 2:
public class Aservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Request.getrequestdispatcher ("Bservlet"). Forward (request, response);
}
}

Assume that the path to access Aservlet is: Http://localhost:8080/hello/servlet/AServlet
Because the path does not start with "/", it is relative to the current path, that is, Http://localhost:8080/hello/servlet/BServlet.

4 <url-pattern> Path
<url-pattern> must start with "/" and is relative to the current application.

5 ServletContext Getting Resources
Must be a relative path, can start with "/", or may not start with "/", but whether or not to start with "/" is relative to the current application path.
For example, to get a resource in Aservlet, the path path for Aservlet is: Http://localhost:8080/hello/servlet/AServlet:
public class Aservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String path1 = This.getservletcontext (). Getrealpath ("A.txt");
String path2 = This.getservletcontext (). Getrealpath ("/a.txt");
System.out.println (path1);
System.out.println (path2);
}
}

Path1 and path2 are the same result: Http://localhost:8080/hello/a.txt

6 Class Get Resources
The class fetch resource must also be a relative path, either beginning with a "/" or without "/".
Package cn.itcast;

Import Java.io.InputStream;

public class Demo {
public void Fun1 () {
InputStream in = Demo.class.getResourceAsStream ("/a.txt");
}

public void fun2 () {
InputStream in = Demo.class.getResourceAsStream ("A.txt");
}
}

where the Fun1 () method gets the resource with a "/" start, then the current application path, that is, the/hello/web-inf/classes/a.txt file;
where the Fun2 () method gets the resource without beginning with "/", then the path relative to the current Demo.class, because the demo class is under the Cn.itcast package, so the resource path is:/hello/web-inf/classes/cn/itcast/ A.txt.

7 ClassLoader Getting Resources
ClassLoader gets the resource must also be a relative path, either beginning with "/" or not using "/". However, the resource is relative to the current classpath, regardless of whether it starts with "/".
public class Demo {
public void Fun1 () {
InputStream in = Demo.class.getClassLoader (). getResourceAsStream ("/a.txt");
}

public void fun2 () {
InputStream in = Demo.class.getClassLoader (). getResourceAsStream ("A.txt");
}
}

The resources of the FUN1 () and fun2 () methods are relative classpath, that is, the classes directory, which is the/hello/web-inf/classes/a.txt

Client and server-side path problems and resource acquisition

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.