. Jsp get path

Source: Internet
Author: User

<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. Io. *" errorpage = "" %>

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

<Title> untitled document </title>

</Head> <body>

Physical path of the current web application: <% = application. getrealpath ("/") %> <br>

The physical path of the JSP file you requested: <% = application. getrealpath (request. getrequesturi () %> <br>

<%

String Path = application. getrealpath (request. getrequesturi ());

String dir = new file (PATH). getparent ();

Out. println ("physical path of the directory where the current JSP file is located" + DIR );

%>

</Body>

</Html>

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

String pipeline Path = request. getservletpath (); // Virtual Path

String realpath = request. getrealpath (physical path); // physical path

Obtain the relative and absolute paths of the current application in JSP.

Absolute path corresponding to the root directory: request. getrequesturi ()

Absolute path of the file: application. getrealpath (request. getrequesturi ());

Absolute path of the current web application: application. getrealpath ("/");

Obtain the upper-level directory of the request file: New file (application. getrealpath (request. getrequesturi (). getparent ()

Servlet to obtain the relative and absolute paths of the current application

Absolute path corresponding to the root directory: request. getservletpath ();

Absolute path of the file: request. getsession (). getservletcontext (). getrealpath

(Request. getrequesturi ())

Absolute path of the current web application: servletconfig. getservletcontext (). getrealpath ("/");

(The servletcontext object can be obtained in the following ways:

Javax. servlet. http. httpsession. getservletcontext ()

Javax. servlet. jsp. pagecontext. getservletcontext ()

Javax. servlet. servletconfig. getservletcontext ()

)

Methods for obtaining relative paths and absolute paths in Java class

Obtain the absolute path in a separate Java class

According to the java. Io. File doc, we can see that:

By default, the directory represented by new file ("/") is system. getproperty ("user. dir ").

ClickProgramObtain 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 ("/"); // path of the class file

System. Out. println (new file ("/"). getabsolutepath ());

System. Out. println (system. getproperty ("user. dir "));

}

}

The Java class in the server gets 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 .....

The root path of your file is c :\.

Therefore, you can access files on your server in two ways:

A. Use absolute path:

For example, put your parameter file in c: \ yourconfig \ yourconf. properties,

Use new fileinputstream ("yourconfig/yourconf. properties") directly ");

B. Use the relative path:

The root directory of the relative path is the root path of your webapplication, that is, the upper-level directory of the WEB-INF, put your parameter file

In yourwebapp \ yourconfig \ yourconf. properties,

This method is used as follows:

New fileinputstream ("./yourconfig/yourconf. properties ");

You can select either of the two methods.

(2). Tomcat

Output System. getproperty ("user. dir") in the class; % atat_home %/bin

(3). Resin

It is not the relative path of your JSP, but the JSP Engine executes this JSP to compile it into a Servlet

For example, use the new file method to test file F = new file ("a.htm ");

This a.htm is in the resin installation directory.

(4). How to read relative paths?

In a Java file, either getresource or getresourceasstream can be used.

For example, getclass (). getresourceasstream (filepath); // filepath can be "/FILENAME", where/Represents the Web

Publish WEB-INF/classes under root path

By default, the path to this method is: WEB-INF/classes. It has been tested in Tomcat.

 

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.