Issue
I have an web app with context root 'myapp'. and one servlet myapp/page1 and one jsp myapp/page2.jsp. From servlet I am redirecting to jsp using 'response.sendRedirect('/page2.jsp')
. It is failing. I have noticed that the request redirect is to 'http://localhost:8080/page2.jsp
' (context root is not being included in the URL).
My understanding is that context root would be included by the container.
Env: Tomcat 7, web.xml version 3.0,
What am I missing?
Solution
No, you're using a relative path. An absolute path to page2.jsp would be "http://localhost:8080/"+request.getContextRoot()+"/page2.jsp"
. Another advantage of this approach is, if you should ever change your appserver, to say JBoss or WebLogic or whatever, you won't need to change your code.
Answered By - hd1
Answer Checked By - Timothy Miller (JavaFixing Admin)