This blog is a comprehensive summary of Javaweb application server-side access to file paths in different environments.
Get the file path after the main application scenario, read the Javaweb custom profile, generate various types of files under a specific path to provide downloads ...
Presumably crossing is also to find ways, first look at the above directory there is no solution to your problem, if there is a click into the corresponding section, if there is no speed off, look at the search engine listed next record bar.
the 1.Servlet init method gets
Getservletcontext (). Getrealpath ("/");
Output:e:\workspace\tree\ (tree is the root directory of my Web project)
2. Get in any class
this. getclass (). getClassLoader (). GetResource ("/"). GetPath (); Thread.CurrentThread (). Getcontextclassloader (). GetResource ("/"). GetPath ();
Output:e:\workspace\tree\web-inf\classes\
Get in 3.HttpServletRequest
Request.getservletcontext (). Getrealpath ("/");
Output:e:\workspaces\tree\webcontent\
4.JSP Get the path to the current directory
Request.getrealpath ("");
Get Jbossweb Release Temp directory warurl=.../tmp/deploy/tmp14544test-exp.war/
Path=c:\jboss-4.0.5.ga\server\default\tmp\deploy\tmp14544test-exp.war\
(String) Request.getcontextpath ();
Get the real path to the project (test) application Path=/test
Request.getrequesturi ();
Get the real path where the app is located path=/test/admin/admindex.jsp
Request.getrealpath (Request.getservletpath ());
Get the absolute disk path of the current file
New File ("."). GetPath ();
Get JBoss Run Directory path=c:\jboss-4.0.5.ga\bin\
5. Server-side Address "server-side view Client"
The server-side relative address refers to the address that is relative to your web app. This address is parsed on the server side (different from the relative addresses in HTML and JavaScript, they are parsed by the client browser)
In other words, the relative addresses in the JSP and servlet should be relative to your Web application, that is, relative to http://192.168.0.1/webapp/.
eg
Forward:servlet in Request.getrequestdispatcher (address); This address is parsed on the server side.
You have to forward to a.jsp should write: Request.getrequestdispatcher ("/user/a.jsp")
This/relative to 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 the JSP;%>
6. Client's address "Client View server Side"
The relative addresses in all the HTML pages are relative to the root of the server (HTTP://192.168.0.1/), not http://192.168.0.1/webapp/(with the directory of the Web app under the directory).
The address of the Action property of the form form in HTML should be relative to the server root directory (HTTP://192.168.0.1/).
If submitted to a.jsp as: action= "/webapp/user/a.jsp" or action= "<%=request.getcontextpath ()% >"/user/a.jsp;
The actiom= "/webapp/handleservlet" JavaScript that is submitted to the servlet is also parsed on the client, so its relative path is the same as the form form.
Therefore, in general, in the Jsp/html page, such as reference to the css,javascript.action and other attributes are preferably preceded by <%=request.getcontextpath ()%> To ensure that the referenced files belong to a directory in your Web app.
In addition, you should try to avoid the use of similar ".", "./", ". /.. /"Similar relative path relative to the location of the file, so that when the file is moved, it is prone to problems.
get the relative and absolute paths of the current app in 7.Servlet
A. Absolute path for root directory: Request.getservletpath ();
B. Absolute path to file: Request.getsession (). Getservletcontext (). Getrealpath (Request.getrequesturi ())
C. Absolute path to the current web app: Servletconfig.getservletcontext (). Getrealpath ("/");
Javax.servlet.http.HttpSession.getServletContext () Javax.servlet.jsp.PageContext.getServletContext () Javax.servlet.ServletConfig.getServletContext ()
D.. Method of obtaining relative path, absolute path in 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").
System.out.println (Thread.CurrentThread (). Getcontextclassloader (). GetResource ("")); System.out.println (Test.classgetClassLoader (). GetResource ("")); System.out.println (Classloader.getsystemresource ("")); System.out.println (Test.classGetResource ("")); System.out.println (Test.classGetResource ("/")); System.out.println (new File ("/"). GetAbsolutePath ()); System.out.println (System.getproperty ("User.dir"));
the Java class in the 8.Web server obtains the current path
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:beaweblogic700 .....
So, your file root path is C:
So there are two ways to get you to access your server-side files.
use absolute path: For example, put your parameter file in C:yourconfigyourconf.properties.
New FileInputStream ("Yourconfig/yourconf.properties");
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 Yourwebappyourconfigyourconf.properties,
New FileInputStream ("./yourconfig/yourconf.properties");
B.tomcat
Output System.getproperty ("User.dir") in the class;%tomcat_home%/bin is displayed.
C.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.
Methods for fetching the root directory of the current project in Java
//because the class name is "Application", "Application.class" must be able to findString result = Application.class. GetResource ("Application.class"). toString (); intindex = Result.indexof ("Web-inf"); if(Index = =-1)... {Index= Result.indexof ("Bin"); } result= result.substring (0, index); if(Result.startswith ("Jar"))... { //returns the path of "jar:file:/f:/..." When the class file is in the jar fileresult = Result.substring (10); }Else if(Result.startswith ("File"))... { //returns the path of "file:/f:/..." When the class file is in the class fileresult = Result.substring (6); } if(Result.endswith ("/")) result = Result.substring (0,result.length ()-1);//does not contain the last "/"returnResult
9.JavaScript get the project root directory
//JS Gets the project root path, such as:http://localhost: 8083/uimcardprjfunction Getrootpath () {//get the current URL, such as:http://localhost: 8083/uimcardprj/share/meun.jspvar curwwwpath=Window.document.location.href; //gets the directory after the host address, such as: uimcardprj/share/meun.jspvar pathname=Window.document.location.pathname; var pos=Curwwwpath.indexof (pathName); //get the host address, such as:http://localhost: 8083var localhostpaht=curwwwpath.substring (0, POS); //get the project name with "/", such as:/uimcardprjvar projectname=pathname.substring (0,PATHNAME.SUBSTR (1). IndexOf ('/') +1); return(localhostpaht+projectName);}
10. A little comprehension of classloader
The getClassLoader () is used in any class: the class loader that obtains the class object, the knowledge of ClassLoader is built up after contact in the follow-up work.
The process of loading a class is very simple: find the location of the class and load the bytecode of the found Java class into memory to generate the corresponding class object.
Java's class loader is designed to implement a process where the JVM has more than one class loader, and in fact, you can have countless class loaders for the JVM if you want.
The class loader itself is also a class, it also needs to be loaded into memory, then the class loader by who load it, there must be a root it?
There really is such a root, it is stays bootstrap ClassLoader. Why do you say it stays?
Because you can't even catch a little bit of its tail in Java code,
Although you can always appreciate its existence, because Java's running environment requires all the class library, it is loaded by it, and it is written in C + + program, can run independently, can be said to be the starting point of the JVM run, tremble, SAO years!
After Bootstrap completes its task, it generates a appclassloader (in fact, the system also uses the Extension class loader extclassloader, which is used to load the classes in the Java Run Environment expansion pack).
This class loader is what we often use, and can be called Classloader.getsystemclassloader () to get it.
We assume that the program does not use the class loader related operation settings or customize the new class loader, then all of the Java classes we write will be loaded by it, and it deserves respect.
Appclassloader find the area of the class is familiar with the classpath, is also a beginner must cross the threshold, there is no flash of the feeling, we follow its class lookup scope to it named Classpath class loader.
Java EE acquisition Path full guide