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!