Java----Various ways to get paths (summary)

Source: Internet
Author: User

A summary of PATH problems in Java Web development (1) Some basic concepts of path in web development assume that the following page is accessed in the browser, as shown in 1:

  

Then several basic concepts for this site are expressed as follows:

1. Web site root directory : http://localhost:8080/

2. root directory of the Web application:http://localhost:8080/test/

3. Sibling Directories: http://localhost:8080/test/articles/article1.jsp and http://localhost:8080/test/articles/ Article2.jsp is in the same sibling directory .

Two Return values for several path-related functions:

Three path correlation functions were tested in the testurl.jsp file:

1. Getrequesturi ()

2. Getcontextpath ()

3. Getservletpath ()

As shown in result 3:

Three Path in Web. xml

The "/" in Servlet mapping <url-pattern>/xx</url-pattern> represents the root path of the current web App.

Just think about it and come to the conclusion that all of the Web. xml files describe the deployment-related information for a particular website, so "/" can only represent the root path of the current web app, not the root directory of the Web site.

Four Routing issues in forwarding and redirection

1. If there is no "/", such as Request.getrequestdispatcher ("hello.jsp"). Forward (request, response) and Response.sendredirect (" Hello.jsp ") means looking for the login.jsp file in the sibling directory.

2. If the path contains "/" (Note that "/" here refers to the first "/" in the path)

A, forward

such as Request.getrequestdispatcher ("/hello.jsp"). Forward (request, Response): Delegate to http://localhost:8080/test/ The following directory looks for the hello.jsp file.

B, redirect

such as Response.sendredirect ("/hello.jsp"): representative to http://localhost:8080/directory looking for hello.jsp file

Five <form action= "/xxx" > or <a href= "/xxx" ></a> Path

"/" represents the root path of the Web site:

Because hyperlinks can be linked to any desired target resource, the/Representative is definitely not the root path of the current web app, but the root path of the current Web site.

Six Path problem for ErrorPage property in page label

The value of the ErrorPage property, expressed as a "/", indicates relative to the current Web application's root directory, otherwise, represents the corresponding page.

The various ways in which Java obtains paths:

(1),Request.getrealpath ("/");//not recommended to get the root path of the project

(2),Request.getrealpath (Request.getrequesturi ());//Get the path of the JSP, this method is more useful, can be used directly in the servlet and JSP

(3),request.getsession (). Getservletcontext (). Getrealpath ("/");//Get the root path of the project, this method is more useful, can be used directly in the servlet and JSP

(4), This.getclass (). getClassLoader (). GetResource (""). GetPath ();//Get the path under Project classes, this method can be in any Jsp,servlet , which is used in Java files, because either Jsp,servlet is actually a Java program and is a class. So it should be a common method.

0. About absolute paths and relative paths

1, the basic concept of Understanding absolute path: absolute path is the real path on the hard disk of the file or directory on your home page, (URL and physical path) For example: C:xyz est.txt represents the absolute path of the Test.txt file. The http://www.sun.com/index.htm also represents a URL absolute path. Relative path: The path relative to a base directory. Contains the relative path to the web (relative to the directory in HTML), for example: In a servlet, "/" represents a directory of Web apps. and the relative representation of the physical path. For example: "./" represents the current directory, ". /"represents the parent directory. This similar representation also belongs to the relative path.

2.1 Server-side address server-side relative address refers to the address of your Web application, this address is resolved on the server side (different from HTML and JavaScript relative address, they are resolved by the client browser)

1, Request.getrealpath

Method: Request.getrealpath ("/")

Get the path: C:\Program files\apache software Foundation\tomcat 5.5\webapps\strutstest\

Method: Request.getrealpath (".")

Get the path: C:\Program files\apache software foundation\tomcat 5.5\webapps\strutstest\.

Method: Request.getrealpath ("")

Get the path: C:\Program files\apache software Foundation\tomcat 5.5\webapps\strutstest

