1

I'm using InternalResourceViewResolver from Spring to render view existing in /WEB-INF/view. I also use JSTL in view. Now the problem comes after change <url-pattern> of DispatcherServlet in web.xml.

I tried to get servlet path by ${pageContext.request.servletPath} but it returns /WEB-INF/view/skel/welcome.jsp which is the path from WEB-INF directory, while request.getServletPath() returns /h2o inside of controller Java code. <% out.print(request.getServletPath()); %> also returns /WEB-INF/view/skel/welcome.jsp in view file (/WEB-INF/view/skel/welcome.jsp).

It seems to come from difference between request instances in controller Java code & JSP view file. Request is instance of org.apache.catalina.core.ApplicationHttpRequest in JSP view file, but it's instance of org.apache.catalina.connector.RequestFacade in controller Java code.

Until now, fortunately, we install it alone and didn't need to care about <uri-patter> or servlet path. But this project requires integration with other system so getting servlet path from view file to map correct URL of some menu or form action target.

Do you have any idea to get correct servlet path (/h2o) from view file?

2 Answers 2

7

${requestScope['javax.servlet.forward.servlet_path']} should work for you as well.

1
  • my god, how much we must dig to find a simple info like this?! Why there's no "getServletPath()" method or something, I was struggling to find this. Thank you sir
    – rmpt
    Mar 23, 2019 at 12:05
1

Have the controller code put the result of request.getServletPath() into a variable in request scope, and then reference that with jstl in your JSP.

1
  • Thank you. What I wanted is little bit different but I can solve the problem in similar way. I set request instance from Java code as a attribute of request named request and I access it with ${request.servletPath} from JSP view file -- request.setAttribute("request", request).
    – leafriend
    Jan 14, 2013 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.