Java Get path Method & relative path read XML file method

Source: Internet
Author: User
Tags rfc

(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 the project classes, this method can be in any jsp,servlet, 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. For additional information about Uri,url,urn, please refer to RFC documentation standards. RFC 2396:uniform Resource Identifiers (URI): Generic Syntax, (http://www.ietf.org/rfc/rfc2396.txt) 2. About jsp/ The relative path and absolute path in the servlet. 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 ();

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 ();

such as:/strutstest

Java reads XML files using relative paths
There are three general storage locations for XML files:
1. Place under the Web-inf;
2.xml files are placed in the/web-inf/classes directory or in the Classpath jar package;
3. In a package that is similar to the Java parsing it, it is not necessarily classpath;

Two, corresponding to the use of the relative path of the Read method:

Method One: (not verified)
Place the XML file in the Web-inf directory, and then
Program code:
InputStream Is=getservletcontext (). getResourceAsStream ("/web-inf/xmlfile.xml");

Method Two: Put the XML file in the/web-inf/classes directory or classpath jar package, you can use ClassLoader static method Getsystemresourceasstream (String s) read;
Program code:
String s_xmlpath= "Com\xml\hotspot.xml";
InputStream In=classloader.getsystemresourceasstream (S_xmlpath);

Method Three: XML at random under a package path:
String s_xmlpath= "Com\xml\hotspot.xml";
ClassLoader Classloader=hotspotxmlparser.class.getclassloader ();
InputStream In=classloader.getresourceasstream (S_xmlpath);
reprinted from: http://itfafa.iteye.com/blog/1485192 http://www.cnblogs.com/rongxh7/archive/2010/05/01/1725447.html

Java Get path Method & relative path read XML file method

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.