Two methods for obtaining the path in jsp, the method for obtaining the url path (recommended), and jspurl

Source: Internet
Author: User

Two methods for obtaining the path in jsp, the method for obtaining the url path (recommended), and jspurl

<% = Request. getContextPath () %> solves the relative path problem and returns the root path of the site.

<A href = "<% = request. getContextPath () %>/XXX. jsp"> // obtain the absolute path.

<A href = "XXX. jsp"> // obtain the relative path.

<A href = "<% = request. getContextPath () %>/XXXX. jsp"> effectively prevents connection failures.

Request. getContextPath () obtains the project name. If the project is the root directory, a "", that is, an empty string. If the project is dzjc, <% = request. getContextPath () %>/returns dzjc/, and the server path is automatically added. <a href = "XXX. jsp "> it should be the xxx in the current path. for jsp pages, you can also set html: base in the head to solve the path problem, but the most commonly used is request. getContextPath.

If you want to obtain the actual physical path of the project file, you can use: <% = request. getRealPath ("/") %> to output: d: \ web \

The request. getRealPath ("") method is no longer recommended.

This. getServletContext (). getRealPath ("/") to obtain the actual physical path of the project file, that is, to obtain the uri path from the absolute address. this method is

The method in HttpServletRequest's decoration class HttpServletRequestWrapper (using the decoration mode) String url = request. getRequestURI ();

System. out. println (url); // output/2/LoginServlet project name + url of the current execution page

The above two methods for obtaining the path and the method for obtaining the url path (recommended) in jsp are all the content shared by Alibaba Cloud. I hope to give you a reference, we also hope that you can support the customer's home.

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.