3

The below spring controller forwards the request to a static JSP path using RequestDispatcher.forward()

RequestDispatcher rs = getServletContext().getRequestDispatcher("/security/html/blah.jsp");                        
try {
    rs.forward(request, response);
} catch (Exception e) { 
     e.printStackTrace();
}

The code is working perfectly fine in local machines. But when I deploy it on cloud servers, I get a stacktrace stating

javax.servlet.ServletException: Requested URI [//security/html/blah.jsp] does not match Resource path pattern

For some reason, a extra slash is added in the resource path, which is causing the issue. If I enter the URL (by removing the extra slash) manually in the address bar, it works.

Since it is a internal forward, I am not even sure whether an extra slash is added in the local machines or not, because the below URL (with two slashes in the path) works locally but not working on cloud servers

IP:port/context//security/html/blah.jsp

I saw a similar issue on the below link (unfortunately, without a solution)

http://forum.springsource.org/showthread.php?115666-Extra-leading-slash-added-by-RequestDispatcher-include()&p=382817#post382817

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vivek
  • 1,640
  • 1
  • 17
  • 34
  • 4
    What does it do when you remove the leading `/` - `request.getRequestDispatcher("security/html/blah.jsp");`? – neo108 Jun 07 '13 at 04:52
  • Sorry, we are fetching the requestDispatcher from the servlet context not the request. I am modifying the question. If I remove the slash from the URL, I get a IllegalStateException – Vivek Jun 07 '13 at 04:58
  • Leading slash is required. What type of servlet container are you using for development (where it works) and on what container it does not work? – Pavel Horal Jun 09 '13 at 12:48
  • Tomcat. If I dont give the leading slash in the request dispatcher occupied from servlet context, I get IllegalStateException – Vivek Jun 09 '13 at 16:38
  • @Vivek Could you check which implementation of `RequestDispatcher` is used when running on local Tomcat and when deployed on remote machine? Just log `dispatcher.getClass().getCanonicalName()` – dimchez Sep 24 '13 at 13:36
  • org.apache.catalina.core.ApplicationDispatcher from Apache Tomcat 5.5 – Vivek Sep 25 '13 at 05:48
  • Could you provide some more information regarding your configuration, e.g. DispatcherServlet mappings? – xwoker Sep 27 '13 at 11:21
  • Could this (unanswered) question be related? http://stackoverflow.com/questions/12379973/double-forwarded-servletrequest-using-requestdispatcher-forward-malforms-url-a?rq=1 – xwoker Sep 27 '13 at 11:22
  • could you post web.xml ,firstpage and dispatcher xml? – Ali.Mojtahed Sep 27 '13 at 19:16
  • @xwoker, yes it seems to be the same problem. But no answer for that question as well :( – Vivek Sep 28 '13 at 04:59
  • @Vivek: Can you please check the `URL` of the page to which the original request was made? How does is look like? – me_digvijay Sep 28 '13 at 18:51
  • As I mentioned, it looks like "/security/html/somepageName.html" – Vivek Sep 30 '13 at 06:35
  • If I may ask Vivek, under which folder is the security folder ? – Raymond Nakampe Sep 30 '13 at 17:34
  • Similar behavior is noted at https://issues.apache.org/bugzilla/show_bug.cgi?id=46131 where a Context element in a config file wrongly had "/" for the root context instead of the empty string "". You might check if that's the case in the Tomcat configuration on your cloud servers. – Alanyst Sep 30 '13 at 21:27
  • I checked the mentioned configuration in my tomcat server. In my local server (where it seems to be working fine) has a context path of "/" which is same as the cloud server. The given link suggests to make context path empty "", I tried that, but still getting the same issue :( – Vivek Oct 01 '13 at 08:12
  • hey Vivek, can you share your web.xml file? – Madhura Oct 01 '13 at 09:35

1 Answers1

-1

You can replace the leading / with getServletContext().getContextPath() to make it work in any environment. Please check and let me know if this solves your problem.

Jasperin
  • 53
  • 7