Request to obtain the path from the request object

Source: Internet
Author: User

Request to obtain the path from the request object

Obtain the path from the request object. For example, the project name is tmall_web. The requested servlet name is loginServlet.

The sample code is as follows:

@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {logger.info ("============= LoginServlet ===================="); PrintWriter out = response. getWriter (); response. setContentType ("text/html, charsets = UTF-8"); out. print ("************************************* **"); out. print ("Project name: tmall_web | serlvet name: loginServlet"); out. print ("************************************* **"); // obtain the Project name:/tmall_web String path = request. getContextPath (); // obtain the project request root path: http: // localhost: 8080/tmall_web/String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; // obtain the Client IP Address: 0: 0: 0: 0: 0: 0: 0: 0: 1 String remoteAddress = request. getRemoteAddr (); // obtain the servlet request Path:/loginServlet String servletPath = request. getServletPath (); // obtain the real root directory of the Project. Hard Disk location: D: \ 0911 \ linpeng_work \ tmall_web \ web \ // If tomcat is deployed: the obtained result shows the project's ROOT path in tomcat, for example, C: \ Users \ Administrator \ Desktop \ apache-tomcat-8.5.20 \ webapps \ ROOT \ String realPath = request. getRealPath ("/"); // get the username: null String remoteUser = request. getRemoteUser (); // obtain the Resource Directory of the project:/tmall_web/loginServlet String requestURI = request. getRequestURI (); // obtain the requested resources of the browser: // localhost: 8080/tmall_web/loginServlet String requestURL = request. getRequestURL (). toString (); out. println ("path:" + path + "<br>"); out. println ("basePath:" + basePath + "<br>"); out. println ("remoteAddr:" + remoteAddress + "<br>"); out. println ("servletPath:" + servletPath + "<br>"); out. println ("realPath:" + realPath + "<br>"); out. println ("remoteUser:" + remoteUser + "<br>"); out. println ("requestURI:" + requestURI + "<br>"); out. println ("requestURI:" + requestURL + "<br> ");}

The above is my personal summary. If there is any error, please correct me. Thank you!

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.