Relative and absolute paths in JSP/Servlet

Source: Internet
Author: User

Absolute path of the Windows operating system: the path starting from the drive letter, such as the relative path of C: \ windows \ system32 \ cmd.exe: The Path starting from the current path, for example, the current path is C: \ windows: To describe the preceding path, enter system32 \ cmd.exe. In fact, the strict relative path should be. \ system32 \ cmd.exe, where,. indicates the current path, which can be omitted in general, but cannot be omitted only in special circumstances. If the current path is c: \ program files and you want to call the above command, you need to enter .. \ windows \ system32 \ cmd.exe where .. is the parent directory. If the current path is c: \ program files \ common files, enter .. \.. \ windows \ system32 \ cmd.exe In addition, there is a special absolute path that does not contain the drive letter, such as \ windows \ system32 \ cmd.exe no matter what the current path is, the system automatically searches for the specified program from the root directory of the current disk. 2. About the relative paths and absolute paths in JSP/Servlet. 2.1 server-side address the server-side relative address refers to the address of your web application, which is parsed on the server side (different from the relative address in html and javascript, they are parsed by the client browser) That means the relative address in jsp and servlet should be relative to your web application, that is, relative to http: // 192.168.0.1/webapp. Where it is used: forward: request in servlet. getRequestDispatcher (address); this address is resolved on the server side. Therefore, you must forward it to. jsp should write this: request. getRequestDispatcher ("/user/. jsp ") the absolute address of this/relative to the current web application webapp is: http://192.168.0.1/ Webapp/user/a. jsp. SendRedirect: <% response. sendRedirect ("/rtccp/user/. jsp "); %> 2.22. The client address. The relative addresses in all html pages are relative to the server root directory ( http://192.168.0.1/ ) Instead of (the directory of the Web application under the Directory) http://192.168.0.1/ Webapp. The address of the form action attribute in Html is relative to the server root directory ( http://192.168.0.1/ ), So if you submit to. jsp: action = "/webapp/user/. jsp "or action =" <% = request. getContextPath () %> "/user/. jsp; submit to servlet is actiom = "/webapp/handleservlet" Javascript is also parsed on the client, so its relative path is the same as the form. Therefore, CSS and Javascript referenced on JSP/HTML pages. it is best to add <% = request. getContextPath () %> to ensure that all referenced files belong to the directory in the Web application. In addition, try to avoid using similar ". ",". /",".. /.. /"and other similar relative paths relative to the file location, so that when the file is moved, it is easy to cause problems. 3. JSP/Servlet get the relative path of the current application and absolute path 3.1 JSP get the relative path of the current application and the absolute path corresponding to the absolute path root directory: request. absolute path of the getRequestURI () file: application. getRealPath (request. getRequestURI (); absolute path of the current web application: application. getRealPath ("/"); get the upper directory of the request File: new File (application. getRealPath (request. getRequestURI ())). in getParent () 3.2 Servlet, obtain the absolute path corresponding to the relative path of the current application and the root directory of the absolute path: request. getServletPath (); absolute file path: request. getSession (). getServletContext (). getRealPath (request. getRequestURI () absolute path of the current web application: servletConfig. getServletContext (). getRealPath ("/"); (ServletContext object obtains several methods: javax. servlet. http. httpSession. getServletContext () javax. servlet. jsp. pageContext. getServletContext () javax. servlet. servletConfig. getServletContext ())

Related Article

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.