JSP contextPath to link to page

I got a JSP project where i am trying to create a link in my nav.jsp that points to my frontpage (index.jsp) for navigation purposes.

<nav>
    <ul class="menu">
        <li><a href="${pageContext.request.contextPath}/index.jsp"><span>Frontpage</span></a></li>
    </ul>
</nav>

But when i run my project, navigate to the newWebsite2015.jsp. And try to use the link in the navigation menu to get back. I get the following error.

HTTP Status 404 - /MyNewRandomBlog1.0/article/index.jsp

But why does it look in the article folder for the index.jsp file, instead of going to the root of the web-directory and locate the index.jsp?

in my footer.jsp file i use pageContext.request.contextPath to retrieve an image, which works fine.

<img src="${pageContext.request.contextPath}/images/farOgSon-172x190px.png" alt="image1"/>

So i dont really understand that it does not work with the anchor.

My web.xml looks like

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>MyNewRandomBlog1.0</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Hirachy

Upvotes: 0

Views: 5403

Answers (1)

${pageContext.request.contextPath} will take your current context path, so that its taking the article folder.

You should use this instead

request.getContextPath()/index.jsp

Upvotes: 1

Related Questions