11

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

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.

Raje
  • 3,285
  • 15
  • 50
  • 70
  • Related: http://stackoverflow.com/questions/3655316/browser-cant-access-css-and-images-when-calling-a-servlet-which-forwards-to-a-j/3658735#3658735 – BalusC Apr 26 '12 at 06:00

5 Answers5

18
<%=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

  • and thats what you need right, so you if you say <%=request.getContextPath()%>/help/page/help.htm you will be able to navigate to your page –  Apr 26 '12 at 05:08
  • I write down following code name It works. Thanks For your details help. – Raje Apr 26 '12 at 05:13
  • Scriptlets are soo 90's. OP was already using EL in first place. Why falling back to obsolete technology? – BalusC Apr 26 '12 at 05:59
14

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>
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
1

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

jahroy
  • 22,322
  • 9
  • 59
  • 108
1

You can also use

<c:url>

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

0

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

So your link should look like :

<a  href="<%=request.getContextPath()%>${helpPath}" target="_blank">name</a>
tusar
  • 3,364
  • 6
  • 37
  • 60