Methods for obtaining absolute and relative paths in jsp

Source: Internet
Author: User
Tags relative

// Obtain the relative path and absolute path of the current file in Servlet and JSP.
 
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 ()
3.2 obtain the relative and absolute paths of the current application in Servlet
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 code is as follows: Copy code

Out. println ("Absolute path corresponding to the root directory:" + request. getRequestURI () + "<br/> ");
 
String strPathFile = application. getRealPath (request. getRequestURI ());
Out. println ("Absolute path of the file:" + strPathFile + "<br/> ");
Out. println (application. getRealPath (request. getRequestURI ()));
String strDirPath = new File (application. getRealPath (request. getRequestURI (). getParent ();
Out. println ("absolute directory path:" + strDirPath + "<br/> ");


 

Program

The code is as follows: Copy code

String classPath = this. getClass (). getClassLoader (). getResource ("/"). getPath ();
  
Try {
ClassPath = URLDecoder. decode (classPath, "gb2312 ");
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}


 

Example

The code is as follows: Copy code

<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 + "</br> ");
String realPath1 = "http: //" + request. getServerName () + ":" + request. getServerPort () + request. getContextPath () + request. getServletPath (). substring (0, request. getServletPath (). lastIndexOf ("/") + 1 );
Out. println ("web URL path:" + realPath1 );
%>
</Body>

Get the absolute path of the project in servlet
String filePath = this. getServletConfig (). getServletContext (). getRealPath ("/");

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 ("/");

ServletContext objects can be obtained in the following ways:

The code is as follows: Copy code

Javax. servlet. http. HttpSession. getServletContext ()

Javax. servlet. jsp. PageContext. getServletContext ()

Javax. servlet. ServletConfig. getServletContext ()

Absolute path of the file: request. getSession (). getServletContext (). getRealPath (request. getRequestURI ())

Absolute path of the current web application: servletConfig. getServletContext (). getRealPath ("/");

Example

// The application object in JSP is the ServerContext in Servlet, so it is obtained in Servlet as follows
// Import java. io. File;

System. out. println ("Absolute path corresponding to the root directory:" + request. getServletPath () + "<br/> ");

String strPathFile = request. getSession (). getServletContext (). getRealPath (request. getRequestURI ());
System. out. println ("Absolute file path:" + strPathFile + "<br/> ");

String strDirPath = new File (request. getSession (). getServletContext (). getRealPath (request. getRequestURI (). getParent ();
System. out. println ("absolute directory path:" + strDirPath + "<br/> ");

The file name cannot contain the following characters :/:*? "<> |

Obtain the full path of a Web project
String strDirPath = getServletContext (). getRealPath ("/");

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.