Assume that your web application is named "news". In the browser, enter the Request Path:
Http: // localhost: 8080/news/main/list. jsp
Then execute the line-oriented code and print the following results:
1. system. Out. println (request. getcontextpath ());
Print result:/news
2. system. Out. println (request. getservletpath ());
Print result:/main/list. jsp
3. system. Out. println (request. getrequesturi ());
Print result:/news/main/list. jsp
4. system. Out. println (request. getrealpath ("/"));
Print result: F: \ Tomcat 6.0 \ webapps \ news \ test
========================================================== ========================================================== =====
<% = Request. getcontextpath () %> returns the root path of the site to solve the relative path problem.
But you don't need to. For example, <a href = "<% = request. getcontextpath () %>/CATALOG. jsp">
Just use <a href = "catalog. jsp">. The two files are in the same directory.
For example, if you want to generate a file and put it in a directory on the server, you can use request. getcontextpath () +/DIR to form a complete directory structure!
However, in the JSP file, the path obtained through request. getcontextpath () is empty. Why?
You have not configured the path attribute in the context, so your project file is in the root directory, which is equivalent to Path = ""; that is, if you enter your server IP address in your browser, your JSP page is displayed, instead of the default Tomcat page. the character string obtained by getcontextpath () is empty; it obtains the virtual directory;
If you want to obtain the actual physical path of the project file, you can use: <% = request. getrealpath ("/") %> to output: D:/Web
Request. getscheme ();
The name of the returned protocol. The default value is HTTP.
Request. getservername ()
The host name displayed in your browser is returned. You can give it a try.
Getserverport ()
Get server port number