Html 我应该如何在jsp页面中获取根文件夹路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10327390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:59:33  来源:igfitidea点击:

how should I get root folder path in jsp page

htmljsp

提问by Raje

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".

我想检索 webapplication 的根路径,然后我想将链接附加到根路径。我试过 request.context 但它返回“http://localhost:8080/webapp/web-inf”。

For example my root folder path is

例如我的根文件夹路径是

path  =  http://localhost:8080/webapp/

and I want to append remaining link to this path

我想将剩余的链接附加到此路径

helpPath= /help/page/help.htm


<a  href="${path} + ${helpPath}" target="_blank">name</a>

Any help or pointer really appreciated.

任何帮助或指针真的很感激。

回答by

<%=request.getContextPath()%> 

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

将为您提供应用程序的根路径,因此在您的情况下它将是http://localhost:8080/webapp

As per comment:

根据评论:

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

will give you your page

会给你你的页面

回答by Rakesh Juyal

You can use pageContext.request.contextPath

您可以使用 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.

但更好的方法是将 base href 设置为此路径,然后按原样使用该路径。

<head>

        <base href="${pageContext.request.contextPath}">

    </head>
<body>
<a  href="${helpPath}" target="_blank">name</a>

</body>

回答by jahroy

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

我相信你可以使用ServletContext的getRealPath()方法。

回答by user1357768

You can also use

你也可以使用

<c:url>

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

jstl 标签。它将为您添加上下文路径。

回答by tusar

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

<%=request.getContextPath()%>会给 /webapp

So your link should look like :

所以你的链接应该是这样的:

<a  href="<%=request.getContextPath()%>${helpPath}" target="_blank">name</a>