1. You can use String path = getServletContext().getRealPath("/") in the init method of the servlet ;
this will get the full path of the web project.
For example: E:\eclipseM9\workspace\tree\
tree is the root directory of my web project 2. You can also call this.getClass().getClassLoader().getResource("/").getPath()
in any class at any time ; this will get the full path of the classes directory. For example: E:\eclipseM9/workspace /tree/WEB-INF/classes/ This method can also determine the path without the web environment, it is better to use 3.request.getContextPath(); Get the context of the web root such as /tree tree is the root context of my web project / *jsp Get the path of the current directory path=request.getRealPath(""); /*Get the temporary directory released by jbossWEB warUrl=.../tmp/deploy/tmp14544test-exp.war/ path=C:\jboss-4.0.5 .GA\server\default\tmp\deploy\tmp14544test-exp.war\
String path = (String)request.getContextPath();
/*Get the real path where the project (test) application is located path=/test
String path = request.getRequestURI();
/*Get the real path where the application is located path=/ test/admin/admindex.jsp
String savePath=request.getRealPath(request.getServletPath());
/*Get the absolute disk path of the current file
//JAVA get the path of the current directory
File file=new File(".");
String path=file.getAbsolutePath();
path=file.getPath();
/*Get the jboss running directory path=C:\jboss-4.0.5.GA\bin\
-------------- --------------------------------
Java relative path/absolute path summary
classification: data column
1. The understanding of basic concepts is
absolutely Path: The absolute path is the real path of the file or directory on your homepage on the hard disk, (URL and physical path) For example:
C:xyz est.txt represents the absolute path of the test.txt file. http://www.sun.com/index.htm also represents an absolute URL path.
Relative path: A path relative to a base directory. Contains the relative path of the Web (relative directory in HTML), for example: in
Servlet, "/" represents the directory of the Web application. and the relative representation of the physical path. For example: "./" represents the current directory, "../" represents the parent directory. This similar representation is also a relative path.
In addition, for URI, URL, URN, etc., please refer to RFC related document standards.
RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax,
(http://www.ietf.org/rfc/rfc2396.txt)
2. About relative paths and absolute paths in JSP/Servlet.
2.1 Server-side address The
server-side relative address refers to the address relative to your web application, this address is resolved on the server side (different from the relative address in html and javascript, they are resolved by the client browser) Also That is to say, the relative addresses in jsp and servlet at this time should be relative to your web application, that is, relative to http://192.168.0.1/webapp/.
The places where it is used are:
forward: request.getRequestDispatcher(address) in servlet; this address is parsed on the server side, so if you want to forward to a.jsp, you should write: request.getRequestDispatcher("/user/a.jsp") this/relative For the current web application webapp, its absolute address is: http://192.168.0.1/webapp/user/a.jsp. sendRedirect: <%response.sendRedirect("/rtccp/user/a.jsp");%> in jsp
2.22, the client's address
The relative addresses in all html pages are relative to the server root directory (http:// 192.168.0.1/) instead of http://192.168.0.1/webapp/ (with the directory of the web application under the directory). The address of the action attribute of the form form in Html should be relative to the server root directory (http://192.168.0.1/), so if it is submitted to a.jsp: action="/webapp/user/a.jsp "or action="<%=request.getContextPath()% >"/user/a.jsp;
submit to servlet as action="/webapp/handleservlet" Javascript is also parsed on the client side, so its relative path and form Same.
Therefore, under normal circumstances, it is best to add CSS, Javascript.Action and other attributes that are referenced in JSP/HTML pages.
<%=request.getContextPath()%>, to ensure that the referenced files belong to the directory in the web application. In addition, you should try to avoid using relative paths such as ".", "./", "../../", etc. to the file location, so that when the file is moved, it is easy to cause problems.
3. Obtain the relative path and absolute path of the current application in JSP/Servlet
3.1 Obtain the relative path and absolute path of the current application in JSP
The absolute path corresponding to the root directory: request.getRequestURI()
The absolute path of the file: application.getRealPath(request .getRequestURI());
the absolute path of the current web application: application.getRealPath("/");
get the upper directory of the requested file: new File(application.getRealPath(request.getRequestURI())).getParent()
3.2 Servlet Obtain the relative path of the current application and the absolute path
corresponding to the absolute path root directory: request.getServletPath();
the absolute path of the file: request.getSession().getServletContext().getRealPath
(request.getRequestURI())
The current web application Absolute path: servletConfig.getServletContext().getRealPath("/");
(The ServletContext object can be obtained in several ways:
javax.servlet.http.HttpSession.getServletContext()
javax.servlet.jsp.PageContext.getServletContext()
javax.servlet.ServletConfig.getServletContext()
)
4. Methods of obtaining relative paths and absolute paths in the Class of 4.java
4.1 Separate Java The absolute path obtained in the class
According to the Doc file of java.io.File, it can be known that: by
default, the directory represented by new File("/") is: System.getProperty("user.dir").
The following 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("/"));
//The path where the Class file is located
System.out.println(new File("/").getAbsolutePath());
System.out.println(System.getProperty("user.dir"));
}
}
4.2 Java in the server The class gets the current path (from the network)
(1).
The root directory of the system file of Weblogic WebApplication is the root directory of your weblogic installation.
For example: if your weblogic is installed in c:beaweblogic700.....
then your file root path is c:.
So, there are two ways to allow you to access your server-side files:
a. Use an absolute path:
such as Put your parameter file in c:yourconfigyourconf.properties and
use new FileInputStream("yourconfig/yourconf.
b. Use relative path:
The root directory of the relative path is the root path of your webapplication, that is, the upper-level directory of WEB-INF, put your parameter file
in yourwebappyourconfigyourconf.properties,
and use it like this:
new FileInputStream("./yourconfig /yourconf.properties");
Both methods are available, you can choose by yourself.
(2).Tomcat
outputs System.getProperty("user.dir") in the class; it shows %Tomcat_Home%/bin
(3).Resin
is not the relative path of your JSP, it is the JSP engine that executes this JSP and compiles it into The path of SERVLET
is the root. For example, use the new file method to test File f = new File("a.htm");
this a.htm is in the installation directory of resin
(4). How to read the relative path?
In the Java file, getResource or getResourceAsStream can be used
as an example: getClass().getResourceAsStream(filePath);//filePath can be "/filename", where / represents the path where this method is used by default in
WEB-INF/classes under the web publishing root path
Yes: WEB-INF/classes. Already tested in Tomcat.
5. The relative path when reading the file, avoid the use of hard coding and absolute path. (from internet)
5.1 Use Spring's DI mechanism to obtain files and avoid hard coding.
Refer to the following link:
http://www.javajia.net/viewtopic.php?p=90213&
5.2 For reading the configuration file,
refer to the following link:
http://dev.csdn.net/develop/article/39 /39681.shtm
5.3 Read an xml file by virtual path or relative path, avoid hardcoding
the following link content:
http://club.gamvan.com/club/clubPage.jsp?iPage=1&tID=10708&ccID=8
6 Common operations of files in .Java (copy, move, delete, create, etc.) (from the Internet)
common java File operation classes
http://www.easydone.cn/014/200604022353065155.htm
Java file operations (in JSP)
http://www.easydone.cn/014/200604022353065155.htm ://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html
Detailed explanation of java file operation (Java Chinese website)
http://www.51cto.com/html/2005/1108/10947 .htm
JAVA how to create, delete, modify, copy directories and fileshttp:
//www.gamvan.com/developer/java/2005/2/264.htmlSummary
:
Through the use of the above content, operations such as moving files, finding files, copying
and deleting files can be solved on the web application server side, and the concepts of relative addresses and absolute addresses of the server are clearer.
It is recommended to refer to URI, the RFC standard document. At the same time, a thorough understanding of Java.io.File, Java.net.URI. and other content
can be more in-depth and thorough understanding of other aspects.
This is the way to go to the root directory of the current project in Java
java code /** *//**
* TODO get the root directory of the current project
* @author PHeH
* Created On 2006-12-30 17:15:41
*/
public class Application ...{
/** *//**
* TODO get root directory
* @return
* @author PHeH
* Created On 2007-5-10 15:16:21
*/
public static String getRootPath().. .{
//Because the class name is "Application", "Application.class" must be found
String result = Application.class.getResource("Application.class").toString();
int index = result.indexOf("WEB-INF");
if(index == -1)...{
index = result. indexOf("bin");
}
result = result.substring(0,index);
if(result.startsWith("jar"))...{
// When the class file is in the jar file, return "jar:file :/F:/ ..." kind of path
result = result.substring(10);
}else if(result.startsWith("file"))...{
// When the class file is in the class file, return "file:/F:/..." like path
result = result.substring(6);
}
if(result.endsWith("/"))result = result.substring(0,result.length()-1);//Does not include the last "/"
return result;
}
}
path problem in java
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325158878&siteId=291194637
Ranking