Search code examples
javajspurlservletsforward

Why Head First book uses "/" when forwarding request from Servlet to JSP?


While reading Head First JSP and Servlets, I learnt that forward slash (/) is used to represent root of Container. So notice how in this example from the book, there is no myApp when using (/).

But when they are forwarding request (and not using response.sendRedirect), they specify (/). Pay attention to the part where they write /result.jsp in this picture: enter image description here

I am a little confused right now. I have project called myApp. And inside it I have index.html, servlet.class and result.jsp.

What I get in the end in browser is http://localhost:8080/myApp/result.jsp. But while reading link I provided at the top, it should be http://localhost:8080/result.jsp. Because as I said (and so did the link), forward slash (/) represents root of container. And root is http://localhost:8080 and not http://localhost:8080/myApp.

What the weirdest thing is that it works both ways - if I type result.jsp and /result.jsp. Can someone please clear this, as it is killing me :)

The thing I am mostly confused is how does /result.jsp even work, when it should take us to http://localhost:8080/result.jsp and my result.jsp is inside myApp and not inside Container technically.


Solution

  • As per the documentation

    If the path begins with a "/" it is interpreted as relative to the current context root.

    So the path is relative to your context root, not your whole application server.