The relative paths that are invoked in the JSP and class files are different. __js

Source: Internet
Author: User

The relative paths that are invoked in the JSP and class files are different.

In 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 obtain your project absolute path.

1.jsp to get the path:
takes the project name TEST as an example:
(1) Gets the full path of the current page containing the project name: Request.getrequesturi ()
Result:/test/ Test.jsp
(2) Gets the project name: Request.getcontextpath ()
Result:/test
(3) Gets the full name of the current page in the directory: Request.getservletpath ()
Results: If the page/test/jsp/test.jsp
(4) under the JSP directory, get the full path of the page's server: Application.getrealpath ("page. jsp")
Result: D:\resin\webapps\ Test\test.jsp
(5) Gets the absolute path to the server on which the page resides: Abspath=new java.io.File (Application.getrealpath (Request.getrequesturi ())). GetParent ();
Result: D:\resin\webapps\TEST

2. Get the path in the class: the absolute path of the
(1) class: Class.class.getClass (). GetResource ("/"). GetPath ()
Results:/d:/test/webroot/web-inf/classes/pack/
(2) The path to the project: System.getproperty (" User.dir ")
Result: D:\TEST

3. Obtain the path in the servlet:
(1) Get the Engineering Directory: Request.getsession (). The 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) Relative address: Request.getrequesturi ()
Result:/test/test

In addition, the class class also has a getResourceAsStream method, remember that there was a project to read in the same package of an XML, the use of this.

1. How to get the current file path

Common:
(1). Test.class.getResource ("")
Gets the URI directory of the current class Filetest.class file. does not include himself.
(2). Test.class.getResource ("/")
The absolute URI of the current classpath is obtained.
(3). Thread.CurrentThread (). Getcontextclassloader (). GetResource ("")
is also the absolute URI of the current classpath.
(4). Test.class.getClassLoader (). GetResource ("")
is also the absolute URI of the current classpath.
(5). Classloader.getsystemresource ("")
is also the absolute URI of the current classpath.
Try not to use relative paths to the current user directory of System.getproperty ("User.dir"), which can be seen as a variety of results.
(6) The new File (""). GetAbsolutePath () is also available.

Note: If there are spaces in these paths, and the spaces are replaced by%20, you can use Path = Java.net.URLDecoder.decode (path, "Utf-8") for conversion;

2.Web Server
(1). Tomcat
Output System.getproperty ("User.dir") in the class;%tomcat_home%/bin is displayed.
(2). Resin
Not your JSP puts the relative path, is the JSP engine executes this JSP compiles the path of the servlet to the root. For example, to test the file F = new file ("A.htm") with the new document method;
This a.htm is under the resin installation directory.
(3). How to read a file
Use Servletcontext.getresourceasstream () to
(4). Get the true path of the file

String File_real_path=servletcontext.getrealpath ("Mypath/filename");
The use of Request.getrealpath ("/") is not recommended;

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.