This article takes the project deployment in the Tomcat server as an example, others believe the same.
The first description of the request page, in the Web, the page path is mainly written in the following types of
1. Request redirect
2, the browser request is requested by the server to the new page (I call "forwarding")
3. Hyperlinks
4. Action of Form form submission
To demonstrate the path notation, first build a project (project name Webpath) and create a servlet (Pathservlet)
The directory structure is as follows
To access the index.jsp file in the directory, for example, the above four paths in Jxf.path.PathServlet.jave (Red section)
1 PackageJxf.path;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 Importjavax.servlet.ServletException;7 ImportJavax.servlet.http.HttpServlet;8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten One Public classPathservletextendsHttpServlet { A Public voiddoget (httpservletrequest request, httpservletresponse response) - throwsservletexception, IOException { - //1. redirect theResponse.sendredirect ("/webpath/index.jsp"); - - //2. Forwarding - //request.getrequestdispatcher ("/index.jsp"). Forward (request, response); + - //3. Hyperlinks + //Response.setcontenttype ("text/html"); A //response.getwriter (). Write ("<thml> at - //4. Form submission - //Response.setcontenttype ("text/html"); - //response.getwriter (). Write ("<thml> - } - in}
The path is written mainly with two points in mind:
1. Who initiated the path (browser, server)
2, the path at the beginning of the direct "/" means the current path (mainly the current path, make it clear)
First of all to analyze "who initiated the path", the above four kinds of wording, in addition to "2, forwarding " is the server initiated, the others are initiated by the browser.
The use of Tomcat server should understand a little, usually we develop time in the browser input "http://localhost: Port number/project name/xxx file" request the required resources, and according to the Tomcat project is in the%tomcat root directory%/ WebApps folder, you can be informed that at this time the browser-initiated request, the code "/" represents the current path should represent the root directory of WebApps
In the code, " forwarding " is the browser that initiates a request to the server, and the server helps it access other resources in the project based on the requested content and returns it to the browser. The entire "Resource path" is initiated by the server, you can view the structure of the Web project after Tomcat deployment, compare the project structure in MyEclipse, can be understood as "/" means that the current directory is the root of the project in the MyEclipse Webroot (also can be interpreted as%tomcat %/webapps/webpath root directory). Since the "/" is indicated as the root of the Webroot, then the index.jsp file should be accessed as "/index.jsp"
Summarize:
1. Browser -initiated path, "/" indicates %tomcat root directory%/webapps root directory
2, the path initiated by the server , "/" represents the WebRoot root directory in the project
The resource path to access is written based on the relative location of the requested resource's file with the path represented by "/".
By the way, the access path of the resource in SRC is mentioned
Create a new TXT file, write something, create a new servlet named SourcePath, and the file directory structure is as follows
Jxf.path.SourcePath.java
1 Public classSourcePathextendsHttpServlet {2 3 Public voiddoget (httpservletrequest request, httpservletresponse response)4 throwsservletexception, IOException {5Response.setcontenttype ("Text/html;charset=utf-8");6 //String Path = Request.getrealpath ("/web-inf/classes/demo.txt");7 //InputStream InputStream = This.getservletcontext (). getResourceAsStream ("/web-inf/classes/demo.txt");8String Path = This. Getservletcontext (). Getrealpath ("/web-inf/classes/demo.txt");9File File =NewFile (path);TenFileInputStream InputStream =Newfileinputstream (file); One byte[] bytes =New byte[1024]; AStringBuilder SB =NewStringBuilder (); - intLen = 0; - while(len = inputstream.read (bytes))!=-1){ theSb.append (NewString (bytes,0, Len)); - } - inputstream.close (); - response.getwriter (). Write (sb.tostring ()); + } - +}
The following methods are available for loading a resource file in a Web project:
Request object. Getrealpath ("/web-inf/classes/demo.txt"); A string that returns the absolute path of the file, but removed after the Java Servlet API 2.1 release, deprecated
The context object. getResourceAsStream ("/web-inf/classes/demo.txt"); //Return the file absolute path created by InputStream
The context object. Getrealpath ("/web-inf/classes/demo.txt"); //String that returns the absolute path of the file
And the path in these methods is the same as the principle of the above URL, which is also initiated by the server, so "/" represents the Webroot root directory, and all the resources under SRC, after the project is deployed, will automatically be placed in the/web-inf/classes directory. So it should be written as "/web-inf/classes/demo.txt."
...... Cond......
Understanding path Problems in a Java Web project