Spring Read config file, address problem, absolute path, relative path

Source: Internet
Author: User
Tags rfc

Spring is relative to the bin, or Web-inf, when reading the configuration file;

"Applicationcontext.xml" is to find bin or Web-inf and sub-folder files;

"/res/applicationcontext.xml" is the file that must be bin or the Res folder under the Web-inf folder;//res can be replaced;

Here are some information for relative and absolute paths:

Transferred from: http://www.cnblogs.com/mabaishui/archive/2011/03/17/1987226.html

1. Understanding of Basic Concepts

absolute Path: The absolute path is the real path on the hard disk of the file or directory on your home page, (URL and physical path) such as:
also represents a
The 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,
)


2. About the relative path and absolute path in Jsp/servlet.

2.1 Server-side address

The server-side relative address refers to the address that is relative to your Web application, which is resolved on the server-side
(unlike the relative addresses in HTML and JavaScript, they are parsed by the client browser)
the.

where they are used are:
Forward:servlet in Request.getrequestdispatcher (address); This address is
in the server-side parsing, so you want to forward to a.jsp should be written like this:
request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp,
.
sendredirect: <%response.sendredirect ("/rtccp/user/a.jsp") in the JSP;%>

2.22, the address of the client
 
) of,
the.
) of,
so, if submitted to a.jsp as: action= "/webapp/user/a.jsp" or action= "<%=request.getcontextpath ()%>"/user/a.jsp;
submit to Servlet for actiom= "/webapp/handleservlet"
JavaScript is also parsed on the client side, so its relative path is the same as the form form.
 

therefore, in general, in the Jsp/html page and other references such as css,javascript.action attributes are best added
<%=request.getcontextpath ()%> To ensure that the referenced files belong to a directory in the Web App.
In addition, you should try to avoid using similar ".", "./", "http://www.cnblogs.com/" and other relative paths relative to the location of the file, so
When a file moves, it is prone to problems.


3. Get the relative and absolute paths of the current application in Jsp/servlet
obtaining the relative and absolute paths of the current application in the 3.1 JSP
absolute path for root directory: Request.getrequesturi ()
absolute path to file: Application.getrealpath (Request.getrequesturi ());
the absolute path of the current Web application: Application.getrealpath ("/");
gets the upper-level directory of the requested file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()

obtaining the relative and absolute paths of the current application in the 3.2 servlet
absolute path for root directory: Request.getservletpath ();
absolute path to file: Request.getsession (). Getservletcontext (). Getrealpath
(Request.getrequesturi ())
absolute path to the current web app: Servletconfig.getservletcontext (). Getrealpath ("/");
(ServletContext objects are available in several ways:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
     )

method of obtaining relative path, absolute path in 4.java class
4.1 Obtaining an absolute path in a separate Java class
according to Java.io.File's doc text, we know:
by default, the directory represented by new File ("/") is: System.getproperty ("User.dir").
The program gets the current path of the execution class
Package Org.cheng.file;
import Java.io.File;

Public class Filetest {
Public static void Main (string[] args) throws Exception {

System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource (""));

System.out.println (FileTest.class.getClassLoader (). GetResource (""));

System.out.println (Classloader.getsystemresource (""));
System.out.println (FileTest.class.getResource (""));
System.out.println (FileTest.class.getResource ("/"));//class file path
System.out.println (New File ("/"). GetAbsolutePath ());
System.out.println (System.getproperty ("User.dir"));
 }
}

4.2 Java classes in the server get the current path (from the network)
(1). Weblogic

the WebApplication System file root directory is the root directory where your WebLogic installation is located.
For example: If your WebLogic is installed in c:\bea\weblogic700 .....
So, your file root path is C: \.
so there are two ways to get you to access your server-side files:
A. Using an absolute path:
For example, put your parameter file in C:\yourconfig\yourconf.properties,
Direct use of the new FileInputStream ("Yourconfig/yourconf.properties");
B. Using relative paths:
The root of the relative path is the root path of your webapplication, which is the top-level directory of the Web-inf, placing your parameter file

in Yourwebapp\yourconfig\yourconf.properties,
This is used:
New FileInputStream ("./yourconfig/yourconf.properties");
Both of these methods can be chosen by themselves.

(2). Tomcat

output System.getproperty ("User.dir") in the class;%tomcat_home%/bin is displayed.

(3). Resin

is not the relative path that your JSP puts, it is the JSP engine that executes this JSP compiled into a servlet
the path to the root. For example, the new file method is used to test for file F = new ("a.htm");
This a.htm is under the resin installation directory.

(4). How to read the relative path?

in a Java file, getresource or getresourceasstream can be

Example: GetClass (). getResourceAsStream (FilePath);//filepath can be "/filename", here/on behalf of the Web

Publish root path under Web-inf/classes

The default path for using this method is: web-inf/classes. has been tested in Tomcat.

5. Relative path when reading files, avoid hard coding and absolute path use. (from the network)
5.1 Use spring's di mechanism to get the file and avoid hard coding.
5.2 Reading of configuration files
5.3 Read an XML file from a virtual path or relative path, avoiding hard-coded
common operations for files in 6.Java (copy, move, delete, create, etc.) (from the network)
Summary:
through the use of the above content can be resolved on the Web application server side, move files, find files, copy
Delete files and other operations, while the relative address of the server, the concept of absolute address clearer.
recommended reference URI, RFC standard text block. At the same time, Java.io.File. Java.net.URI and other content to understand thoroughly

C:\xyz\test.txt represents the absolute path to the Test.txt file. http://www.sun.com/index.htm (The relative address ofHttp://www.ietf.org/rfc/rfc2396.txt in JSP and servlet should be relative to your Web application, that is, relative to Http://192.168.0.1/webapp / Its absolute address is:http://192.168.0.1/webapp/user/a.jsp All the relative addresses in the HTML page are relative to the server root directory (http://192.168.0.1/ instead of (with the directory of the Web app under the directory) the address of the Action property of the Form form in thehttp://192.168.0.1/webapp/ HTML should be relative to the server root directory (http/ 192.168.0.1/ refer to the following connection content:
http://www.javajia.net/viewtopic.php?p=90213&
refer to the following connection content:
http://dev.csdn.net/develop/article/39/39681.shtm
refer to the following connection content:
http://club.gamvan.com/club/clubPage.jsp?iPage=1&tID=10708&ccID=8
 
Common Java File operation classes
http://www.easydone.cn/014/200604022353065155.htm
 
Java File Operations Encyclopedia (in JSP)
http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html

Java file Operation details (Java Chinese network)
http://www.51cto.com/html/2005/1108/10947.htm

how JAVA creates \ Deletes \ modifies \ Replicates directories and files
http://www.gamvan.com/developer/java/2005/2/264.html

Summary:
through the use of the above content can be resolved on the Web application server side, move files, find files, copy
Delete files and other operations, while the relative address of the server, the concept of absolute address clearer.
recommended reference URI, RFC standard text block. At the same time, Java.io.File. Java.net.URI and other content to understand thoroughly
The understanding of other aspects can be more thorough and thorough

Spring Read config file, address problem, absolute path, relative path

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.