Search code examples
javajsptomcatservletsforward

Forwarding a request from servlet to .jsp fails when deployed on remote server


I have been stuck with this problem for two days now and I cant find a solution. I have this project structure as above: enter image description here I want to forward a request from Survey(it is a Servlet) to survey.jsp. Now when I deploy this from IntelliJ on localhost the forward is made with success. Now i take the .war and use the Tomcat Apache manager to deploy it to a dedicated Server. When i call dedicatedserverip:8080 the index.jsp loads properly as in localhost:8080. But when it comes to forward there are two cases:

  1. When the name of .war file is different from survey.war when the forward happens I get error 404 . (In this case I think that the request is forwarded to dedicatedserverip:8080)
  2. When the name of .war file is survey.war when forward happens it happens to load again index.jsp page. (in this case I think the request is forwarded to dedicatedserverip:8080/survey)

Below is the code I use to forward the request:

req.getRequestDispatcher("/survey.jsp").forward(req, resp);

Now my question is: Is there something done wrong? Or is there something that I must understand that I haven't? How can I fix it and get the needed result as in localhost?


Solution

  • Finally, I solved the problem. It had nothing to do with the forward, the problem was in the submit form action. Action was action="/survey". I replaced with ${pageContext.request.contextPath}/survey . And it solved the problem. This explains why the index page was called when .war name was survey.warand error page when it was different. Thank you for your support.