Your location:Tech News>News>Script>* Java get to explore a path to achieve

Latest News

* Java get to explore a path to achieve

Author:fsadmin

Views:

  (In the init method of the servlet in), getRealPath ( "web.getRealPath ( "/"); // not recommended to obtain project root path

  (,), ,.getRealPath (,.getRequestURI ()); // Get jsp path, , method is relatively easy to use, and can be used directly in the jsp servlet

  (,), ,.getSession ().getServletContext ().getRealPath ( "/"); // get the root path engineering, , method is relatively easy to use, and can be used directly in the jsp servlet

  (4), ,.getClass ()., ().getResource ( "").getPath (); // get the path in engineering ,es, this method can be used in any jsp, servlet, java file, because no matter jsp, servlet are actually java program, are a class.So it should be a common approach.

  0, absolute and relative paths on

  ,.Understand the basic concept of absolute path: absolute path is on your home page file or directory on your hard drive true path, (URL and the physical path) example: C: xyz est.Method: String filepath = , represents the test.Absolute path to strutsTest name for the project file.http: // www.sun.com / index.htm also represents an absolute URL path.Relative path: The path to the directory with a relative benchmark.Web contains the relative path (relative to the directory in HTML), such as: Servlet, "/" represents the root directory of Web getRealPath ( "")s.And represents a relative physical path.For example: "https: // www.jb51.net / article / "represents the current directory," https: // www.jb51.net / "on behalf of parent directory.This represents a similar, but also belong to a relative path.Also on the URI, URL, URN, etc., please refer to the relevant RFC document standards.RFC ,396: Uniform Resource Identifiers (URI): Generic Syntax, (http: // www.ietf.org / rfc / rfc2396.,) 2.About relative paths JSP / Servlet and the absolute path. 2.Relative address address of the server 1 server-side refers to the relative address of your web ,, the address is in the server-side parsing (other than html and javascript relative address, they are by the client browser resolution)

  1, ,.getRealPath

  Methods: request.getRealPath ( "/")

  Resulting path: C: \ Program Files \ Apache Software Foundation \ Tomcat 5.5 \ webapps \ strutsTest ",

  ,.,.,

  ,.Methods: request.

  getRealPath ( "").Resulting path: C: \ Program Files \ Apache Software Foundation \ Tomcat 5

  5 \ webapps \ strutsTest.request

  request.2, request ").C: \ Program Files \ Apache Software Foundation \ Tomcat 5

  5 \ webapps \ strutsTest \ web.xml.xml

  getParameter ( "");.getMyFile ();

  ActionForm.Method: String filepath = request

  getParameter ( "myFile");.The resulting path: D: \ VSS installation directory

  TXT.txt

  getMyFile ();.The resulting path: D: \ VSS installation directory

  TXT.txt

  3, to obtain system path

  In the ActionForm myFile for private String myFile;

  In the jsp page: To

  --------------------------------------------------

  --------------------------------------------------

  In the Application:

  getProperty ( "user

  ,.dir ").In the Servlet:

  ServletContext servletContext = config

  getServletContext ();.String rootPath = servletContext

  getRealPath ( "/");.In the jsp:

  application

  application.4 Other 1

  1

  1.String path = getServletContext ()

  getRealPath ( "/");.This will get the full path of the web project

  For example: E: \ eclipseM9 \ workspace \ tree ",

  ,

  ,

  2.,

  this.,.,.,.,

  ,

  ,

  ,.,.,.,.,

  ,

  ,

  3.request.,

  ,

  ,

  ,

  ,

  ,

  1. ,?,

  ,.,.,

  2. class.getClassLoader.,? ,

  ,.

  ,

  ,

  request.,

  application.,

  ,.,

  System.,.,

  ,

  ,

  request.,

  ActionForm.,

  ,?,.txt

  ,

  this.,.,.,.,

  ,

  this.,.,.,.,

  ,

  ,

  request.,

  

  * Java get to explore a path to achieve

Recommend article

Relate article