Two Methods of Obtaining Path in jsp and Method of Obtaining Path in url of Recommendation

  • 2021-11-13 02:23:58
  • OfStack

< %=request.getContextPath()% > Is to solve the problem of relative path, and can return to the root path of the site.

< a href=" < %=request.getContextPath()% > /XXX.jsp" > //This gives you an absolute path

< a href="XXX.jsp" > //This gives you a relative path

< a href=" < %=request.getContextPath()% > /XXXX.jsp" > It can prevent the failure of connection more effectively.

request. getContextPath () gets the name of the project, if the project is the root directory, it gets a "", that is, an empty note string, if the project is dzjc, < %=request.getContextPath()% > The/will get dzjc/, and the path on the server side will be automatically added. < a href="XXX.jsp" > It should refer to the xxx. jsp page under the current path. Sometimes html: base can be set in head to solve the path problem, but request. getContextPath is used most.

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

Now the method request. getRealPath ("") is no longer recommended. You can use

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

The method in HttpServletRequestWrapper of HttpServletRequest (using decorative mode) String url = request. getRequestURI ();

System. out. println (url); //Output/2/LoginServlet Project Name + url of Current Execution Page


Related articles: