how should I get root folder path in jsp page

前端 未结 5 1336
南笙
南笙 2020-12-18 00:00

I want to retrieve root path of webapplication, then I want to append link to root path. I tried request.context but it returns \"http://localhost:8080/webapp/web-inf\".

相关标签:
5条回答
  • 2020-12-18 00:25

    You can also use

    <c:url>
    

    jstl tag. It will add the context path for you.

    0 讨论(0)
  • 2020-12-18 00:27
    <%=request.getContextPath()%> 
    

    will give you the rootpath of your application so in your case it will be http://localhost:8080/webapp

    As per comment:

    <%=request.getContextPath()%>/help/page/help.htm 
    

    will give you your page

    0 讨论(0)
  • 2020-12-18 00:29

    I believe you can use the getRealPath() method of the ServletContext.

    0 讨论(0)
  • 2020-12-18 00:30

    You can use pageContext.request.contextPath

    ${pageContext.request.contextPath}
    

    So you can use,

    <a  href="${pageContext.request.contextPath}${helpPath}" target="_blank">name</a>
    

    But the better way is to set the base href to this path and then use the path as it is.

    <head>
    
            <base href="${pageContext.request.contextPath}">
    
        </head>
    <body>
    <a  href="${helpPath}" target="_blank">name</a>
    
    </body>
    
    0 讨论(0)
  • 2020-12-18 00:36

    <%=request.getContextPath()%> will give /webapp

    So your link should look like :

    <a  href="<%=request.getContextPath()%>${helpPath}" target="_blank">name</a>
    
    0 讨论(0)
提交回复
热议问题