Request.getrealpath ("Web. xml")

C:\Program Files\apache Software Foundation\tomcat 5.5\webapps\strutstest\web.xml

2, Request.getparameter ("");

Actionform.getmyfile ();

Method: String filepath = request.getparameter ("MyFile");

Get the path: D:\VSS installation directory \users.txt

Method: String filepath = Actionform.getmyfile ();

Get the path: D:\VSS installation directory \users.txt

--------------------------------------------------
Strutstest for Project name

MyFile in Actionform, for private String myFile;

In the JSP page: for

--------------------------------------------------

3. Get the system path

In the application:

System.getproperty ("User.dir")

In the servlet:

ServletContext ServletContext = Config.getservletcontext ();

String RootPath = Servletcontext.getrealpath ("/");

In the JSP:

Application.getrealpath ("")

4. Other 1

1. You can do this in the Init method of the servlet

String path = Getservletcontext (). Getrealpath ("/");

This will get the full path of the Web project

Example: E:\eclipseM9\workspace\tree\

Tree is the root directory of my Web project

2. You can also call in any class at any time.

This.getclass (). getClassLoader (). GetResource (""). GetPath (); After testing, this method is safe and the most effective

This.getclass (). GetResource ("/conf"). GetPath ();//Tested, this method is also safe

This will get the full path to the classes directory

Example:/d:/workspace/strutstest/webroot/web-inf/classes/

There are also This.getclass (). GetResource (""). GetPath (). toString ();

This will get to/d:/workspace/strutstest/webroot/web-inf/classes/bl/

This method can also not be used in the Web environment to determine the path, more useful

3.request.getcontextpath ();

Get the Web root context

such as/tree

Tree is the root context of my web project

5. Other 2

Several ways of obtaining paths in Java--

1. How does the JDK determine the path in the program? generally in programming, the file path is divided into relative paths and absolute paths, the absolute path is relatively good processing, but not flexible, so we in the programming of the file operation, is generally read the relative path of the file,

Relative paths can be a bit more complicated, but also relatively simple, relative to the path, mainly relative to who, can be the path of the ClassLoader, or the current Java file under the path, in JSP programming may be relative to the site's path, relative to the site's path, we can Getservletcontext (). Getrealpath ("\") and Request.getrealpath ("\"): This is the absolute path to get the site, and Getcontextpath (): Get the virtual path of the site;

2. Class.getClassLoader.getPath (): Get the path to the ClassLoader: What is the ClassLoader? The General class loader has the system and the user's own definition, the system ClassLoader is provided by the JDK, his path is the path under the JDK, or in JSP programming, such as Tomcat, the location of the class loader obtained is the path of Tomaca's own design loader.

After understanding these, the operation of the file path is quite clear, when we are programming, as long as we want to know that the file we are manipulating is relative to what path, the relative path can be achieved.

6. Summary

1. Get the file path under the Web server

Request.getrealpath ("/")
Application.getrealpath ("") "in JSP"
ServletContext (). Getrealpath ("")

System.getproperty ("User.dir") "called at different locations, gets the path is dynamically changing"

2. Get Local Path

JSP,

Request.getparameter ("MyFile");
Actionform.getmyfile ();

Get the same value: such as D:\VSS installation directory \users.txt

*********************************

This.getclass (). getClassLoader (). GetResource (""). GetPath ();
==/d:/workspace/strutstest/webroot/web-inf/classes/
This.getclass (). GetResource (""). GetPath (). toString ();
==/d:/workspace/strutstest/webroot/web-inf/classes/bl/

3. Get relative path

Request.getcontextpath ();

Other than that:

In a Java file, getresource or getresourceasstream can be

Example: GetClass (). getResourceAsStream (FilePath);//filepath can be "/filename", here/on behalf of the Web publishing root path web-inf/ Classes the path to use this method by default is: Web-inf/classes. has been tested in Tomcat.

Java----Various ways to get paths (summary)

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.