Servlet path is not included in <c:url>

1.1k views Asked by At

I have a simple Java Webapp (eg. test) containing two different SpringMVC application.

My web.xml maps them as:

    <servlet-mapping>
        <servlet-name>web</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>restful</servlet-name>
        <url-pattern>/restful/*</url-pattern>
    </servlet-mapping>

Inside the web part I'm using "classic" libraries, such as JSTL core.

I don't understand how to avoid JSTL c:url tag ignoring the URL pattern.

If I write

<c:url value="/browse/"/>

the link is rendered as /test/browse and not /test/web/browse.

What I'm missing?

Thank you

1

There are 1 answers

5
BalusC On BEST ANSWER

The <c:url> does indeed not take servlet path into account. That's your own responsibility. The <c:url> only takes HttpServletRequest#getContextPath() into account.

Either hardcode yourself:

<c:url value="/web/browse" />

Or inline result of HttpServletRequest#getServletPath():

<c:url value="${pageContext.request.servletPath}/browse" />

Or if you're forwarding, inline result of RequestDispatcher#FORWARD_SERVLET_PATH:

<c:url value="${requestScope['javax.servlet.forward.servlet_path']}/browse" />

Wrap if necessary in a custom tag to save boilerplate.