String path = Struts2util.getservletcontext (). Getrealpath ("/");
<% String Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";%>
One, method one
(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), the absolute path of the class: Class.class.getClass (). GetResource ("/"). GetPath ()
Results:/d:/test/webroot/web-inf/classes/pack/
This.getclass (). getClassLoader (). GetResource (""). GetPath ();//Get the path under Project classes, this method can be used in any Jsp,servlet,java file, Because either Jsp,servlet is actually a Java program, it's a class. So it should be a common method.
(5), Tomcat to get the absolute path
Private String projectname= "Sz_pro"; The name of your project (File.separator)//Gets the absolute path of the current project (Linux not applicable) public String Getporjectpath () { string nowpath;// The path to the current Tomcat bin directory, such as D:\java\software\apache-tomcat-6.0.14\bin String tempdir; Nowpath=system.getproperty ("User.dir"); Tempdir=nowpath.replace ("Bin", "WebApps"); Change bin folder to WebApps file inside tempdir+= "\ \" + ProjectName; Determine if there is a suffix if (! ") /". Equals (Tempdir.substring (Tempdir.length ()))) { TempDir + = File.separator; } return tempdir;}
There is also a method that can be used to get the absolute path of the class (current classpath) Windows,linux can
String Classespath = This.getclass (). getClassLoader (). GetResource (""). GetPath (); Logger.info ("============= Constant.getporjectpath () = "+classespath"; String Photopath = Constant.getporjectpath (classespath) + "virtualdir/upload/data/photo/fmcg/"; Logger.info ("======= ======photopath= "+photopath);
Gets the absolute path of the current project public static string Getporjectpath (String classespath) { string tempdir; String classpath[] = Classespath.split ("WebApps"); TEMPDIR=CLASSPATH[0]; if (! " /". Equals (Tempdir.substring (Tempdir.length ()))) { TempDir + = File.separator; } return tempdir; }
(6) Struts acquisition path (Struts2-core-2.0.11.2.jar)
filename = request.getparameter ("filename"); SimpleDateFormat df = new SimpleDateFormat ("YyyyMMdd"); SimpleDateFormat df1 = new SimpleDateFormat ("YYYYMMDDHHMMSS");//Gets the root path in the project. If the item deletes the photo, it also loses the file folder = new file (Servletactioncontext.getservletcontext (). Getrealpath ("/") + "uploadimages/" + Df.format (New Date ())), if (!folder.exists ()) { folder.mkdirs ();}
Or
public string GetPath (string uploadtype,string loginId) {Actioncontext ac = Actioncontext.getcontext (); ServletContext sc = (ServletContext) ac.get (Servletactioncontext.servlet_context); String Savepath = Sc.getrealpath ("/"); String Realpath = "WebApps" + sc.getcontextpath (). Replace ("/", "\ \"); String Path = "virtualdir\\upload\\data\\filezip\\" +loginid+ "\ \"; if ("1". Equals (Uploadtype)) {path = "virtualdir\\ upload\\data\\photozip\\ "+loginid+" \ ";} Savepath = savepath.substring (0, Savepath.length ()-Realpath.length ()-1) + Path;return Savepath;}
Second, method two
The relative paths that are called in the JSP and class files are different. In the JSP, the root directory is webroot in the class file, the root directory is webroot/web-inf/classes of course you can also use System.getproperty ("User.dir") to get your project's absolute path.
Another: The way to get the path in detail in Jsp,servlet,java!
Get path in 1.jsp:
Take the project name test as an example:
(1) Get the full path of the current page containing the project name: Request.getrequesturi ()
Results:/test/test.jsp
(2) Get project name: Request.getcontextpath ()
Results:/test
(3) Get the current page in the same directory under the full name: Request.getservletpath ()
Result: If the page is/test/jsp/test.jsp in the JSP directory
(4) Get the full path of the server on which the page is located: Application.getrealpath ("page. JSP")
Result: D: "Resin" WebApps "TEST" test.jsp
(5) Obtain the absolute path of the server on which the page is located: Abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Result: D: "Resin" WebApps "TEST"
2. Get the path in the class:
(1) The absolute path of the class: Class.class.getClass (). GetResource ("/"). GetPath ()
Results:/d:/test/webroot/web-inf/classes/pack/
(2) The path to get the project: System.getproperty ("User.dir")
Result: D: "TEST
3. Get the path in the servlet:
(1) Get the Project catalog: Request.getsession (). Getservletcontext (). Getrealpath ("") parameter can be specific to the package name.
Result: E: "Tomcat" WebApps "TEST
(2) Get IE Address bar address: Request.getrequesturl ()
Results: Http://localhost:8080/TEST/test
(3) Get relative Address: Request.getrequesturi ()
Results:/test/test
1. Basic point of view
on the 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 to 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 the servlet, "/" represents the directory used by the Web. and the relative schematic of the physical path. For example: "./" represents the current directory, ". /"represents the parent directory. This similar signal is also a relative path.
For additional information about Uri,url,urn, please refer to the RFC documentation rules.
RFC 2396:uniform Resource Identifiers (URI): Generic Syntax,
(/artimage/20100102/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 that is used in relation to your Web address, which is analyzed on the server side (not the same as the relative address in HTML and JavaScript). They are analyzed by the client reader) that is, the relative address of this moment in the JSP and servlet should be relative to your web usage, that is, relative to http://192.168.0.1/webapp/.
They are used in the following locations:
Forward:servlet in Request.getrequestdispatcher (address); This address is analyzed on the server side, so You want to forward to a.jsp should write: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current web use WebApp, its absolute address is:/http 192.168.0.1/webapp/user/a.jsp. Sendredirect: <%response.sendredirect ("/rtccp/user/a.jsp") in the JSP;%>
2.2, the address of the client
The relative addresses in all the HTML pages are relative to the server root (HTTP://192.168.0.1/), not http://192.168.0.1/webapp/(with the directory that the Web uses 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/), so if committed to a.jsp: action= "/webapp/user/a.jsp" or action= " <%=request.getcontextpath ()% > "/user/a.jsp;
Submitted to the servlet as actiom= "/webapp/handleservlet" JavaScript is also parsed on the client side, so its relative path is the same as the form form.
As a result, it is generally better to precede attributes such as css,javascript.action such as jsp/html pages with
<%=request.getcontextpath ()%> To ensure that the referenced files belong to the directory in Web usage. In addition, we should try to prevent the use of similar ".", "./", ". /.. /"Similar relative path relative to the location of the file, so when the file is moved, it is very simple to doubt.
3. Get the relative and absolute paths currently used in Jsp/servlet
3.1 The current used relative and absolute paths in the JSP
Absolute path for root directory: Request.getrequesturi ()
Absolute path to File: Application.getrealpath (Request.getrequesturi ());
The absolute path used by the current Web: Application.getrealpath ("/");
Gets the upper-level directory of the requested file: New file (Application.getrealpath (Request.getrequesturi ())). GetParent ()
3.2 servlet gets the relative and absolute paths currently used
Absolute path for root directory: Request.getservletpath ();
Absolute path to File: Request.getsession (). Getservletcontext (). Getrealpath
(Request.getrequesturi ())
Absolute path used by the current Web: Servletconfig.getservletcontext (). Getrealpath ("/");
(ServletContext objects are made in several forms:
Javax.servlet.http.HttpSession.getServletContext ()
Javax.servlet.jsp.PageContext.getServletContext ()
Javax.servlet.ServletConfig.getServletContext ()
)
Get relative path in 4.java class, Essentials of Absolute path
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.currentthrea D (). 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 class file is located in the path System.out.println (new file ("/"). GetAbsolutePath ()); System.out.println (System.getproperty ("User.dir")); The Java class in the}4.2 server obtains the current path (1). The Weblogic webapplication System file root directory is the root directory where your Weblogic installation is located. For example: If your WebLogic installed in c:eaweblogic700 ..... So, your file root path is C:. So, there are two forms that allow you to access your server-side files: A. Use absolute path: For example, put your parameter file in C:yourconfigyourconf.properties, use the new FileInputStream directly ("Yourconfig /yourconf.properties "); B. Using a relative path: the root of the relative path is the root of your webapplication, that is, the top level directory of the Web-inf, and puts your parameter file in Yourwebappyourconfigyourconf.properties, which is used: New FileInputStream ("./yourconfIg/yourconf.properties "); Both of these forms can be selected by themselves. (2). Tomcat outputs System.getproperty ("User.dir") in the class, and%tomcat_home%/bin (3) is displayed. Resin is not the relative path that your JSP puts, it is the JSP engine that executes this JSP to compile the path of the servlet to the root. For example, the new file method is used for the test of file F = new ("a.htm"); This a.htm is in the installation directory of resin (4). How to read the relative path? Examples of getresource or getResourceAsStream in Java files are: GetClass (). getResourceAsStream (FilePath);//filepath can be "/filename", Here/on behalf of the WEB revealed the root path under Web-inf/classes The default path to apply the essentials is: web-inf/classes. has been tested in Tomcat. The use of the above content can be processed in the Web using the server side, move files, find files, copy, delete files and other operations, while the relative address of the server, the absolute address point of view more clearly. Recommended reference URI, RFC rules text block. At the same time, the content of Java.io.File. Java.net.URI is clear, and the other aspects can be more thorough and thorough.5.
Specify a path in the. Properties
As in Common.properties
Upload_path=. /.. /virtualdir/uploadlocation_alarm_image=./. /.. /resource/images/locationimage.giflocation_last_alarm_image=./. /.. /resource/images/locationimage2.gif
String Thisuserpath = "" + File.separator + "";
Java relative path/absolute path Summary (RPM)