Request. getcontextpath is used to solve the problem of relative paths. You can return the root path of the site.

Source: Internet
Author: User

 

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

 

 

 

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.